URLSessionHttpClient
@objc
public final class URLSessionHttpClient : NSObject, HttpClientProtocol, @unchecked Sendable
URLSession-based implementation of HttpClientProtocol.
Applies request/response interceptors, injects standard headers, and prevents automatic redirects.
This class is thread-safe and can be safely shared across multiple threads and async contexts. All mutable state is confined to the URLSession instance, which is itself thread-safe.
Multiple concurrent requests can safely use the same client instance.
-
Creates a new URLSession-based HTTP client instance with optional configuration.
Declaration
Swift
public static func createClient( _ configure: (HttpClientConfig) -> Void = { _ in } ) -> URLSessionHttpClientParameters
configureA closure that configures the HTTP client.
Return Value
A configured
URLSessionHttpClientinstance. -
Creates a new HTTP client with custom URLSession configuration.
This initializer allows injecting a custom URLSession for testing or advanced configuration. Interceptor arrays are copied from the config at initialization to ensure thread-safe immutability.
Declaration
Swift
public init(config: HttpClientConfig, session: URLSession, delegate: URLSessionTaskDelegate? = nil)Parameters
configThe HTTP client configuration.
sessionThe URLSession to use for requests.
delegateOptional URLSessionTaskDelegate for handling redirects and authentication.
-
Creates a new HTTP request instance ready for configuration.
Declaration
Swift
public func request() -> HttpRequestReturn Value
A new
URLSessionHttpRequestwith standard headers pre-configured. -
request(request:Asynchronous) Executes a pre-configured HTTP request asynchronously.
This method applies all registered request interceptors, performs the HTTP call, and applies response interceptors before returning the result.
Declaration
Swift
public func request(request: HttpRequest) async throws -> HttpResponseParameters
requestThe configured HTTP request to execute.
Return Value
An
HttpResponsecontaining the HTTP response or an error. -
request(builder:Asynchronous) Executes an HTTP request configured via a builder closure.
This convenience method creates a new request, configures it using the provided closure, and executes it in one call.
Example:
let response = try await client.request { req in req.url = "https://api.example.com/users" req.setHeader(name: "Accept", value: "application/json") req.get() }Declaration
Swift
public func request(builder: @escaping @Sendable (HttpRequest) -> Void) async throws -> HttpResponseParameters
builderA closure that configures the HTTP request.
Return Value
An
HttpResponsecontaining the HTTP response or an error. -
Invalidates the URLSession and cancels all pending tasks.
After calling this method, the client cannot be used for new requests. All outstanding tasks will be cancelled.
Declaration
Swift
public func close() -
Logs the details of an HTTP request.
Declaration
Swift
public func logRequest(request: URLRequest?)Parameters
requestThe URLRequest to be logged.
-
Logs the details of an HTTP response.
Declaration
Swift
public func logResponse(responseData: Data?, response: URLResponse?)Parameters
responseDataThe data returned by the server.
responseThe URLResponse object containing the response metadata.
View on GitHub