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 } -
headerDefault implementationHTTP header to be submitted in the request. Defaults to an empty header.
Default Implementation
Declaration
Swift
var header: Header { get } -
queryItemsDefault 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 } -
cachePolicyDefault implementationThe caching policy to specify when converted to a
URLRequest. Defaults to.useProtocolCachePolicy.Default Implementation
Declaration
Swift
var cachePolicy: URLRequest.CachePolicy { get } -
timeoutIntervalDefault implementationThe timeout interval to specify when converted to a
URLRequest. Defaults to60.0.Default Implementation
Declaration
Swift
var timeoutInterval: TimeInterval { get } -
bodyProviderDefault implementationA provider for the body of the request. Default is
RequestProvider.nonefor an empty body.Default Implementation
Declaration
Swift
var bodyProvider: BodyProvider { get } -
responseDecoderDefault implementationUndocumented
Default Implementation
Undocumented
Declaration
Swift
var responseDecoder: ResponseDecoder<Resource> { get } -
authenticationProviderDefault implementationA provider that adds authentication credentials to the header of the request. Defaults to
AuthenticationProvider.nonefor no authentication.Note
The authentication provider is run after the body provider.Default Implementation
Declaration
Swift
var authenticationProvider: AuthenticationProvider { get }
-
toURLRequest()Extension methodConverts the
RequestConvertibleinto a FoundationURLRequest.Throws
Throws:
- An
InvalidRequestURLErrorif a validURLcould not be constructed from the request. - Any errors thrown by the request’s
bodyProvider.
Declaration
Swift
public func toURLRequest() throws -> URLRequestReturn Value
A
URLRequestconstructed from the instance. - An
-
descriptionExtension methodDeclaration
Swift
public var description: String { get }
View on GitHub
RequestConvertible Protocol Reference