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 convert Data 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 toAutomaticBodyDecoding. A default decoder is created for you automatically, and added by default to every Requestable or ComposableRequest type that has been specialized with your type as its ResponseBody

    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:

    See similar types BodyEncoder and ResponseValidator

    See more

    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 into Data

    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 toAutomaticBodyEncoding. A default encoder is created for you automatically, and added by default to every Requestable or ComposableRequest type that has been specialized with your type as its RequestBody

    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:

    See similar types BodyDecoder and ResponseValidator

    See more

    Declaration

    Swift

    public struct BodyEncoder<Body>
  • A ComposableRequest is a generic type used to execute HTTP requests without needing to create request-specific Requestable 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 ComposableRequest with body types, as well as an error model of your choosing. If you use body types String, Data, Codable, RequestParameters or AnyJSON, default encoder and decoders are provided for you. If you use an error type HTTPError, 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 continue

    See more

    Declaration

    Swift

    public struct ComposableRequest<RequestBody, ResponseBody, ResponseError> : Requestable where ResponseError : Error
  • A value type used to validate a RequestResponse

    See more

    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 a Request and recieved with a RequestResponse

    See the the nested Key and Value types for more detailed information. You can create a RequestHeaders using a dictionary litera containing those types.

    See more

    Declaration

    Swift

    public struct RequestHeaders : ExpressibleByDictionaryLiteral, Equatable, Hashable, CustomStringConvertible
  • RequestParameters are a type used to express values as a URL encoded form.

    See more

    Declaration

    Swift

    public struct RequestParameters : Equatable, Hashable, CustomStringConvertible, ExpressibleByDictionaryLiteral
  • A network response generated by a Requestable

    See more

    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 data

    Declaration

    Swift

    public struct NoBody
  • A type used to specialize Requestable when you aren’t concerned with response validation

    Declaration

    Swift

    public struct NoError : Error
  • A generic JSON value type that can be expressed with Swift Literals

    See more

    Declaration

    Swift

    public struct AnyJSON : ExpressibleByStringLiteral, ExpressibleByArrayLiteral, ExpressibleByDictionaryLiteral, ExpressibleByIntegerLiteral, ExpressibleByFloatLiteral, ExpressibleByBooleanLiteral
  • A type erased Requestable wrapper, with no other functional differences.

    See more

    Declaration

    Swift

    public struct AnyRequestable<RequestBody, ResponseBody, ResponseError> : Requestable where ResponseError : Error