Structures
The following structures are available globally.
-
A type that produces the body of a request and updates the header of a request accordingly.
A
BodyProvider
implements a function that takes aninout
Header
and produces aRequestBody
. In thebody(updating:)
method, the provider should construct theRequestBody
and update theContent-Type
of the providedHeader
. The provider can also set any other relevant fields in the header.Several predefined
BodyProvider
s are provided for text bodies and JSON bodies.Creating a
BodyProvider
The
body(updating:)
method is defined when theBodyProvider
is initialised as a closure. The method is somewhat odd because it doesn’t take any data to convert to aRequestBody
as input. In yourBodyProvider
s you should capture the input data in the closure provided at initialization. This means that mostBodyProvider
s will be provided by factory functions that take the input data and construct a new body provider using it.When implementing a provider, ensure that updates to the provided
See moreheader
are only applied when any throwing functions have succeeded. Otherwise you may end up in the situation where the header of a request is updated but its body has not been created because an error was thrown during creation.Declaration
Swift
public struct BodyProvider
-
A
See moreString
wrapper that implements case insensitive comparison, sorting and hashing methods.Declaration
Swift
public struct CaseInsensitiveString : CustomStringConvertible
-
A header field in a HTTP request. A field consists of a key-value pair mapping
name
ontovalue
.Note
As HTTP headers are case insensitive, the implementations ofHashable
andEquatable
take this into consideration and convert all header names to lowercase before comparing them.Declaration
Swift
public struct Field
-
A method (AKA verb) to be sent in a HTTP request. The standard (HTTP/1.1) headers are implemented as static properties. New methods can be added in an extension on the
HTTPMethod
type.Pattern Matching
See moreHTTPMethod
provides an overload of the~=
operator so methods can easily be pattern matched on in aswitch
statement.Declaration
Swift
public struct HTTPMethod : Hashable, RawRepresentable
-
A header in a HTTP request. A header consists of a series of fields containing a key and a value. Keys are represented by the
Field.Name
type while values are simplyString
s.Note
AHeader
instance does not preserve the order of fields added to it.Declaration
Swift
public struct Header : Hashable
-
A media type (MIME type) used to identify content.
Media types consist of a top level
type
asubtype
and zero or more parameters (key-value pairs). The implementation in Requests is based on RFC 2045 and RFC 6838. The key take-aways from these RFCs are:- The
type
andsubtype
of a media type are case-insensitive. - The key of a parameter is case-insensitive but the value is case-sensitive.
- The parameters of a media type do not effect its value.
The implications of these for
MediaType
are:- The implementations of
Equatable
andHashable
are case-insensitive. - Ditto for the parameters of a
MediaType
. - The
parameters
of aMediaType
are not used in theEquatable
andHashable
implementation ofMediaType
.
Defining New Media Types
Several common media types are defined as static properties on
MediaType
. You can add new types in an extension ofMediaType
. Define the new subtype in an extension ofMediaType.SubType
and the new media type in an extension ofMediaType
For media types with required parameters (e.g.,
multipart/form-data
), define the media type as a static function of its required parameters. For example,MediaType.formData
is defined as:static let formData: (String) -> MediaType = { boundary in MediaType(type: .multipart, subtype: .formData, parameters: ["boundary": boundary]) }
which will produce a new
See moreMediaType
with theboundary
parameter set to the providedString
.Declaration
Swift
public struct MediaType : CustomStringConvertible
- The
-
A
RequestProviding
type to a single API host.Use an
See moreAnonymousRequestProvider
for one-off API requests or when you do not care about theAPI
parameter of theRequest
type.Declaration
Swift
public struct AnonymousRequestProvider : RequestProviding
-
A request for a resource from an API.
Overview
The
Request
structure is a concrete implementation of aRequestConvertible
that provides a chainable builder-like interface to constructing requests. You rarely instantiate aRequest
directly. Instead, requests to a specific API are created by aRequestProviding
type and are customised using the builder functions onRequest
.The
Request
structure is generic over theAPI
type and theResource
type. TheAPI
parameter is immutable and can be used to constrain extensions onRequest
to a specific API. TheResource
parameter ismutable
in the sense that it can be changed using thereceiving(_:)
builder function.One-off Requests
A convenience initializer
init(method:baseURL:endpoint:)
is provided for one-off requests. This uses anAnonymousRequestProvider
and is intended to be used for exploratory work with an API. Its use outside of this context is strongly discouraged.See also
RequestConvertible
See also
RequestProviding
Declaration
Swift
public struct Request<API, Resource> : RequestConvertible where API : RequestProviding
-
Indicates a valid URL could not be constructed from a
RequestConvertible
type.Declaration
Swift
public struct InvalidRequestURLError : Error
-
An error that wraps an error that occurred when executing a network request.
See moreDeclaration
Swift
public struct RequestTransportError : Error
-
A type that can decode a response type from the raw body of a HTTP response.
See moreDeclaration
Swift
public struct ResponseDecoder<Response>