Structures
The following structures are available globally.
-
A generic value type to decode response body content from
Data
Usage
You specilize the
Decoder
with the type you want to decode, and pass in a closure to convertData
into 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
. Adefault
decoder is created for you automatically, and added by default to everyRequestable
orComposableRequest
type that has been specialized with your type as itsResponseBody
extension 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>.default
Ombi provides default body decoders for the following types:
String
Data
AnyJSON
- Types that conform to
Decodable
- Types that conform to
AutomaticBodyDecoding
See similar types
See moreBodyEncoder
andResponseValidator
Declaration
Swift
public struct BodyDecoder<Body>
-
A generic value type to encode request body content as
Data
Usage
You specilize the
Encoder
with the type you want to encoder, and pass in a closure to convert the type intoData
let 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
. Adefault
encoder is created for you automatically, and added by default to everyRequestable
orComposableRequest
type that has been specialized with your type as itsRequestBody
extension 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>.default
Ombi provides default body encoders for the following types:
String
Data
AnyJSON
RequestParameters
- Types that conform to
Encodable
- Types that conform to
AutomaticBodyEncoding
See similar types
See moreBodyDecoder
andResponseValidator
Declaration
Swift
public struct BodyEncoder<Body>
-
A
ComposableRequest
is a generic type used to execute HTTP requests without needing to create request-specificRequestable
types.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 moreComposableRequest
with body types, as well as an error model of your choosing. If you use body typesString
,Data
,Codable
,RequestParameters
orAnyJSON
, 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 moreRequestResponse
Declaration
Swift
public struct ResponseValidator<Body, Error> where Error : Error
-
RequestHeaders
is a value type that stores mapping of header keys an their values. It is sent with aRequest
and recieved with aRequestResponse
See the the nested
See moreKey
andValue
types for more detailed information. You can create aRequestHeaders
using a dictionary litera containing those types.Declaration
Swift
public struct RequestHeaders : ExpressibleByDictionaryLiteral, Equatable, Hashable, CustomStringConvertible
-
See moreRequestParameters
are 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 moreRequestable
Declaration
Swift
public struct RequestResponse<Body>
extension RequestResponse: Equatable where Body: Equatable
extension RequestResponse: Hashable where Body: Hashable
-
A type used to specialize
Requestable
when you aren’t concerned with body dataDeclaration
Swift
public struct NoBody
-
A type used to specialize
Requestable
when 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 moreRequestable
wrapper, with no other functional differences.Declaration
Swift
public struct AnyRequestable<RequestBody, ResponseBody, ResponseError> : Requestable where ResponseError : Error