OptionalResponseType

public struct OptionalResponseType<T>: Decodable where T: Decodable

A wrapper which will contain the decoded T instance, if one was successfully decoded; otherwise nil. OptionalResponseType is useful for when you have a request which may or may not return the expected JSON structure. For example, fetching a mythical UserDetails JSON for a user who does not exist, but for reason - unbeknownst to us - the server return no JSON instead of a 404.

Example:

struct UserDetails {
    var mothersMaidenName: String
    var lovesTheDentist: Bool
}

struct UserDetailsRequest: Codable {
    var userId: Int
}

extension UserDetailsRequest: Interceptable, Gettable {
    typealias ResponseType = OptionalResponseType<UserDetails>
    var path: String = "/some/api/path"
}

UserDetailsRequest(userId: 42).submit() { result in
    if case let Result.success(optionalUserDetails) = result {
        let userDetails = optionalUserDetails.instance

    }
}

  • Gets the optional inflated instance of T that may or may not have been returned in an HTTPURLResponse

    Declaration

    Swift

    public private(set) var instance: T? = nil
  • Declaration

    Swift

    public init(from decoder: Decoder) throws