OAuth2DataLoader

open class OAuth2DataLoader : OAuth2Requestable

A class that makes loading data from a protected endpoint easier.

  • The OAuth2 instance used for OAuth2 access tokvarretrieval.

    Declaration

    Swift

    public let oauth2: OAuth2
  • If set to true, a 403 is treated as a 401. The default is false.

    Declaration

    Swift

    public var alsoIntercept403: Bool
  • Designated initializer.

    Provide host if 301 and 302 redirects should be followed automatically, as long as they appear on the same host.

    Declaration

    Swift

    public init(oauth2: OAuth2, host: String? = nil)

    Parameters

    oauth2

    The OAuth2 instance to use for authorization when loading data.

    host

    If given will handle redirects within the same host by way of OAuth2DataLoaderSessionTaskDelegate

Make Requests

  • Overriding this method: it intercepts unauthorizedClient errors, stops and enqueues all calls, starts the authorization and, upon success, resumes all enqueued calls.

    The callback is easy to use, like so:

    perform(request: req) { dataStatusResponse in
        do {
            let (data, status) = try dataStatusResponse()
            // do what you must with `data` as Data and `status` as Int
        }
        catch let error {
            // the request failed because of `error`
        }
    }
    

    Declaration

    Swift

    override open func perform(request: URLRequest, callback: @escaping ((OAuth2Response) -> Void))

    Parameters

    request

    The request to execute

    callback

    The callback to call when the request completes/fails. Looks terrifying, see above on how to use it

  • This method takes an additional retry flag, then uses the base implementation of perform(request:callback:) to perform the given request. It intercepts 401 (and 403, if alsoIntercept403 is true), enqueues the request and performs authorization. During authorization, all requests to be performed are enqueued and they are all dequeued once authorization finishes, either by retrying them on authorization success or by aborting them all with the same error.

    The callback is easy to use, like so:

    perform(request: req) { dataStatusResponse in
        do {
            let (data, status) = try dataStatusResponse()
            // do what you must with `data` as Data and `status` as Int
        }
        catch let error {
            // the request failed because of `error`
        }
    }
    

    Declaration

    Swift

    open func perform(request: URLRequest, retry: Bool, callback: @escaping ((OAuth2Response) -> Void))

    Parameters

    request

    The request to execute

    retry

    Whether the request should be retried on 401 (and possibly 403)

    callback

    The callback to call when the request completes/fails

  • If not already authorizing, will use its oauth2 instance to start authorization after forgetting any tokens (!).

    This method will ignore calls while authorization is ongoing, meaning you will only get the callback once per authorization cycle.

    Declaration

    Swift

    open func attemptToAuthorize(callback: @escaping ((OAuth2JSON?, OAuth2Error?) -> Void))

    Parameters

    callback

    The callback passed on from authorize(callback:). Authorization finishes successfully (auth parameters will be non-nil but may be an empty dict), fails (error will be non-nil) or is canceled (both params and error are nil)

Queue

  • Enqueues the given URLRequest and callback. You probably don’t want to use this method yourself.

    Declaration

    Swift

    public func enqueue(request: URLRequest, callback: @escaping ((OAuth2Response) -> Void))

    Parameters

    request

    The URLRequest to enqueue for later execution

    callback

    The closure to call when the request has been executed

  • Enqueues the given OAuth2DataRequest. You probably don’t want to use this method yourself.

    Declaration

    Swift

    public func enqueue(request: OAuth2DataRequest)

    Parameters

    request

    The OAuth2DataRequest to enqueue for later execution

  • Dequeue all enqueued requests, applying the given closure to all of them. The queue will be empty by the time the closure is called.

    Declaration

    Swift

    public func dequeueAndApply(closure: ((OAuth2DataRequest) -> Void))

    Parameters

    closure

    The closure to apply to each enqueued request

  • Uses dequeueAndApply() by signing and re-performing all enqueued requests.

    Declaration

    Swift

    func retryAll()
  • Uses dequeueAndApply() to all enqueued requests, calling their callback with a response representing the given error.

    Declaration

    Swift

    func throwAllAway(with error: OAuth2Error)

    Parameters

    error

    The error with which to finalize all enqueued requests