@@ -105,41 +105,40 @@ open class OAuth2Requestable {
105105 open var requestPerformer : OAuth2RequestPerformer ?
106106
107107 /**
108- Perform the supplied request and call the callback with the response JSON dict or an error. This method is intended for authorization
108+ Perform the supplied request and return the response JSON dict or throw an error. This method is intended for authorization
109109 calls, not for data calls outside of the OAuth2 dance.
110110
111- This implementation uses the shared `NSURLSession` and executes a data task. If the server responds with an error, this will be
112- converted into an error according to information supplied in the response JSON (if availale).
113-
114- The callback returns a response object that is easy to use, like so:
115-
116- perform(request: req) { response in
117- do {
118- let data = try response.responseData()
119- // do what you must with `data` as Data and `response.response` as HTTPURLResponse
120- }
121- catch let error {
122- // the request failed because of `error`
123- }
124- }
125-
126- Easy, right?
111+ This implementation uses the shared `NSURLSession`. If the server responds with an error, this will be
112+ converted into an error according to information supplied in the response JSON (if available).
127113
128114 - parameter request: The request to execute
129- - parameter callback: The callback to call when the request completes/fails. Looks terrifying, see above on how to use it
115+ - returns : OAuth2 response
130116 */
131- open func perform( request: URLRequest , callback : @escaping ( ( OAuth2Response ) -> Void ) ) {
117+ open func perform( request: URLRequest ) async -> OAuth2Response {
132118 self . logger? . trace ( " OAuth2 " , msg: " REQUEST \n \( request. debugDescription) \n --- " )
133119 let performer = requestPerformer ?? OAuth2DataTaskRequestPerformer ( session: session)
134120 requestPerformer = performer
135- let task = performer. perform ( request: request) { sessData, sessResponse, error in
136- self . abortableTask = nil
137- self . logger? . trace ( " OAuth2 " , msg: " RESPONSE \n \( sessResponse? . debugDescription ?? " no response " ) \n \n \( String ( data: sessData ?? Data ( ) , encoding: String . Encoding. utf8) ?? " no data " ) \n --- " )
138- let http = ( sessResponse as? HTTPURLResponse ) ?? HTTPURLResponse ( url: request. url!, statusCode: 499 , httpVersion: nil , headerFields: nil ) !
139- let response = OAuth2Response ( data: sessData, request: request, response: http, error: error)
140- callback ( response)
121+
122+ do {
123+ // TODO: add support for aborting the request, see https://www.hackingwithswift.com/quick-start/concurrency/how-to-cancel-a-task
124+ let ( sessData, sessResponse) = try await performer. perform ( request: request)
125+ self . logger? . trace ( " OAuth2 " , msg: " RESPONSE \n \( sessResponse. debugDescription) \n \n \( String ( data: sessData, encoding: String . Encoding. utf8) ?? " no data " ) \n --- " )
126+
127+ guard let response = sessResponse as? HTTPURLResponse else {
128+ throw CommonError . castError (
129+ from: String ( describing: sessResponse. self) ,
130+ to: String ( describing: HTTPURLResponse . self)
131+ )
132+ }
133+
134+ return OAuth2Response ( data: sessData, request: request, response: response, error: nil )
135+
136+ } catch {
137+ self . logger? . trace ( " OAuth2 " , msg: " RESPONSE \n no response \n \n no data \n --- " )
138+
139+ let http = HTTPURLResponse ( url: request. url!, statusCode: 499 , httpVersion: nil , headerFields: nil ) !
140+ return OAuth2Response ( data: nil , request: request, response: http, error: error)
141141 }
142- abortableTask = task
143142 }
144143
145144 /// Currently running abortable session task.
@@ -222,3 +221,16 @@ public func callOnMainThread(_ callback: (() -> Void)) {
222221 }
223222}
224223
224+ // TODO: move to a separate file
225+ enum CommonError : Error {
226+ case castError( from: String , to: String )
227+ }
228+
229+ extension CommonError : CustomStringConvertible {
230+ public var description : String {
231+ switch self {
232+ case . castError( from: let from, to: let to) :
233+ return " Could not cast \( from) to \( to) "
234+ }
235+ }
236+ }
0 commit comments