RequestConvertible
public protocol RequestConvertible : CustomStringConvertible
A type that can convert itself into a Foundation URLRequest
.
Overview
Conforming types provide all the information needed to create a URLRequest
for a Resource
from an API. The
associated Resource
type is the type encoded in the body of the response to the request. Conforming types provide
a ResponseDecoder<Resource>
that is responsible for transforming the raw response data into the associated
Resource
for the request.
Conforming to the RequestConvertible
Protocol
Conforming to the RequestConvertible
protocol requires you specify an associated Resource
type and declare:
- The base URL for the request.
- The endpoint for the request.
- The HTTP method for the request.
- The decoder for the associated resource type.
Default implementations of other properties are provided using the values defined in DefaultValue
. Where it can be
inferred from the Resource
type, a default ResponseDecoder
is also provided.
Note
If your request does not expect a response body, set the associatedResource
type to Void
.
-
The type encoded in the body of the response for
Request
.Declaration
Swift
associatedtype Resource
-
The base URL of the API. Convention dictates that this should not end with a trailing slash.
Declaration
Swift
var baseURL: URL { get }
-
The path to the endpoint of the API. Convention dictates that this should start with a forwards slash.
Declaration
Swift
var endpoint: String { get }
-
The HTTP method to use with the request.
Declaration
Swift
var method: HTTPMethod { get }
-
header
Default implementationHTTP header to be submitted in the request. Defaults to an empty header.
Default Implementation
Declaration
Swift
var header: Header { get }
-
queryItems
Default implementationURL query parameters to be submitted in the request. Defaults to an empty array.
Note
An empty array of query items is interpreted as no query items. The resulting URL will have no query query parameter component.Default Implementation
Declaration
Swift
var queryItems: [URLQueryItem] { get }
-
cachePolicy
Default implementationThe caching policy to specify when converted to a
URLRequest
. Defaults to.useProtocolCachePolicy
.Default Implementation
Declaration
Swift
var cachePolicy: URLRequest.CachePolicy { get }
-
timeoutInterval
Default implementationThe timeout interval to specify when converted to a
URLRequest
. Defaults to60.0
.Default Implementation
Declaration
Swift
var timeoutInterval: TimeInterval { get }
-
bodyProvider
Default implementationA provider for the body of the request. Default is
RequestProvider.none
for an empty body.Default Implementation
Declaration
Swift
var bodyProvider: BodyProvider { get }
-
responseDecoder
Default implementationUndocumented
Default Implementation
Undocumented
Declaration
Swift
var responseDecoder: ResponseDecoder<Resource> { get }
-
authenticationProvider
Default implementationA provider that adds authentication credentials to the header of the request. Defaults to
AuthenticationProvider.none
for no authentication.Note
The authentication provider is run after the body provider.Default Implementation
Declaration
Swift
var authenticationProvider: AuthenticationProvider { get }
-
toURLRequest()
Extension methodConverts the
RequestConvertible
into a FoundationURLRequest
.Throws
Throws:
- An
InvalidRequestURLError
if a validURL
could not be constructed from the request. - Any errors thrown by the request’s
bodyProvider
.
Declaration
Swift
public func toURLRequest() throws -> URLRequest
Return Value
A
URLRequest
constructed from the instance. - An
-
description
Extension methodDeclaration
Swift
public var description: String { get }