Skip to content

Commit b2a8374

Browse files
authored
Merge pull request #5 from JZDesign/document.functions.1
Document functions
2 parents 9d341f7 + a42447e commit b2a8374

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

Sources/HTTPEngine/HTTPEngine.swift

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,23 @@ public typealias ResponseValidationClosure = (Int) -> ValidResponse
88
public struct HTTPEngine {
99
public init() {}
1010

11+
12+
/// Creates a URLRequest Object
13+
/// - Parameters:
14+
/// - method: HTTPMethod - `.get, .put. post` etc.,
15+
/// - url: A URL Object
16+
/// - body: Data?: The body data to send with a request
17+
/// - header: A dictionary of HTTP Request Headers - `["Content-Type": "text", "Some Key": "Some Value"]`
18+
/// - Returns: A fully constructed URLRequest
19+
///
20+
/// -- Headers
21+
///
22+
/// By default all requests have the `["Accept-Encoding": "gzip;q=1.0,compress;q=0.5"]` header included.
23+
///
24+
/// All `.post, .put, & .patch` requests also contain `["Content-Type": "application/json"]` by default.
25+
///
26+
/// These values can be overridden by including those headers as arguments when calling this function
27+
///
1128
public func buildRequest(
1229
method: HTTPMethod,
1330
url: URL,
@@ -32,7 +49,33 @@ public struct HTTPEngine {
3249

3350
return request
3451
}
35-
52+
53+
54+
/// Makes a request via HTTP
55+
/// - Parameters:
56+
/// - method: HTTPMethod - `.get, .put. post` etc.,
57+
/// - urlString: URL domain + path as a string: `"abc.com/some/path"`
58+
/// - body: Data?: The body data to send with a request
59+
/// - header: A dictionary of HTTP Request Headers - `["Content-Type": "text", "Some Key": "Some Value"]`
60+
/// - validator: `(Int) -> Bool` - A function to validate the response code of the request. By default, makeRequest() will fail if the status code does not fall within the 200 - 299 range. To override this, pass in a function that compares the status code and returns a boolean. True == success, False == failure. Upon failure an error will be thrown that contains the HTTPURLResponse for inspection.
61+
///
62+
/// - Returns: AnyPubliser<Data, Error>
63+
///
64+
/// -- Headers
65+
///
66+
/// By default all requests have the `["Accept-Encoding": "gzip;q=1.0,compress;q=0.5"]` header included.
67+
///
68+
/// All `.post, .put, & .patch` requests also contain `["Content-Type": "application/json"]` by default.
69+
///
70+
/// These values can be overridden by including those headers as arguments when calling this function
71+
///
72+
/// -- Validation
73+
///
74+
/// By default the validation checks for a 200-299 status code and fails if the code is out of bounds
75+
/// ```swift
76+
/// // example validator
77+
/// validator: { $0 == 202 }
78+
/// ```
3679
public func makeRequest(
3780
method: HTTPMethod,
3881
url urlString: String,
@@ -54,8 +97,9 @@ public struct HTTPEngine {
5497
}
5598
.eraseToAnyPublisher()
5699
}
57-
58-
func validateResponse(_ response: URLResponse?, validator: ResponseValidationClosure? = nil) throws {
100+
101+
102+
private func validateResponse(_ response: URLResponse?, validator: ResponseValidationClosure? = nil) throws {
59103
let response = try response as? HTTPURLResponse ??? Errors.Response.couldNotRetrieveStatusCode
60104

61105
guard let validator = validator else {

Sources/HTTPEngine/Utilities.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ public extension URLRequest {
77
}
88
}
99

10+
11+
/// A publisher that immediately throws
12+
/// - Parameters:
13+
/// - type: The expected type of the publisher
14+
/// - error: The error to throw
15+
/// - Returns: AnyPublisher<Type, Error> That fails immediately
16+
///
17+
/// -- Use case
18+
/// Sometimes a function returns a publisher, but we need to unwap a value or perform a try catch before a publisher can be created. In this instance we can return this publisher instead to allow the publisher chain to handle those errors.
1019
public func ThrowingPublisher<T>(forType type: T.Type, throws error: Error) -> AnyPublisher<T, Error> {
1120
Result<T?, Error> { nil }
1221
.publisher
@@ -19,7 +28,14 @@ public func ThrowingPublisher<T>(forType type: T.Type, throws error: Error) -> A
1928
}.eraseToAnyPublisher()
2029
}
2130

31+
2232
infix operator ??? : TernaryPrecedence
33+
/// Unwrap or throw
34+
/// - Parameters:
35+
/// - left: Any Optional
36+
/// - right: Error
37+
/// - Throws: The error from the right
38+
/// - Returns: The unwrapped optional from the left
2339
public func ???<T>(_ left: Optional<T>, right: Error) throws -> T {
2440
guard let value = left else { throw right }
2541
return value

0 commit comments

Comments
 (0)