-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tiago Nascimento <[email protected]>
- Loading branch information
1 parent
e1e8d12
commit 0c07177
Showing
3 changed files
with
126 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import Foundation | ||
|
||
import SpruceIDMobileSdkRs | ||
|
||
public class Oid4vciSyncHttpClient: SyncHttpClient { | ||
public func httpClient(request: HttpRequest) throws -> HttpResponse { | ||
guard let url = URL(string: request.url) else { | ||
throw HttpClientError.Other(error: "failed to construct URL") | ||
} | ||
|
||
let session = URLSession.shared | ||
var req = URLRequest(url: url, | ||
cachePolicy: .useProtocolCachePolicy, | ||
timeoutInterval: 60) | ||
req.httpMethod = request.method | ||
req.httpBody = request.body | ||
req.allHTTPHeaderFields = request.headers | ||
|
||
// Semaphore used to wait for the callback to complete | ||
let semaphore = DispatchSemaphore(value: 0) | ||
|
||
var data: Data? | ||
var response: URLResponse? | ||
var error: Error? | ||
|
||
let dataTask = session.dataTask(with: req) { | ||
data = $0 | ||
response = $1 | ||
error = $2 | ||
|
||
// Signaling from inside the callback will let the outside function | ||
// know that `data`, `response` and `error` are ready to be read. | ||
semaphore.signal() | ||
} | ||
// Initiate execution of the http request | ||
dataTask.resume() | ||
|
||
// Blocking wait for the callback to signal back | ||
_ = semaphore.wait(timeout: .distantFuture) | ||
|
||
if let error { | ||
throw HttpClientError.Other(error: "failed to execute request: \(error)") | ||
} | ||
|
||
guard let response = response as? HTTPURLResponse else { | ||
throw HttpClientError.Other(error: "failed to parse response") | ||
} | ||
|
||
guard let data = data else { | ||
throw HttpClientError.Other(error: "failed to parse response data") | ||
} | ||
|
||
guard let statusCode = UInt16(exactly: response.statusCode) else { | ||
throw HttpClientError.Other(error: "failed to parse response status code") | ||
} | ||
|
||
let headers = try response.allHeaderFields.map({ (key, value) in | ||
guard let key = key as? String else { | ||
throw HttpClientError.HeaderParse | ||
} | ||
|
||
guard let value = value as? String else { | ||
throw HttpClientError.HeaderParse | ||
} | ||
|
||
return (key, value) | ||
}) | ||
|
||
return HttpResponse( | ||
statusCode: statusCode, | ||
headers: Dictionary(uniqueKeysWithValues: headers), | ||
body: data) | ||
} | ||
} | ||
|
||
public class Oid4vciAsyncHttpClient: AsyncHttpClient { | ||
public func httpClient(request: HttpRequest) async throws -> HttpResponse { | ||
guard let url = URL(string: request.url) else { | ||
throw HttpClientError.Other(error: "failed to construct URL") | ||
} | ||
|
||
let session = URLSession.shared | ||
var req = URLRequest(url: url, | ||
cachePolicy: .useProtocolCachePolicy, | ||
timeoutInterval: 60) | ||
req.httpMethod = request.method | ||
req.httpBody = request.body | ||
req.allHTTPHeaderFields = request.headers | ||
|
||
let data: Data | ||
let response: URLResponse | ||
do { | ||
(data, response) = try await session.data(for: req) | ||
} catch { | ||
throw HttpClientError.Other(error: "failed to execute request: \(error)") | ||
} | ||
|
||
guard let response = response as? HTTPURLResponse else { | ||
throw HttpClientError.Other(error: "failed to parse response") | ||
} | ||
|
||
guard let statusCode = UInt16(exactly: response.statusCode) else { | ||
throw HttpClientError.Other(error: "failed to parse response status code") | ||
} | ||
|
||
let headers = try response.allHeaderFields.map({ (key, value) in | ||
guard let key = key as? String else { | ||
throw HttpClientError.HeaderParse | ||
} | ||
|
||
guard let value = value as? String else { | ||
throw HttpClientError.HeaderParse | ||
} | ||
|
||
return (key, value) | ||
}) | ||
|
||
return HttpResponse( | ||
statusCode: statusCode, | ||
headers: Dictionary(uniqueKeysWithValues: headers), | ||
body: data) | ||
} | ||
} |