Skip to content

Commit fb60cbb

Browse files
authored
Release 0.2.0 (#10)
# Release 0.2.0 This release contains convenient helper methods that wrap Swift's Combine + URLSession and supplies an enjoyable and succinct developer experience to: - Build and make requests - Encode json Body - Decode the Json Responses - Handle Status Code Validation - Return developer friendly errors to work with ## What is not in this release All the desired helper methods `patch` `delete` etc., ## Notes The branch was titled `release1.0` by mistake.
1 parent 45a4ffb commit fb60cbb

File tree

6 files changed

+60
-9
lines changed

6 files changed

+60
-9
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
# HTTPEngine
2-
![Swift](https://github.com/JZDesign/HTTPEngine/workflows/Swift/badge.svg)
2+
![Swift](https://github.com/JZDesign/HTTPEngine/workflows/Swift/badge.svg) [![SPM compatible](https://img.shields.io/badge/SPM-compatible-e66f20.svg?style=flat)](https://github.com/apple/swift-package-manager) [![Docs](https://img.shields.io/badge/Jazzy-Documentation-634fb3.svg?style=flat)](https://jzdesign.github.io/HTTPEngine/)
33

4-
A description of this package.
4+
A convenience wrapper around Swift's Combine and URLSession to make `URLRequests`
5+
6+
## SPM
7+
```swift
8+
dependencies: [
9+
.package(url: "https://github.com/JZDesign/HTTPEngine.git", .upToNextMajor(from: "0"))
10+
],
11+
```
12+
13+
## [View Documentation](https://jzdesign.github.io/HTTPEngine/)
14+
15+
Documentation generated by [Jazzy](https://github.com/realm/jazzy).

Sources/HTTPEngine/Errors.swift

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import Foundation
22

33
public struct Errors {
4-
enum Request: Error {
4+
public enum Request: Error {
55
case invalidURL
66
}
77

8-
enum Response: Error {
8+
public enum Response: Error {
99
case couldNotRetrieveStatusCode
1010
case unexpectedStatusCode(HTTPURLResponse)
1111
case redirect(Int)
1212
case unknown(Int)
1313

14-
enum ClientError: Error {
14+
public enum ClientError: Error {
1515
case badRequest_400
1616
case invalidCredentials_401
1717
case forbidden_403
@@ -22,7 +22,7 @@ public struct Errors {
2222
case unkown(Int)
2323
}
2424

25-
enum ServerError: Error {
25+
public enum ServerError: Error {
2626
case internalServerError_500
2727
case notImplemented_501
2828
case badGateway_502
@@ -31,7 +31,30 @@ public struct Errors {
3131
case unkown(Int)
3232
}
3333

34-
static func errorWith(statusCode: Int) -> Error? {
34+
35+
/// Returns a human readable HTTP Error
36+
/// - Parameter statusCode: HTTP Status Code
37+
/// - Returns: Error?
38+
///
39+
/// ```swift
40+
/// code ~= 300...399 -> Errors.Response.redirect(statusCode)
41+
/// code ~= 400 -> Errors.Response.ClientError.badRequest_400
42+
/// code ~= 401 -> Errors.Response.ClientError.invalidCredentials_401
43+
/// code ~= 403 -> Errors.Response.ClientError.forbidden_403
44+
/// code ~= 404 -> Errors.Response.ClientError.notFound_404
45+
/// code ~= 405 -> Errors.Response.ClientError.notAllowed_405
46+
/// code ~= 409 -> Errors.Response.ClientError.conflict_409
47+
/// code ~= 429 -> Errors.Response.ClientError.tooManyRequests_429
48+
/// code ~= 402, 410...418, 430...499 -> Errors.Response.ClientError.unkown(statusCode)
49+
/// code ~= 500 -> Errors.Response.ServerError.internalServerError_500
50+
/// code ~= 501 -> Errors.Response.ServerError.notImplemented_501
51+
/// code ~= 502 -> Errors.Response.ServerError.badGateway_502
52+
/// code ~= 503 -> Errors.Response.ServerError.unavailable_503
53+
/// code ~= 504 -> Errors.Response.ServerError.timeout_504
54+
/// code ~= 505...599 -> Errors.Response.ServerError.unkown(statusCode)
55+
/// default -> Errors.Response.unknown(statusCode)
56+
///```
57+
public static func errorWith(statusCode: Int) -> Error? {
3558
switch statusCode {
3659
case 200...299: return nil
3760
case 300...399: return Errors.Response.redirect(statusCode)

Sources/HTTPEngine/HTTPEngine+ConvenienceMethods.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ public extension HTTPEngine {
2727
/// ```swift
2828
/// // example validator
2929
/// validator: { $0 == 202 }
30+
/// // Failure throws Errors.Response.unexpectedStatusCode(HTTPURLRequest)
3031
/// ```
31-
public func makeRequestAndParseResponse<Response: Decodable>(
32+
func makeRequestAndParseResponse<Response: Decodable>(
3233
_ decodableResponse: Response.Type,
3334
method: HTTPMethod,
3435
url: String,
@@ -64,8 +65,9 @@ public extension HTTPEngine {
6465
/// ```swift
6566
/// // example validator
6667
/// validator: { $0 == 202 }
68+
/// // Failure throws Errors.Response.unexpectedStatusCode(HTTPURLRequest)
6769
/// ```
68-
public func makeRequestAndParseResponse<Body: Encodable, Response: Decodable>(
70+
func makeRequestAndParseResponse<Body: Encodable, Response: Decodable>(
6971
_ decodableResponse: Response.Type,
7072
method: HTTPMethod,
7173
url: String,
@@ -95,6 +97,7 @@ public extension HTTPEngine {
9597
/// ```swift
9698
/// // example validator
9799
/// validator: { $0 == 202 }
100+
/// // Failure throws Errors.Response.unexpectedStatusCode(HTTPURLRequest)
98101
/// ```
99102
func get<Response: Decodable>(
100103
_ value: Response.Type,
@@ -120,6 +123,7 @@ public extension HTTPEngine {
120123
/// ```swift
121124
/// // example validator
122125
/// validator: { $0 == 202 }
126+
/// // Failure throws Errors.Response.unexpectedStatusCode(HTTPURLRequest)
123127
/// ```
124128
func post<Response: Decodable, Body: Encodable>(
125129
_ value: Response.Type,
@@ -145,6 +149,7 @@ public extension HTTPEngine {
145149
/// ```swift
146150
/// // example validator
147151
/// validator: { $0 == 202 }
152+
/// // Failure throws Errors.Response.unexpectedStatusCode(HTTPURLRequest)
148153
/// ```
149154
func post<Response: Decodable>(
150155
_ value: Response.Type,

Sources/HTTPEngine/HTTPEngine.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import Foundation
22
import Combine
33

4+
/// HTTP Request Header
45
public typealias Header = [String: String]
6+
/// Boolean that represents the success or failure of the ResponseValidationClosure
57
public typealias ValidResponse = Bool
8+
/// A function that takes the HTTPResonse's StatusCode in for comparison. Return `true` if the code is expected or `false` if the function should throw an error.
69
public typealias ResponseValidationClosure = (Int) -> ValidResponse
710

811
public struct HTTPEngine {
@@ -75,6 +78,7 @@ public struct HTTPEngine {
7578
/// ```swift
7679
/// // example validator
7780
/// validator: { $0 == 202 }
81+
/// // Failure throws Errors.Response.unexpectedStatusCode(HTTPURLRequest)
7882
/// ```
7983
public func makeRequest(
8084
method: HTTPMethod,

Sources/HTTPEngine/HTTPMethod.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import Foundation
22

3+
34
public enum HTTPMethod: String, CaseIterable {
45
case post
56
case get
67
case patch
78
case delete
89
case put
10+
case head
11+
case options
12+
case trace
13+
case connect
914
}

Sources/HTTPEngine/Utilities.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import Foundation
22
import Combine
33

44
public extension URLRequest {
5+
6+
/// Combine convenience method
7+
/// - Returns: URLSession.DataTaskPublisher
58
func dataTaskPublisher() -> URLSession.DataTaskPublisher {
69
return URLSession.shared.dataTaskPublisher(for: self)
710
}

0 commit comments

Comments
 (0)