Skip to content

Commit cf4ff26

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 10b0e01 commit cf4ff26

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
@@ -237,7 +237,7 @@ public class HTTPClient {
237237
/// - deadline: Point in time by which the request must complete.
238238
/// - logger: The logger to use for this request.
239239
public func get(url: String, deadline: NIODeadline? = nil, logger: Logger) -> EventLoopFuture<Response> {
240-
return self.execute(url: url, method: .GET, deadline: deadline, logger: logger)
240+
return self.execute(.GET, url: url, deadline: deadline, logger: logger)
241241
}
242242

243243
/// Execute `POST` request using specified URL.
@@ -258,7 +258,7 @@ public class HTTPClient {
258258
/// - deadline: Point in time by which the request must complete.
259259
/// - logger: The logger to use for this request.
260260
public func post(url: String, body: Body? = nil, deadline: NIODeadline? = nil, logger: Logger) -> EventLoopFuture<Response> {
261-
return self.execute(url: url, method: .POST, body: body, deadline: deadline, logger: logger)
261+
return self.execute(.POST, url: url, body: body, deadline: deadline, logger: logger)
262262
}
263263

264264
/// Execute `PATCH` request using specified URL.
@@ -279,7 +279,7 @@ public class HTTPClient {
279279
/// - deadline: Point in time by which the request must complete.
280280
/// - logger: The logger to use for this request.
281281
public func patch(url: String, body: Body? = nil, deadline: NIODeadline? = nil, logger: Logger) -> EventLoopFuture<Response> {
282-
return self.execute(url: url, method: .PATCH, body: body, deadline: deadline, logger: logger)
282+
return self.execute(.PATCH, url: url, body: body, deadline: deadline, logger: logger)
283283
}
284284

285285
/// Execute `PUT` request using specified URL.
@@ -300,7 +300,7 @@ public class HTTPClient {
300300
/// - deadline: Point in time by which the request must complete.
301301
/// - logger: The logger to use for this request.
302302
public func put(url: String, body: Body? = nil, deadline: NIODeadline? = nil, logger: Logger) -> EventLoopFuture<Response> {
303-
return self.execute(url: url, method: .PUT, body: body, deadline: deadline, logger: logger)
303+
return self.execute(.PUT, url: url, body: body, deadline: deadline, logger: logger)
304304
}
305305

306306
/// Execute `DELETE` request using specified URL.
@@ -319,18 +319,18 @@ public class HTTPClient {
319319
/// - deadline: The time when the request must have been completed by.
320320
/// - logger: The logger to use for this request.
321321
public func delete(url: String, deadline: NIODeadline? = nil, logger: Logger) -> EventLoopFuture<Response> {
322-
return self.execute(url: url, method: .DELETE, deadline: deadline, logger: logger)
322+
return self.execute(.DELETE, url: url, deadline: deadline, logger: logger)
323323
}
324324

325325
/// Execute arbitrary HTTP request using specified URL.
326326
///
327327
/// - parameters:
328-
/// - url: Request url.
329328
/// - method: Request method.
329+
/// - url: Request url.
330330
/// - body: Request body.
331331
/// - deadline: Point in time by which the request must complete.
332332
/// - logger: The logger to use for this request.
333-
public func execute(url: String, method: HTTPMethod, body: Body? = nil, deadline: NIODeadline? = nil, logger: Logger? = nil) -> EventLoopFuture<Response> {
333+
public func execute(_ method: HTTPMethod = .GET, url: String, body: Body? = nil, deadline: NIODeadline? = nil, logger: Logger? = nil) -> EventLoopFuture<Response> {
334334
do {
335335
let request = try Request(url: url, method: method, body: body)
336336
return self.execute(request: request, deadline: deadline, logger: logger ?? HTTPClient.loggingDisabled)
@@ -342,15 +342,15 @@ public class HTTPClient {
342342
/// Execute arbitrary HTTP+UNIX request to a unix domain socket path, using the specified URL as the request to send to the server.
343343
///
344344
/// - parameters:
345-
/// - socketPath: The path to the unix domain socket to connect to.
346-
/// - url: The URL path and query that will be sent to the server.
347345
/// - method: Request method.
346+
/// - socketPath: The path to the unix domain socket to connect to.
347+
/// - urlPath: The URL path and query that will be sent to the server.
348348
/// - body: Request body.
349349
/// - deadline: Point in time by which the request must complete.
350350
/// - logger: The logger to use for this request.
351-
public func execute(socketPath: String, url: String, method: HTTPMethod, body: Body? = nil, deadline: NIODeadline? = nil, logger: Logger? = nil) -> EventLoopFuture<Response> {
351+
public func execute(_ method: HTTPMethod = .GET, socketPath: String, urlPath: String, body: Body? = nil, deadline: NIODeadline? = nil, logger: Logger? = nil) -> EventLoopFuture<Response> {
352352
do {
353-
guard let url = URL(httpURLWithSocketPath: socketPath, uri: url) else {
353+
guard let url = URL(httpURLWithSocketPath: socketPath, uri: urlPath) else {
354354
throw HTTPClientError.invalidURL
355355
}
356356
let request = try Request(url: url, method: method, body: body)
@@ -363,15 +363,15 @@ public class HTTPClient {
363363
/// 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.
364364
///
365365
/// - parameters:
366-
/// - secureSocketPath: The path to the unix domain socket to connect to.
367-
/// - url: The URL path and query that will be sent to the server.
368366
/// - method: Request method.
367+
/// - secureSocketPath: The path to the unix domain socket to connect to.
368+
/// - urlPath: The URL path and query that will be sent to the server.
369369
/// - body: Request body.
370370
/// - deadline: Point in time by which the request must complete.
371371
/// - logger: The logger to use for this request.
372-
public func execute(secureSocketPath: String, url: String, method: HTTPMethod, body: Body? = nil, deadline: NIODeadline? = nil, logger: Logger? = nil) -> EventLoopFuture<Response> {
372+
public func execute(_ method: HTTPMethod = .GET, secureSocketPath: String, urlPath: String, body: Body? = nil, deadline: NIODeadline? = nil, logger: Logger? = nil) -> EventLoopFuture<Response> {
373373
do {
374-
guard let url = URL(httpsURLWithSocketPath: secureSocketPath, uri: url) else {
374+
guard let url = URL(httpsURLWithSocketPath: secureSocketPath, uri: urlPath) else {
375375
throw HTTPClientError.invalidURL
376376
}
377377
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
@@ -225,8 +225,10 @@ class HTTPClientTests: XCTestCase {
225225
try self.defaultClient.put(url: self.defaultHTTPBinURLPrefix + "echo-method").wait().headers[canonicalForm: "X-Method-Used"]))
226226
XCTAssertNoThrow(XCTAssertEqual(["DELETE"[...]],
227227
try self.defaultClient.delete(url: self.defaultHTTPBinURLPrefix + "echo-method").wait().headers[canonicalForm: "X-Method-Used"]))
228+
XCTAssertNoThrow(XCTAssertEqual(["GET"[...]],
229+
try self.defaultClient.execute(url: self.defaultHTTPBinURLPrefix + "echo-method").wait().headers[canonicalForm: "X-Method-Used"]))
228230
XCTAssertNoThrow(XCTAssertEqual(["CHECKOUT"[...]],
229-
try self.defaultClient.execute(url: self.defaultHTTPBinURLPrefix + "echo-method", method: .CHECKOUT).wait().headers[canonicalForm: "X-Method-Used"]))
231+
try self.defaultClient.execute(.CHECKOUT, url: self.defaultHTTPBinURLPrefix + "echo-method").wait().headers[canonicalForm: "X-Method-Used"]))
230232
}
231233

232234
func testConvenienceExecuteMethodsOverSocket() throws {
@@ -237,9 +239,11 @@ class HTTPClientTests: XCTestCase {
237239
}
238240

239241
XCTAssertNoThrow(XCTAssertEqual(["GET"[...]],
240-
try self.defaultClient.execute(socketPath: path, url: "echo-method", method: .GET).wait().headers[canonicalForm: "X-Method-Used"]))
242+
try self.defaultClient.execute(socketPath: path, urlPath: "echo-method").wait().headers[canonicalForm: "X-Method-Used"]))
243+
XCTAssertNoThrow(XCTAssertEqual(["GET"[...]],
244+
try self.defaultClient.execute(.GET, socketPath: path, urlPath: "echo-method").wait().headers[canonicalForm: "X-Method-Used"]))
241245
XCTAssertNoThrow(XCTAssertEqual(["POST"[...]],
242-
try self.defaultClient.execute(socketPath: path, url: "echo-method", method: .POST).wait().headers[canonicalForm: "X-Method-Used"]))
246+
try self.defaultClient.execute(.POST, socketPath: path, urlPath: "echo-method").wait().headers[canonicalForm: "X-Method-Used"]))
243247
})
244248
}
245249

@@ -254,9 +258,11 @@ class HTTPClientTests: XCTestCase {
254258
}
255259

256260
XCTAssertNoThrow(XCTAssertEqual(["GET"[...]],
257-
try localClient.execute(secureSocketPath: path, url: "echo-method", method: .GET).wait().headers[canonicalForm: "X-Method-Used"]))
261+
try localClient.execute(secureSocketPath: path, urlPath: "echo-method").wait().headers[canonicalForm: "X-Method-Used"]))
262+
XCTAssertNoThrow(XCTAssertEqual(["GET"[...]],
263+
try localClient.execute(.GET, secureSocketPath: path, urlPath: "echo-method").wait().headers[canonicalForm: "X-Method-Used"]))
258264
XCTAssertNoThrow(XCTAssertEqual(["POST"[...]],
259-
try localClient.execute(secureSocketPath: path, url: "echo-method", method: .POST).wait().headers[canonicalForm: "X-Method-Used"]))
265+
try localClient.execute(.POST, secureSocketPath: path, urlPath: "echo-method").wait().headers[canonicalForm: "X-Method-Used"]))
260266
})
261267
}
262268

0 commit comments

Comments
 (0)