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.

Factory Method

  • Creates a new URLSession-based HTTP client instance with optional configuration.

    Declaration

    Swift

    public static func createClient(
        _ configure: (HttpClientConfig) -> Void = { _ in }
    ) -> URLSessionHttpClient

    Parameters

    configure

    A closure that configures the HTTP client.

    Return Value

    A configured URLSessionHttpClient instance.

  • 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

    config

    The HTTP client configuration.

    session

    The URLSession to use for requests.

    delegate

    Optional URLSessionTaskDelegate for handling redirects and authentication.

  • Creates a new HTTP request instance ready for configuration.

    Declaration

    Swift

    public func request() -> HttpRequest

    Return Value

    A new URLSessionHttpRequest with 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 -> HttpResponse

    Parameters

    request

    The configured HTTP request to execute.

    Return Value

    An HttpResponse containing 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 -> HttpResponse

    Parameters

    builder

    A closure that configures the HTTP request.

    Return Value

    An HttpResponse containing 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

    request

    The URLRequest to be logged.

  • Logs the details of an HTTP response.

    Declaration

    Swift

    public func logResponse(responseData: Data?, response: URLResponse?)

    Parameters

    responseData

    The data returned by the server.

    response

    The URLResponse object containing the response metadata.