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
BodyProviderimplements a function that takes aninoutHeaderand produces aRequestBody. In thebody(updating:)method, the provider should construct theRequestBodyand update theContent-Typeof the providedHeader. The provider can also set any other relevant fields in the header.Several predefined
BodyProviders are provided for text bodies and JSON bodies.Creating a
BodyProviderThe
body(updating:)method is defined when theBodyProvideris initialised as a closure. The method is somewhat odd because it doesn’t take any data to convert to aRequestBodyas input. In yourBodyProviders you should capture the input data in the closure provided at initialization. This means that mostBodyProviders 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 moreheaderare 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 moreStringwrapper 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
nameontovalue.See moreNote
As HTTP headers are case insensitive, the implementations ofHashableandEquatabletake 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
HTTPMethodtype.Pattern Matching
See moreHTTPMethodprovides an overload of the~=operator so methods can easily be pattern matched on in aswitchstatement.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.Nametype while values are simplyStrings.See moreNote
AHeaderinstance 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
typeasubtypeand 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
typeandsubtypeof 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
MediaTypeare:- The implementations of
EquatableandHashableare case-insensitive. - Ditto for the parameters of a
MediaType. - The
parametersof aMediaTypeare not used in theEquatableandHashableimplementation 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.SubTypeand the new media type in an extension ofMediaTypeFor 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.formDatais defined as:static let formData: (String) -> MediaType = { boundary in MediaType(type: .multipart, subtype: .formData, parameters: ["boundary": boundary]) }which will produce a new
See moreMediaTypewith theboundaryparameter set to the providedString.Declaration
Swift
public struct MediaType : CustomStringConvertible - The
-
A
RequestProvidingtype to a single API host.Use an
See moreAnonymousRequestProviderfor one-off API requests or when you do not care about theAPIparameter of theRequesttype.Declaration
Swift
public struct AnonymousRequestProvider : RequestProviding -
A request for a resource from an API.
Overview
The
Requeststructure is a concrete implementation of aRequestConvertiblethat provides a chainable builder-like interface to constructing requests. You rarely instantiate aRequestdirectly. Instead, requests to a specific API are created by aRequestProvidingtype and are customised using the builder functions onRequest.The
Requeststructure is generic over theAPItype and theResourcetype. TheAPIparameter is immutable and can be used to constrain extensions onRequestto a specific API. TheResourceparameter 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 anAnonymousRequestProviderand is intended to be used for exploratory work with an API. Its use outside of this context is strongly discouraged.See also
RequestConvertibleSee moreSee also
RequestProvidingDeclaration
Swift
public struct Request<API, Resource> : RequestConvertible where API : RequestProviding
-
Indicates a valid URL could not be constructed from a
RequestConvertibletype.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>
View on GitHub
Structures Reference