Structures
The following structures are available globally.
-
A generic value type to decode response body content from
DataUsage
You specilize the
Decoderwith the type you want to decode, and pass in a closure to convertDatainto the type:let decoder = BodyDecoder<MyType> { data in // return `MyType?` or throw an error }Reusability
Typically, you will want to re-use body decoders for a given type.
The best way to do this is to declare a your decoder in a type-constrained extensions
extension BodyDecoder where Body == MyType { static var myDecoder: Self { .init { data in // Convert `Data?` into `MyType?` or throw an error } } } // You can now use this decoder with a `ComposableRequest`, or when creating type that conform to `Requestable` let request = ComposableRequest<Any, MyType, Error> .decode(with: .myDecoder) struct MyRequest: Requestable { var responseDecoder: BodyDecoder<MyType> { .myDecoder } }Alternatively, if you want to create an decoder for a type you have control over, simply have that type conform to
AutomaticBodyDecoding. Adefaultdecoder is created for you automatically, and added by default to everyRequestableorComposableRequesttype that has been specialized with your type as itsResponseBodyextension MyType: AutomaticBodyDecoding { init?(fromData: Data?) throws { // Initialize `MyType`, return `nil`, or throw an error } } // `default` is created for you automatically, and need not be added to `Requestable` or `ComposableRequest` instances. let decoder = BodyDecoder<MyType>.defaultOmbi provides default body decoders for the following types:
StringDataAnyJSON- Types that conform to
Decodable - Types that conform to
AutomaticBodyDecoding
See similar types
See moreBodyEncoderandResponseValidatorDeclaration
Swift
public struct BodyDecoder<Body> -
A generic value type to encode request body content as
DataUsage
You specilize the
Encoderwith the type you want to encoder, and pass in a closure to convert the type intoDatalet encoder = BodyEncoder<MyType> { body in // return `Data?` or throw an error }Reusability
Typically, you will want to re-use body encoders for a given type.
The best way to do this is to declare a your encoder in a type-constrained extensions
extension BodyEncoder where Body == MyType { static var myEncoder: Self { .init { body in // Convert `MyType?` into `Data?` or throw an error } } } // You can now use this encoder with a `ComposableRequest`, or when creating type that conform to `Requestable` let request = ComposableRequest<Any, MyType, Error> .encode(with: .myEncoder) struct MyRequest: Requestable { var requestEncoder: BodyEncoder<MyType> { .myEncoder } }Alternatively, if you want to create an encoder for a type you have control over, simply have that type conform to
AutomaticBodyEncoding. Adefaultencoder is created for you automatically, and added by default to everyRequestableorComposableRequesttype that has been specialized with your type as itsRequestBodyextension MyType: AutomaticBodyEncoding { func asData() throws -> Data? { // Return `Data?` or throw an error } } // `default` is created for you automatically, and need not be added to `Requestable` or `ComposableRequest` instances. let encoder = BodyEncoder<MyType>.defaultOmbi provides default body encoders for the following types:
StringDataAnyJSONRequestParameters- Types that conform to
Encodable - Types that conform to
AutomaticBodyEncoding
See similar types
See moreBodyDecoderandResponseValidatorDeclaration
Swift
public struct BodyEncoder<Body> -
A
ComposableRequestis a generic type used to execute HTTP requests without needing to create request-specificRequestabletypes.Basic usage might look like this:
cancellable = ComposableRequest<AnyJSON, AnyJSON, HTTPError>() .path("/users") .method(.post) .headers([.contentType: .contentType(.json)]) .body( [ "name": "morpheus", "job": "leader" ] ) .send(on: "https://reqres.in/api") .sink { result in // handle completion } receiveValue: { response in // handle response }You can specialize a
See moreComposableRequestwith body types, as well as an error model of your choosing. If you use body typesString,Data,Codable,RequestParametersorAnyJSON, default encoder and decoders are provided for you. If you use an error typeHTTPError, a response validator is used for you. If you provide your own error model, rember to provide a validator, as the default validator automatically allows all responses to continueDeclaration
Swift
public struct ComposableRequest<RequestBody, ResponseBody, ResponseError> : Requestable where ResponseError : Error -
A value type used to validate a
See moreRequestResponseDeclaration
Swift
public struct ResponseValidator<Body, Error> where Error : Error -
RequestHeadersis a value type that stores mapping of header keys an their values. It is sent with aRequestand recieved with aRequestResponseSee the the nested
See moreKeyandValuetypes for more detailed information. You can create aRequestHeadersusing a dictionary litera containing those types.Declaration
Swift
public struct RequestHeaders : ExpressibleByDictionaryLiteral, Equatable, Hashable, CustomStringConvertible -
See moreRequestParametersare a type used to express values as a URL encoded form.Declaration
Swift
public struct RequestParameters : Equatable, Hashable, CustomStringConvertible, ExpressibleByDictionaryLiteral -
A network response generated by a
See moreRequestableDeclaration
Swift
public struct RequestResponse<Body>extension RequestResponse: Equatable where Body: Equatableextension RequestResponse: Hashable where Body: Hashable -
A type used to specialize
Requestablewhen you aren’t concerned with body dataDeclaration
Swift
public struct NoBody -
A type used to specialize
Requestablewhen you aren’t concerned with response validationDeclaration
Swift
public struct NoError : Error -
A generic JSON value type that can be expressed with Swift Literals
See moreDeclaration
Swift
public struct AnyJSON : ExpressibleByStringLiteral, ExpressibleByArrayLiteral, ExpressibleByDictionaryLiteral, ExpressibleByIntegerLiteral, ExpressibleByFloatLiteral, ExpressibleByBooleanLiteral -
A type erased
See moreRequestablewrapper, with no other functional differences.Declaration
Swift
public struct AnyRequestable<RequestBody, ResponseBody, ResponseError> : Requestable where ResponseError : Error
View on GitHub
Install in Dash
Structures Reference