-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Upload Request Timeout, Fix API Error Messages #230
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
30ef5d3
fixed api error and set offline mode false by default
jumaallan 2ffa1eb
Merge branch 'main' into feat/fix-api-messages
jumaallan 986bc5e
Specify a new argument for SmileID initialiser to specify timeout for…
tobitech 2ca5946
Refactor the upload rest service implementation to use an async await…
tobitech b33536b
some code formatting.
tobitech b4a5d1b
Merge branch 'feat/fix-api-messages' into upload-request-timeout
tobitech 097e939
update changelog. code formatting.
tobitech f1d5ec1
make a static function for creating a URLSession to be used in the ne…
tobitech 3397df1
update documentation
tobitech 832ecb5
update doc.
tobitech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 |
---|---|---|
|
@@ -13,11 +13,14 @@ class URLSessionRestServiceClient: NSObject, RestServiceClient { | |
typealias URLSessionResponse = (data: Data, response: URLResponse) | ||
let session: URLSessionPublisher | ||
let decoder = JSONDecoder() | ||
let uploadRequestTimeout: TimeInterval | ||
|
||
public init( | ||
session: URLSessionPublisher = URLSession.shared | ||
session: URLSessionPublisher = URLSession.shared, | ||
uploadRequestTimeout: TimeInterval = SmileID.defaultUploadRequestTimeout | ||
) { | ||
self.session = session | ||
self.uploadRequestTimeout = uploadRequestTimeout | ||
} | ||
|
||
func send<T: Decodable>(request: RestRequest) async throws -> T { | ||
|
@@ -31,24 +34,21 @@ class URLSessionRestServiceClient: NSObject, RestServiceClient { | |
} | ||
} | ||
|
||
public func upload(request: RestRequest) async throws -> AsyncThrowingStream<UploadResponse, Error> { | ||
AsyncThrowingStream<UploadResponse, Error> { continuation in | ||
do { | ||
let urlRequest = try request.getUploadRequest() | ||
let delegate = URLDelegate(continuation: continuation) | ||
let uploadSession = URLSession(configuration: .default, delegate: delegate, delegateQueue: nil) | ||
uploadSession.uploadTask(with: urlRequest, from: request.body) { data, response, error in | ||
if let error = error { | ||
continuation.finish(throwing: error) | ||
return | ||
} | ||
if (response as? HTTPURLResponse)?.statusCode == 200 { | ||
continuation.yield(.response(data: data)) | ||
} | ||
}.resume() | ||
} catch { | ||
continuation.finish(throwing: error) | ||
} | ||
public func upload(request: RestRequest) async throws -> Data { | ||
guard let requestBody = request.body else { | ||
throw SmileIDError.invalidRequestBody | ||
} | ||
|
||
do { | ||
let urlRequest = try request.getUploadRequest() | ||
let configuration = URLSessionConfiguration.default | ||
configuration.timeoutIntervalForRequest = uploadRequestTimeout | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tobitech on android this applies to all requests not just the upload, not sure if I missed the discussion but could there be a reason why we're only usingthe timeout on the upload only? |
||
let uploadSession = URLSession(configuration: configuration) | ||
|
||
let urlSessionResponse = try await uploadSession.upload(for: urlRequest, from: requestBody) | ||
return try checkStatusCode(urlSessionResponse) | ||
} catch { | ||
throw mapToAPIError(error) | ||
} | ||
} | ||
|
||
|
@@ -75,21 +75,32 @@ class URLSessionRestServiceClient: NSObject, RestServiceClient { | |
} | ||
} | ||
|
||
private struct ErrorResponse: Codable { | ||
let error: String | ||
} | ||
|
||
private func checkStatusCode(_ urlSessionResponse: URLSessionResponse) throws -> Data { | ||
let decoder = JSONDecoder() | ||
guard let httpResponse = urlSessionResponse.response as? HTTPURLResponse, | ||
httpResponse.isSuccess | ||
else { | ||
if let decodedError = try? JSONDecoder().decode( | ||
if let decodedError = try? decoder.decode( | ||
SmileIDErrorResponse.self, | ||
from: urlSessionResponse.data | ||
) { | ||
throw SmileIDError.api(decodedError.code, decodedError.message) | ||
} | ||
throw SmileIDError.httpError( | ||
( | ||
if let httpError = try? decoder.decode( | ||
ErrorResponse.self, | ||
from: urlSessionResponse.data | ||
) { | ||
throw SmileIDError.httpError(( | ||
urlSessionResponse.response as? HTTPURLResponse | ||
)?.statusCode ?? 500, | ||
urlSessionResponse.data | ||
)?.statusCode ?? 500, httpError.error) | ||
} | ||
throw SmileIDError.httpError( | ||
(urlSessionResponse.response as? HTTPURLResponse)?.statusCode ?? 500, | ||
"Unknown error occurred" | ||
) | ||
} | ||
|
||
|
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe document this as well?