Skip to content

Instantly share code, notes, and snippets.

@sathishvgs
Last active January 19, 2019 15:11
Show Gist options
  • Save sathishvgs/1e1ff35857d77a27623406917ee70d31 to your computer and use it in GitHub Desktop.
Save sathishvgs/1e1ff35857d77a27623406917ee70d31 to your computer and use it in GitHub Desktop.
OAuth Authorization Flow
public func authRequest(_ urlRequest: URLRequest, responseJSON: @escaping (DataResponse<Any>) -> Void) -> DataRequest {
if isAccessTokenValid {
return self.makeRequest(url: urlRequest).validate().responseJSON(completionHandler: responseJSON)
} else {
return refreshToken(urlRequest: urlRequest, { [weak self] (success) in
guard success else {
responseJSON(DataResponse(request: urlRequest, response: nil, data: nil, result: Result.failure(error)))
return
}
self.makeRequest(url: urlRequest).validate().responseJSON(completionHandler: responseJSON)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment