Skip to content

Commit 662cf46

Browse files
Renamed execute(url:) methods such that the HTTP method is the first argument in the parameter list
Motivation: If these are intended to be general methods for building simple requests, then it makes sense to have the method be the first parameter in the list. Modifications: Moved the `method: HTTPMethod` parameter to the front of the list for all `execute([...] url: [...])` methods, and made it default to .GET. I also changed the url parameter to be `urlPath` for the two socketPath based execute methods. Result: A cleaner public interface for users of the API.
1 parent 9d621ac commit 662cf46

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,13 @@ httpClient.execute(request: request, delegate: delegate).futureResult.whenSucces
162162
Connecting to servers bound to socket paths is easy:
163163
```swift
164164
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
165-
httpClient.execute(socketPath: "/tmp/myServer.socket", url: "/path/to/resource", method: .GET).whenComplete (...)
165+
httpClient.execute(.GET, socketPath: "/tmp/myServer.socket", urlPath: "/path/to/resource").whenComplete (...)
166166
```
167167

168168
Connecting over TLS to a unix domain socket path is possible as well:
169169
```swift
170170
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
171-
httpClient.execute(secureSocketPath: "/tmp/myServer.socket", url: "/path/to/resource", body: .string("hello"), method: .POST).whenComplete (...)
171+
httpClient.execute(.POST, secureSocketPath: "/tmp/myServer.socket", urlPath: "/path/to/resource", body: .string("hello")).whenComplete (...)
172172
```
173173

174174
Direct URLs can easily be contructed to be executed in other scenarios:

Diff for: Sources/AsyncHTTPClient/HTTPClient.swift

+15-15
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public class HTTPClient {
194194
/// - url: Remote URL.
195195
/// - deadline: Point in time by which the request must complete.
196196
public func get(url: String, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
197-
return self.execute(url: url, method: .GET, deadline: deadline)
197+
return self.execute(.GET, url: url, deadline: deadline)
198198
}
199199

200200
/// Execute `POST` request using specified URL.
@@ -204,7 +204,7 @@ public class HTTPClient {
204204
/// - body: Request body.
205205
/// - deadline: Point in time by which the request must complete.
206206
public func post(url: String, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
207-
return self.execute(url: url, method: .POST, body: body, deadline: deadline)
207+
return self.execute(.POST, url: url, body: body, deadline: deadline)
208208
}
209209

210210
/// Execute `PATCH` request using specified URL.
@@ -214,7 +214,7 @@ public class HTTPClient {
214214
/// - body: Request body.
215215
/// - deadline: Point in time by which the request must complete.
216216
public func patch(url: String, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
217-
return self.execute(url: url, method: .PATCH, body: body, deadline: deadline)
217+
return self.execute(.PATCH, url: url, body: body, deadline: deadline)
218218
}
219219

220220
/// Execute `PUT` request using specified URL.
@@ -224,7 +224,7 @@ public class HTTPClient {
224224
/// - body: Request body.
225225
/// - deadline: Point in time by which the request must complete.
226226
public func put(url: String, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
227-
return self.execute(url: url, method: .PUT, body: body, deadline: deadline)
227+
return self.execute(.PUT, url: url, body: body, deadline: deadline)
228228
}
229229

230230
/// Execute `DELETE` request using specified URL.
@@ -233,17 +233,17 @@ public class HTTPClient {
233233
/// - url: Remote URL.
234234
/// - deadline: The time when the request must have been completed by.
235235
public func delete(url: String, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
236-
return self.execute(url: url, method: .DELETE, deadline: deadline)
236+
return self.execute(.DELETE, url: url, deadline: deadline)
237237
}
238238

239239
/// Execute arbitrary HTTP request using specified URL.
240240
///
241241
/// - parameters:
242-
/// - url: Request url.
243242
/// - method: Request method.
243+
/// - url: Request url.
244244
/// - body: Request body.
245245
/// - deadline: Point in time by which the request must complete.
246-
public func execute(url: String, method: HTTPMethod, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
246+
public func execute(_ method: HTTPMethod = .GET, url: String, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
247247
do {
248248
let request = try Request(url: url, method: method, body: body)
249249
return self.execute(request: request, deadline: deadline)
@@ -255,14 +255,14 @@ public class HTTPClient {
255255
/// Execute arbitrary HTTP+UNIX request to a unix domain socket path, using the specified URL as the request to send to the server.
256256
///
257257
/// - parameters:
258-
/// - socketPath: The path to the unix domain socket to connect to.
259-
/// - url: The URL path and query that will be sent to the server.
260258
/// - method: Request method.
259+
/// - socketPath: The path to the unix domain socket to connect to.
260+
/// - urlPath: The URL path and query that will be sent to the server.
261261
/// - body: Request body.
262262
/// - deadline: Point in time by which the request must complete.
263-
public func execute(socketPath: String, url: String, method: HTTPMethod, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
263+
public func execute(_ method: HTTPMethod = .GET, socketPath: String, urlPath: String, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
264264
do {
265-
guard let url = URL(httpURLWithSocketPath: socketPath, uri: url) else {
265+
guard let url = URL(httpURLWithSocketPath: socketPath, uri: urlPath) else {
266266
throw HTTPClientError.invalidURL
267267
}
268268
let request = try Request(url: url, method: method, body: body)
@@ -275,14 +275,14 @@ public class HTTPClient {
275275
/// Execute arbitrary HTTPS+UNIX request to a unix domain socket path over TLS, using the specified URL as the request to send to the server.
276276
///
277277
/// - parameters:
278-
/// - secureSocketPath: The path to the unix domain socket to connect to.
279-
/// - url: The URL path and query that will be sent to the server.
280278
/// - method: Request method.
279+
/// - secureSocketPath: The path to the unix domain socket to connect to.
280+
/// - urlPath: The URL path and query that will be sent to the server.
281281
/// - body: Request body.
282282
/// - deadline: Point in time by which the request must complete.
283-
public func execute(secureSocketPath: String, url: String, method: HTTPMethod, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
283+
public func execute(_ method: HTTPMethod = .GET, secureSocketPath: String, urlPath: String, body: Body? = nil, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
284284
do {
285-
guard let url = URL(httpsURLWithSocketPath: secureSocketPath, uri: url) else {
285+
guard let url = URL(httpsURLWithSocketPath: secureSocketPath, uri: urlPath) else {
286286
throw HTTPClientError.invalidURL
287287
}
288288
let request = try Request(url: url, method: method, body: body)

Diff for: Tests/AsyncHTTPClientTests/HTTPClientTests.swift

+11-5
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,10 @@ class HTTPClientTests: XCTestCase {
211211
try self.defaultClient.put(url: self.defaultHTTPBinURLPrefix + "echo-method").wait().headers[canonicalForm: "X-Method-Used"]))
212212
XCTAssertNoThrow(XCTAssertEqual(["DELETE"[...]],
213213
try self.defaultClient.delete(url: self.defaultHTTPBinURLPrefix + "echo-method").wait().headers[canonicalForm: "X-Method-Used"]))
214+
XCTAssertNoThrow(XCTAssertEqual(["GET"[...]],
215+
try self.defaultClient.execute(url: self.defaultHTTPBinURLPrefix + "echo-method").wait().headers[canonicalForm: "X-Method-Used"]))
214216
XCTAssertNoThrow(XCTAssertEqual(["CHECKOUT"[...]],
215-
try self.defaultClient.execute(url: self.defaultHTTPBinURLPrefix + "echo-method", method: .CHECKOUT).wait().headers[canonicalForm: "X-Method-Used"]))
217+
try self.defaultClient.execute(.CHECKOUT, url: self.defaultHTTPBinURLPrefix + "echo-method").wait().headers[canonicalForm: "X-Method-Used"]))
216218
}
217219

218220
func testConvenienceExecuteMethodsOverSocket() throws {
@@ -223,9 +225,11 @@ class HTTPClientTests: XCTestCase {
223225
}
224226

225227
XCTAssertNoThrow(XCTAssertEqual(["GET"[...]],
226-
try self.defaultClient.execute(socketPath: path, url: "echo-method", method: .GET).wait().headers[canonicalForm: "X-Method-Used"]))
228+
try self.defaultClient.execute(socketPath: path, urlPath: "echo-method").wait().headers[canonicalForm: "X-Method-Used"]))
229+
XCTAssertNoThrow(XCTAssertEqual(["GET"[...]],
230+
try self.defaultClient.execute(.GET, socketPath: path, urlPath: "echo-method").wait().headers[canonicalForm: "X-Method-Used"]))
227231
XCTAssertNoThrow(XCTAssertEqual(["POST"[...]],
228-
try self.defaultClient.execute(socketPath: path, url: "echo-method", method: .POST).wait().headers[canonicalForm: "X-Method-Used"]))
232+
try self.defaultClient.execute(.POST, socketPath: path, urlPath: "echo-method").wait().headers[canonicalForm: "X-Method-Used"]))
229233
})
230234
}
231235

@@ -240,9 +244,11 @@ class HTTPClientTests: XCTestCase {
240244
}
241245

242246
XCTAssertNoThrow(XCTAssertEqual(["GET"[...]],
243-
try localClient.execute(secureSocketPath: path, url: "echo-method", method: .GET).wait().headers[canonicalForm: "X-Method-Used"]))
247+
try localClient.execute(secureSocketPath: path, urlPath: "echo-method").wait().headers[canonicalForm: "X-Method-Used"]))
248+
XCTAssertNoThrow(XCTAssertEqual(["GET"[...]],
249+
try localClient.execute(.GET, secureSocketPath: path, urlPath: "echo-method").wait().headers[canonicalForm: "X-Method-Used"]))
244250
XCTAssertNoThrow(XCTAssertEqual(["POST"[...]],
245-
try localClient.execute(secureSocketPath: path, url: "echo-method", method: .POST).wait().headers[canonicalForm: "X-Method-Used"]))
251+
try localClient.execute(.POST, secureSocketPath: path, urlPath: "echo-method").wait().headers[canonicalForm: "X-Method-Used"]))
246252
})
247253
}
248254

0 commit comments

Comments
 (0)