Skip to content

Commit 277d075

Browse files
author
Jacob Rakidzich
committed
add get and post
1 parent bdae21e commit 277d075

File tree

2 files changed

+106
-3
lines changed

2 files changed

+106
-3
lines changed

Sources/HTTPEngine/HTTPEngine+ConvenienceMethods.swift

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Combine
33

44
public extension HTTPEngine {
55

6-
/// Makes a request via HTTP
6+
/// Makes a request via HTTP and Decodes the response
77
/// - Parameters:
88
/// - decodableResponse: Decodable - An object that represents the response body
99
/// - method: HTTPMethod - `.get, .put. post` etc.,
@@ -39,7 +39,7 @@ public extension HTTPEngine {
3939
}
4040

4141

42-
/// Makes a request via HTTP
42+
/// Makes a request via HTTP, Encodes the body and Decodes the response
4343
/// - Parameters:
4444
/// - decodableResponse: Decodable - An object that represents the response body
4545
/// - method: HTTPMethod - `.get, .put. post` etc.,
@@ -79,4 +79,81 @@ public extension HTTPEngine {
7979
.decode(type: decodableResponse.self, decoder: JSONDecoder())
8080
.eraseToAnyPublisher()
8181
}
82+
83+
84+
/// Makes a request via HTTP and Decodes the response
85+
/// - Parameters:
86+
/// - decodableResponse: Decodable - An object that represents the response body
87+
/// - urlString: URL domain + path as a string: `"abc.com/some/path"`
88+
/// - 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.
89+
///
90+
/// - Returns: AnyPubliser<Data, Error>
91+
///
92+
/// -- Validation
93+
///
94+
/// By default the validation checks for a 200-299 status code and fails if the code is out of bounds
95+
/// ```swift
96+
/// // example validator
97+
/// validator: { $0 == 202 }
98+
/// ```
99+
func get<Response: Decodable>(
100+
_ value: Response.Type,
101+
url: String,
102+
validator: ResponseValidationClosure? = nil
103+
) -> AnyPublisher<Response, Error> {
104+
makeRequestAndParseResponse(value.self, method: .get, url: url, validator: validator)
105+
}
106+
107+
108+
/// Makes a request via HTTP, Encodes the body and Decodes the response
109+
/// - Parameters:
110+
/// - decodableResponse: Decodable - An object that represents the response body
111+
/// - urlString: URL domain + path as a string: `"abc.com/some/path"`
112+
/// - body: Encodable?: The encodable object that represents body data to send with a request
113+
/// - 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.
114+
///
115+
/// - Returns: AnyPubliser<Data, Error>
116+
///
117+
/// -- Validation
118+
///
119+
/// By default the validation checks for a 200-299 status code and fails if the code is out of bounds
120+
/// ```swift
121+
/// // example validator
122+
/// validator: { $0 == 202 }
123+
/// ```
124+
func post<Response: Decodable, Body: Encodable>(
125+
_ value: Response.Type,
126+
url: String,
127+
body: Body? = nil,
128+
validator: ResponseValidationClosure? = nil
129+
) -> AnyPublisher<Response, Error> {
130+
makeRequestAndParseResponse(value.self, method: .post, url: url, body: body, validator: validator)
131+
}
132+
133+
134+
/// Makes a request via HTTP, Encodes the body and Decodes the response
135+
/// - Parameters:
136+
/// - decodableResponse: Decodable - An object that represents the response body
137+
/// - urlString: URL domain + path as a string: `"abc.com/some/path"`
138+
/// - 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.
139+
///
140+
/// - Returns: AnyPubliser<Data, Error>
141+
///
142+
/// -- Validation
143+
///
144+
/// By default the validation checks for a 200-299 status code and fails if the code is out of bounds
145+
/// ```swift
146+
/// // example validator
147+
/// validator: { $0 == 202 }
148+
/// ```
149+
func post<Response: Decodable>(
150+
_ value: Response.Type,
151+
url: String,
152+
validator: ResponseValidationClosure? = nil
153+
) -> AnyPublisher<Response, Error> {
154+
makeRequestAndParseResponse(value.self, method: .post, url: url, body: nil as NilBody?, validator: validator)
155+
}
156+
}
157+
158+
private struct NilBody: Encodable {
82159
}

Tests/HTTPEngineTests/HTTPEngine+ConvenieceMethodTests.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ final class HTTPEngineConvenienceMethodTests: XCTestCase {
99
("make request and parse response decodes into type", testMakeRequestAndParseResponseDecodesIntoType),
1010
("make request and parse response throws if Decode fails", testMakeRequestAndParseResponseThrowsIfDecodeFails),
1111
("make request and parse response Encodes and Decodes into type", testMakeRequestAndParseResponseEncodesAndDecodesIntoType),
12+
("get succeeds", testGetSucceeds),
13+
("post succeeds", testPostSucceeds),
1214
]
1315

1416
func testMakeRequestAndParseResponseDecodesIntoType() {
@@ -43,7 +45,31 @@ final class HTTPEngineConvenienceMethodTests: XCTestCase {
4345
.assertResult(test: self) {
4446
XCTAssertEqual($0.key, "value")
4547
}
46-
48+
}
49+
50+
func testGetSucceeds() {
51+
stub(condition: isHost("google.com") && isMethodGET()) { _ in
52+
HTTPStubsResponse(jsonObject: ["key": "value"], statusCode: 200, headers: nil)
53+
}
54+
55+
HTTPEngine()
56+
.get(TestResponseBody.self, url: "https://google.com")
57+
.assertResult(test: self) {
58+
XCTAssertEqual($0.key, "value")
59+
60+
}
61+
}
62+
63+
func testPostSucceeds() {
64+
stub(condition: isHost("google.com") && isMethodPOST()) { _ in
65+
HTTPStubsResponse(jsonObject: ["key": "value"], statusCode: 200, headers: nil)
66+
}
67+
68+
HTTPEngine()
69+
.post(TestResponseBody.self, url: "https://google.com")
70+
.assertResult(test: self) {
71+
XCTAssertEqual($0.key, "value")
72+
}
4773
}
4874
}
4975

0 commit comments

Comments
 (0)