APIRequest

open class APIRequest<ReturnType> : Operation where ReturnType : APIReturnable

Base class for creating an APIRequest.

Note

APIRequests should be created through a class that conforms to APIService.

Types & Aliases

  • Alias for the callback when an request completes.

    Declaration

    Swift

    public typealias Completion = (Result<(ReturnType, HTTPCookies), Error>) -> Void

Implemenation Properties

  • DispatchQueue used to serialize access to state.

    Declaration

    Swift

    private let stateQueue: DispatchQueue
  • Tracks state of the request; backs state property.

    Declaration

    Swift

    private var _state: APIRequestState
  • Objective-C visible setter and getter for _state.

    Declaration

    Swift

    @objc
    private dynamic var state: APIRequestState { get set }
  • A Boolean value indicating whether the request can be performed now.

    Declaration

    Swift

    open override var isReady: Bool { get }
  • A Boolean value indicating whether the request is currently executing.

    Declaration

    Swift

    public final override var isExecuting: Bool { get }
  • A Boolean value indicating whether the request has finished executing.

    Declaration

    Swift

    public final override var isFinished: Bool { get }
  • A Boolean value indicating the request executes asynchronously.

    Declaration

    Swift

    public final override var isAsynchronous: Bool { get }
  • The URLSessionDataTask backing the request.

    Declaration

    Swift

    private var dataTask: URLSessionDataTask?

Request Properties

  • The endpoint for the request relative to the baseURL of the service.

    Declaration

    Swift

    public let endpoint: String
  • The http method for the request.

    Declaration

    Swift

    public let method: HTTPMethod
  • The url parameters for the request.

    Declaration

    Swift

    public let parameters: HTTPParameters?
  • The json body for the request.

    Declaration

    Swift

    public let body: HTTPBody?
  • The http headers for the request.

    Declaration

    Swift

    public let headers: HTTPParameters?
  • The APIService the request is part of.

    Declaration

    Swift

    public let service: APIService.Type
  • The object used to authorize the request.

    Declaration

    Swift

    open private(set) var authorization: APIAuthorization?
  • The callback on a completed request, called with the result.

    Declaration

    Swift

    open private(set) var completion: ((Result<(ReturnType, HTTPCookies), Error>) -> Void)?

Private API

  • Registers the dependent keys for isReady.

    Declaration

    Swift

    @objc
    private dynamic class func keyPathsForValuesAffectingIsReady() -> Set<String>
  • Registers the dependent keys for isExecuting.

    Declaration

    Swift

    @objc
    private dynamic class func keyPathsForValuesAffectingIsExecuting() -> Set<String>
  • Registers the dependent keys for isFinished.

    Declaration

    Swift

    @objc
    private dynamic class func keyPathsForValuesAffectingIsFinished() -> Set<String>
  • Creates a URLRequest representing the request.

    Declaration

    Swift

    private func formURLRequest() throws -> URLRequest
  • The completion handler for the URLSessionDataTask backing the request.

    Declaration

    Swift

    private func urlRequestCallback(data: Data?, response: URLResponse?, error: Error?)
  • Starts the request, this method should only be called by an OperationQueue. Instead, use launch(), launch(on:), or add this object to an OperationQueue to begin the request.

    Declaration

    Swift

    public final override func start()

Public API

  • Creates a new APIRequest.

    Note

    Only to be used by a class that conforms to APIService.

    Declaration

    Swift

    public init(service: APIService.Type,
                endpoint: String,
                params: HTTPParameters? = nil,
                body: HTTPBody? = nil,
                headers: HTTPHeaders? = nil,
                method: HTTPMethod)

    Parameters

    endpoint

    The api endpoint relative to the baseURL of the service.

    params

    Optional url parameters for the request.

    body

    Optional http headers for the request.

    body

    Optional http headers for the request.

    method

    The method for the request.

  • Sets the authorization for an request.

    Declaration

    Swift

    @discardableResult
    open func authorize(with authorization: APIAuthorization?) -> APIRequest

    Parameters

    authorization

    The object used to authorize the request.

    Return Value

    self for method chaining as needed.

  • Sets the callback on a completed request, called with the result.

    Declaration

    Swift

    @discardableResult
    open func onCompletion(_ completion: Completion?) -> APIRequest

    Parameters

    comepletion

    The block to be called on a completed request.

    Return Value

    self for method chaining as needed.

  • Launches the request and calls the completion handler once the finished.

    Declaration

    Swift

    @discardableResult
    open func launch(on queue: OperationQueue = .defaultAPIRequestQueue) -> APIRequest

    Parameters

    queue

    The OperationQueue to run the request on, will be run the default queue if none is specified.

    Return Value

    self for method chaining as needed.

  • Cancels an in-flight request.

    Declaration

    Swift

    open override func cancel()