ServiceRequest

public protocol ServiceRequest

A protocol for requests to be made on a Service.

Use this protocol to define requests, which can be any of the HTTPRequestMethod types. The ServiceRequest.httpMethod is the only property that you must provide a value for- all others provide a default implementation.

Requests can be customized with:

To make a request, simply call the request() method on the Service. There are three versions of this method:

  1. A closure based method which executes the closure on completion of the request - Service.request(_:session:autoResumeTask:completion:).
  2. An asynchronous throwing method using Swift concurrency (Available when using Swift 5.5 or greater on Linux, iOS 13+, watchOS 6+, macOS10.15+, tvOS 13+) - Service.request(_:session:).
  3. A method which returns a Combine publisher (available on iOS 13+, watchOS 6+, tvOS 13+, macOS 10.15) - Service.requestPublisher(_:session:).

Properties

  • endpoint Default implementation

    The endpoint this request is associated with

    See also

    Endpoint.path

    Default Implementation

    Declaration

    Swift

    var endpoint: Endpoint.Type? { get }
  • The type of request

    Declaration

    Swift

    var httpMethod: HTTPRequestMethod { get }
  • pathComponents Default implementation

    Path components of the request will be appended to the base URL.

    Array elements are separated by a / in the final request URL. Defaults to an empty array (no parameters).

    Note

    If an endpoint is specified, the Endpoint.path value will be automatically set as the first path item before any values specified here. In this case, do not add the endpoint path to the pathComponents or it will be duplicated.

    Default Implementation

    No path components (only the Service.baseURL will be used).

    Declaration

    Swift

    var pathComponents: [String] { get }
  • queryParameters Default implementation

    Query parameters of the request. Default is an empty array (no parameters)

    Default Implementation

    No query parameters

    Declaration

    Swift

    var queryParameters: [URLQueryItem] { get }
  • headers Default implementation

    HTTP headers of the request. Default is an empty array (no headers)

    Note

    Do not set Content-Type header field values here; instead use the contentType property. The contents of that property will be added to the URLRequest.allHTTPHeaderFields property.

    Default Implementation

    An empty dictionary

    Declaration

    Swift

    var headers: [String : String] { get }
  • contentType Default implementation

    The content type of the request. The default is application/json.

    Note

    This value is added as an HTTP header on the URLRequest.

    Default Implementation

    Returns RequestContentType.applicationJSON

    Declaration

    Swift

    var contentType: RequestContentType? { get }
  • body Default implementation

    Body of the request

    Default Implementation

    Returns nil.

    Declaration

    Swift

    var body: Data? { get }