RequestProviding

public protocol RequestProviding

A type that provides an interface to create requests to an API.

Overview

For each API, create a type that conforms to RequestProviding and implement the base URL of the API. Using this type you can create Requests for the different endpoints of the API using the construction methods provided by the protocol.

You can optionally implement the request(to:using:) method to customize the default request provided by the other construction methods.

  • The base url of all requests to the API. Convention dictates that this URL should not have a trailing slash.

    Declaration

    Swift

    var baseURL: URL { get }
  • request(to:using:) Default implementation

    Constructs a new request to an endpoint of the API using a specific HTTP method.

    Default Implementation

    Declaration

    Swift

    func request(to endpoint: String, using method: HTTPMethod) -> Request<Self, Void>

    Parameters

    endpoint

    The endpoint for the request.

    method

    The HTTP method for the request.

    Return Value

    A new Request to the endpoint using the method.

  • get(_:from:) Extension method

    Constructs a GET request for a resource at an endpoint of the API.

    Declaration

    Swift

    public func get<NewResource>(_ resourceDecoder: ResponseDecoder<NewResource>, from endpoint: String)
        -> Request<Self, NewResource>

    Parameters

    resourceDecoder

    A decoder for the resource at the endpoint.

    endpoint

    The endpoint to retrieve the resource from.

    Return Value

    A new request for the resource at the endpoint

  • post(_:to:) Extension method

    Constructs a POST request sending a body of data to an endpoint of the API.

    Declaration

    Swift

    public func post(_ body: BodyProvider, to endpoint: String) -> Request<Self, Void>

    Parameters

    body

    The data to send in the request’s body.

    endpoint

    The endpoint to send the data to.

    Return Value

    A new request to send the data to the endpoint.

  • put(_:to:) Extension method

    Constructs a PUT request sending a body of data to an endpoint of the API.

    Declaration

    Swift

    public func put(_ body: BodyProvider, to endpoint: String) -> Request<Self, Void>

    Parameters

    body

    The data to send in the request’s body.

    endpoint

    The endpoint to send the data to.

    Return Value

    A new request to send the data to the endpoint.

  • patch(_:with:) Extension method

    Constructs a PATCH request sending a body of data to an endpoint of the API.

    Declaration

    Swift

    public func patch(_ endpoint: String, with body: BodyProvider) -> Request<Self, Void>

    Parameters

    endpoint

    The endpoint to send the data to.

    body

    The data to send in the request’s body.

    Return Value

    A new request to send the data to the endpoint.

  • delete(_:) Extension method

    Constructs a DELETE request for a resource at an endpoint of the API.

    Declaration

    Swift

    public func delete(_ endpoint: String) -> Request<Self, Void>

    Parameters

    endpoint

    The endpoint of the resource to delete.

    Return Value

    A new request to delete a resource.

  • head(_:) Extension method

    Constructs a HEAD request for the headers of a resource at an endpoint of the API.

    Declaration

    Swift

    public func head(_ endpoint: String) -> Request<Self, Void>

    Parameters

    endpoint

    The endpoint of the resource to retrieve the headers of.

    Return Value

    A new request to retrieve the headers of a resource.