BodyDecoder

public struct BodyDecoder<Body>

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

Initializers

  • Create a BodyDecoder

    Declaration

    Swift

    public init(_ handler: @escaping Handler)

    Parameters

    handler

    The decoding closure

API

  • Closure used to decode Body from Data

    Declaration

    Swift

    public typealias Handler = (Data?) throws -> Body?
  • The handler that decodes Data

    Declaration

    Swift

    public let decode: Handler
  • A fatal decoder that will cause a runtime failure

    Declaration

    Swift

    static var fatal: `Self` { get }

Available where Body == String

  • Create a String body decoder with specified encoding

    Declaration

    Swift

    init(encoding: String.Encoding = .utf8)

    Parameters

    encoding

    The encoding of the String

  • The default String body decoder

    Declaration

    Swift

    static var `default`: `Self` { get }

Available where Body == Data

  • The default Data body decoder

    Declaration

    Swift

    static var `default`: `Self` { get }

Available where Body == AnyJSON

  • Create a decoder for an AnyJSON

    Declaration

    Swift

    init(options: JSONSerialization.ReadingOptions)

    Parameters

    options

    The reading options to use when deserializing the JSON

  • The default AnyJSON decoder

    Declaration

    Swift

    static var `default`: `Self` { get }

Available where Body: Decodable

  • Create a decoder for a Codable using a JSONDecoder

    Declaration

    Swift

    static func json(decoder: JSONDecoder = JSONDecoder()) -> BodyDecoder<Body>

    Parameters

    decoder

    The JSONDecoder to use for decoding

    Return Value

    The decoder

  • The default Codable decoder

    Declaration

    Swift

    static var `default`: `Self` { get }

Available where Body: AutomaticBodyDecoding

Available where Body == NoBody

  • Undocumented

    Declaration

    Swift

    static var `default`: `Self` { get }