HTTPClient
public class HTTPClient
HTTPClient class provides API for request execution.
Example:
let client = HTTPClient(eventLoopGroupProvider: .createNew)
client.get(url: "https://swift.org", deadline: .now() + .seconds(1)).whenComplete { result in
switch result {
case .failure(let error):
// process error
case .success(let response):
if let response.status == .ok {
// handle response
} else {
// handle remote error
}
}
}
It is important to close the client instance, for example in a defer statement, after use to cleanly shutdown the underlying NIO EventLoopGroup
:
try client.syncShutdown()
Undocumented
Declaration
Swift
public let eventLoopGroup: EventLoopGroup
Create an
HTTPClient
with specifiedEventLoopGroup
provider and configuration.Declaration
Swift
public convenience init(eventLoopGroupProvider: EventLoopGroupProvider, configuration: Configuration = Configuration())
Parameters
eventLoopGroupProvider
Specify how
EventLoopGroup
will be created.configuration
Client configuration.
Create an
HTTPClient
with specifiedEventLoopGroup
provider and configuration.Declaration
Swift
public required init(eventLoopGroupProvider: EventLoopGroupProvider, configuration: Configuration = Configuration(), backgroundActivityLogger: Logger)
Parameters
eventLoopGroupProvider
Specify how
EventLoopGroup
will be created.configuration
Client configuration.
Shuts down the client and
EventLoopGroup
if it was created by the client.Declaration
Swift
public func syncShutdown() throws
Shuts down the client and event loop gracefully. This function is clearly an outlier in that it uses a completion callback instead of an EventLoopFuture. The reason for that is that NIO’s EventLoopFutures will call back on an event loop. The virtue of this function is to shut the event loop down. To work around that we call back on a DisQueue instead.
Declaration
Swift
public func shutdown(queue: DisQueue = .global(), _ callback: @escaping (Error?) -> Void)
Execute
POST
request using specified URL.Declaration
Parameters
url
Remote URL.
body
Request body.
deadline
Point in time by which the request must complete.
logger
The logger to use for this request.
Execute
request using specified URL.
Declaration
Parameters
url
Remote URL.
body
Request body.
deadline
Point in time by which the request must complete.
logger
The logger to use for this request.
Execute
PUT
request using specified URL.Declaration
Parameters
url
Remote URL.
body
Request body.
deadline
Point in time by which the request must complete.
logger
The logger to use for this request.
Execute arbitrary HTTP request using specified URL.
Declaration
Parameters
method
Request method.
url
Request url.
body
Request body.
deadline
Point in time by which the request must complete.
logger
The logger to use for this request.
Execute arbitrary HTTP+UNIX request to a unix domain socket path, using the specified URL as the request to send to the server.
Declaration
Parameters
method
Request method.
socketPath
The path to the unix domain socket to connect to.
urlPath
The URL path and query that will be sent to the server.
body
Request body.
deadline
Point in time by which the request must complete.
logger
The logger to use for this request.
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.
Declaration
Parameters
method
Request method.
secureSocketPath
The path to the unix domain socket to connect to.
urlPath
The URL path and query that will be sent to the server.
body
Request body.
deadline
Point in time by which the request must complete.
logger
The logger to use for this request.
Execute arbitrary HTTP request using specified URL.
Declaration
Parameters
request
HTTP request to execute.
deadline
Point in time by which the request must complete.
logger
The logger to use for this request.
Execute arbitrary HTTP request using specified URL.
Declaration
Swift
public func execute(request: Request, eventLoop: EventLoopPreference, deadline: NIODeadline? = nil) -> EventLoopFuture<Response>
Parameters
request
HTTP request to execute.
eventLoop
NIO Event Loop preference.
deadline
Point in time by which the request must complete.
Execute arbitrary HTTP request and handle response processing using provided delegate.
Declaration
Swift
public func execute(request: Request, eventLoop eventLoopPreference: EventLoopPreference, deadline: NIODeadline? = nil, logger: Logger?) -> EventLoopFuture<Response>
Parameters
request
HTTP request to execute.
eventLoop
NIO Event Loop preference.
deadline
Point in time by which the request must complete.
logger
The logger to use for this request.
Execute arbitrary HTTP request and handle response processing using provided delegate.
Declaration
Swift
public func execute<Delegate: HTTPClientResponseDelegate>(request: Request, delegate: Delegate, deadline: NIODeadline? = nil) -> Task<Delegate.Response>
Parameters
request
HTTP request to execute.
delegate
Delegate to process response parts.
deadline
Point in time by which the request must complete.
Execute arbitrary HTTP request and handle response processing using provided delegate.
Declaration
Swift
public func execute<Delegate: HTTPClientResponseDelegate>(request: Request, delegate: Delegate, deadline: NIODeadline? = nil, logger: Logger) -> Task<Delegate.Response>
Parameters
request
HTTP request to execute.
delegate
Delegate to process response parts.
deadline
Point in time by which the request must complete.
logger
The logger to use for this request.
Execute arbitrary HTTP request and handle response processing using provided delegate.
Declaration
Swift
public func execute<Delegate: HTTPClientResponseDelegate>(request: Request, delegate: Delegate, eventLoop eventLoopPreference: EventLoopPreference, deadline: NIODeadline? = nil) -> Task<Delegate.Response>
Parameters
request
HTTP request to execute.
delegate
Delegate to process response parts.
eventLoop
NIO Event Loop preference.
deadline
Point in time by which the request must complete.
logger
The logger to use for this request.
Execute arbitrary HTTP request and handle response processing using provided delegate.
Declaration
Swift
public func execute<Delegate: HTTPClientResponseDelegate>(request: Request, delegate: Delegate, eventLoop eventLoopPreference: EventLoopPreference, deadline: NIODeadline? = nil, logger originalLogger: Logger?) -> Task<Delegate.Response>
Parameters
request
HTTP request to execute.
delegate
Delegate to process response parts.
eventLoop
NIO Event Loop preference.
deadline
Point in time by which the request must complete.
See moreHTTPClient
configuration.Declaration
Swift
public struct Configuration
Specifies how the library will treat event loop passed by the user.
See moreDeclaration
Swift
public struct EventLoopPreference
extension HTTPClient.EventLoopPreference: CustomStringConvertible