AnyRestable

public class AnyRestable<ExpectedResponseType: Decodable>: Restable

A Type-erased container which can hold any Restable for a given response type. Use this for variables and/or function parameters instead of raw Restable. Attemping to use a raw Restable or any of its children (Gettable, Postable, etc.) will result in a compiler error regarding associated types. To get the full fledged story google Swift Static Linking and Protocols with Associated Types, crack a bottle of whisky, and watch Game of Thrones.

  • The expected Decodable that the wrapped request will handle. This should match the wrapped Restable’s ResponseType.

    Declaration

    Swift

    public typealias ResponseType = ExpectedResponseType
  • The base url against which the request will be made. Example:

    "https://www.google.com"
    

    Declaration

    Swift

    public var baseURL: String
  • The Path to the endpoint. Example:

    /some/path

    Declaration

    Swift

    public var path: String
  • Wraps the provided restable into a type-erased enclosure, allowing it to be passed around to other functions, used as a stored property, etc.

    Declaration

    Swift

    public init<R: Restable>(_ restable: R) where R.ResponseType == ExpectedResponseType

    Parameters

    restable

    The restable to be type-erased.

  • Creates a URLRequest object.

    Declaration

    Swift

    public func request() throws -> URLRequest

    Return Value

    A URLRequest object, if one was successfully created

  • Submits this request

    Throws

    If a URLSessionDataTask failed to create

    Declaration

    Swift

    @discardableResult public func submit(callbackOnMain: Bool = true,
                       session: URLSession = URLSession.shared,
                       completion: RestableCompletionHandler<ResponseType>? = nil) throws -> URLSessionDataTask

    Parameters

    callbackOnMain

    When true, will dispatch the completion on the main queue. Otherwise completion will be dispatched on whichever dispatch queue the task was original submitted. Defaults to true.

    session

    The URLSession from which the URLSessionDataTask will be created. Defaults to URLSession.shared

    completion

    The handler to be called upon completion or failure. Defaults to nil

    Return Value

    The URLSessionDataTask