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 DispatchQueue instead.
Declaration
Swift
public func shutdown(queue: DispatchQueue = .global(), _ callback: @escaping (Error?) -> Void)
-
Execute
GET
request using specified URL.Declaration
Swift
public func get(url: String, deadline: NIODeadline? = nil) -> EventLoopFuture<Response>
Parameters
url
Remote URL.
deadline
Point in time by which the request must complete.
-
Execute
GET
request using specified URL.Declaration
Swift
public func get(url: String, deadline: NIODeadline? = nil, logger: Logger) -> EventLoopFuture<Response>
Parameters
url
Remote URL.
deadline
Point in time by which the request must complete.
logger
The logger to use for this request.
-
Execute
POST
request using specified URL.Declaration
Parameters
url
Remote URL.
body
Request body.
deadline
Point in time by which the request must complete.
-
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
PATCH
request using specified URL.Declaration
Parameters
url
Remote URL.
body
Request body.
deadline
Point in time by which the request must complete.
-
Execute
PATCH
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.
-
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
DELETE
request using specified URL.Declaration
Swift
public func delete(url: String, deadline: NIODeadline? = nil) -> EventLoopFuture<Response>
Parameters
url
Remote URL.
deadline
The time when the request must have been completed by.
-
Execute
DELETE
request using specified URL.Declaration
Swift
public func delete(url: String, deadline: NIODeadline? = nil, logger: Logger) -> EventLoopFuture<Response>
Parameters
url
Remote URL.
deadline
The time when the request must have been completed by.
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.
-
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
See moreEventLoopGroup
will be created and establishes lifecycle ownership.Declaration
Swift
public enum EventLoopGroupProvider
-
Specifies how the library will treat event loop passed by the user.
See moreDeclaration
Swift
public struct EventLoopPreference
extension HTTPClient.EventLoopPreference: CustomStringConvertible
-
Specifies decompression settings.
See moreDeclaration
Swift
public enum Decompression
-
A representation of an HTTP cookie.
See moreDeclaration
Swift
public struct Cookie
-
Represent HTTP response.
See moreDeclaration
Swift
public struct Response
-
Represent request body.
See moreDeclaration
Swift
public struct Body
-
Represent HTTP request.
See moreDeclaration
Swift
public struct Request
-
HTTP authentication
See moreDeclaration
Swift
public struct Authorization : Hashable