HttpClientProtocol

public protocol HttpClientProtocol : Sendable

Protocol defining the contract for HTTP client implementations.

The HttpClientProtocol provides a high-level interface for making HTTP requests with support for configuration, interceptors, and async/await patterns.

  • Creates a new HTTP request instance.

    Declaration

    Swift

    func request() -> HttpRequest

    Return Value

    A new HttpRequest ready for configuration.

  • request(request:) Asynchronous

    Executes a pre-configured HTTP request.

    Declaration

    Swift

    func request(request: HttpRequest) async throws -> HttpResponse

    Parameters

    request

    The configured HTTP request to execute.

    Return Value

    An HttpResponse instance

  • request(builder:) Asynchronous

    Executes an HTTP request configured via a builder closure.

    Example:

    let response = try await client.request { req in
        req.url = URL(string: "https://api.example.com/users")
        req.setHeader(name: "Accept", value: "application/json")
        req.get()
    }
    

    Declaration

    Swift

    func request(builder: @escaping @Sendable (HttpRequest) -> Void) async throws -> HttpResponse

    Parameters

    builder

    A closure that configures the request.

    Return Value

    An HttpResponse instance

  • Closes the client and releases resources.

    After calling this method, the client should not be used for new requests.

    Declaration

    Swift

    func close()