Restable

public protocol Restable

The base protocol for method-specific protocols.

  • The expected, deserialized type for this request.

    For example, if we are creating a request which gets a User with a specific ID, we might have have something like the following:

    struct User: Decodable {
        enum Sex: String, Codable {
            case male, female
        }
    
        var name: String
        var age: Int
        var sex: Sex
    }
    
    struct GetUserRequest {
        var userId: Int
    }
    
    extension GetUserRequest: Gettable {
        typealias ResponseType = User
        var url: URL? {
            return URL(string: "https://myfancyapi.com/user/\(userId)")
        }
    }
    

    In our request we are expecting some User JSON to come down. We create a struct which maps the structure of that JSON, have it conform to Swift’s Decodable protocol, and, using the ResponseType, tell our GetUserRequest “Hey, when you receive the JSON in the response, deserialize it to whatever ResponeType is (i.e. a User instance)

    Declaration

    Swift

    associatedtype ResponseType: Decodable
  • dateDecodingStrategy Default implementation

    Defines the JSONDecoder.DateDecodingStrategy to use when decoding from JSON into the conforming instance. Defaults to .deferredToDate

    Default Implementation

    Defines the JSONDecoder.DateDecodingStrategy to use when decoding from JSON into the conforming type. Defaults to .deferredToDate

    Declaration

    Swift

    var dateDecodingStrategy: JSONDecoder.DateDecodingStrategy
  • dateEncodingStrategy Default implementation

    Defines the JSONEncoder.DateEncodingStrategy to use when JSON encoding a conforming instance. Defaults to .deferredToDate

    Default Implementation

    Defines the JSONEncoder.DateEncodingStrategy to use when JSON encoding a conforming instance. Defaults to .deferredToDate

    Declaration

    Swift

    var dateEncodingStrategy: JSONEncoder.DateEncodingStrategy
  • baseURL Default implementation

    The base url against which the request will be made. Example:

    "https://www.google.com"
    

    Default Implementation

    The base url against which the request will be made. Example:

    "https://www.google.com"
    

    Declaration

    Swift

    var baseURL: String
  • path Default implementation

    The Path to the endpoint. Example:

    /some/path

    Default Implementation

    The Path to the endpoint. Example:

    /some/path

    Declaration

    Swift

    var path: String
  • url Default implementation

    An optional URL to use for the request instead of baseURL and path. When preset baseURL and path will be ignored. Defaults to nil.

    Default Implementation

    An optional URL to use for the request instead of baseURL and path. When preset baseURL and path will be ignored. Defaults to nil.

    Declaration

    Swift

    var url: URL?
  • cachePolicy Default implementation

    The cache policy to use for this Restable. Defaults to .useProtocolCachePolicy

    Default Implementation

    The cache policy to use for this Restable. Defaults to .useProtocolCachePolicy

    Declaration

    Swift

    var cachePolicy: URLRequest.CachePolicy
  • timeoutInterval Default implementation

    The timeout interval for the request. Defaults to 60.0

    Default Implementation

    The timeout interval for the request. Defaults to 60.0

    Declaration

    Swift

    var timeoutInterval: TimeInterval
  • Creates a URLRequest object.

    Declaration

    Swift

    func request() throws -> URLRequest

    Return Value

    A URLRequest object, if one was successfully created

  • resultFormat Default implementation

    Defines the expected format of the response

    Default Implementation

    Defines the expected format of the response Defaults to .json

    Declaration

    Swift

    var resultFormat: ResultFormat
  • Submits this request

    Throws

    If a URLSessionDataTask failed to create

    Default Implementation

    Submits this request

    Throws

    If a URLSessionDataTask failed to create

    Declaration

    Swift

    @discardableResult func submit(callbackOnMain: Bool, session: URLSession, completion: RestableCompletionHandler<ResponseType>?) throws -> URLSessionDataTask

    Parameters

    callbackOnMain

    A flag indicating if the completionHandler should be dispatched to the main queue.

    session

    The URLSession from which the URLSessionDataTask will be created.

    completion

    The handler to be called upon completion or failure.

    Return Value

    The URLSessionDataTask

  • fullPath Extension method

    Returns a dumb concatenation of baseURL + path.

    Declaration

    Swift

    public var fullPath: String