-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(API): implement AppSyncRealTimeClient and WebSocketClient with U…
…RLSession (#3575) * feat(API): implement AppSyncRealTimeClient and WebSocketClient with URLSession (#3527) * feat(datastore): replace appSync realtime client with URLSession * Add timeout for appsync realtime request * adding unit test cases * move websocket components to core * added more interfaces for unit test * add unit test cases for websocket client * fix broken integration test cases * change appsync client state to a subject * Add resettable for appsync and websocket clients * add integration test cases for appsync client * Add subscription actor * fix subscription life cycle issue * add integration test cases for datastore with large number of models * add integration test case for max subscription reached * fix auth error handling for AppSyncRealTimeRequest errors * add doc comments for WebSocketClient and AppSyncRealTimeClient * add integration test case for retry on maxSubscriptionReached * move sendRequest to appSyncRealTimeClient * Add more doc comments * bind tasks cancellables to connection life cycle * update websocket spi name * resolve comments * fix(Logging): Updating the required reason API usage (#3570) * chore: release 2.27.3 [skip ci] * chore: finalize release 2.27.3 [skip ci] * feat(Auth): Adding forceAliasCreation option during confirmSignUp (#3382) * feat(Auth): Adding forceAliasCreation option during confirmSignUp * update pinpoint unit tests * Revert "update pinpoint unit tests" This reverts commit 0f804d8. * rename CognitoAuth to AuthToken --------- Co-authored-by: Harsh <[email protected]> Co-authored-by: Abhash Kumar Singh <[email protected]> Co-authored-by: aws-amplify-ops <[email protected]> * add a high level readme file for AppSyncRealTimeClient module * add a high level readme file for WebSocketClient module * update push notification test scheme * revert changes made in push notification category --------- Co-authored-by: Harsh <[email protected]> Co-authored-by: Abhash Kumar Singh <[email protected]> Co-authored-by: aws-amplify-ops <[email protected]>
- Loading branch information
1 parent
c2fb1c0
commit 014d9b1
Showing
68 changed files
with
4,340 additions
and
1,130 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 |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
// | ||
|
||
import Amplify | ||
import AppSyncRealTimeClient | ||
|
||
extension APIError { | ||
|
||
|
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 |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
// | ||
|
||
import Amplify | ||
import AppSyncRealTimeClient | ||
|
||
extension AWSAPIPlugin { | ||
var log: Logger { | ||
|
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
111 changes: 111 additions & 0 deletions
111
.../API/Sources/AWSAPIPlugin/AppSyncRealTimeClient/AppSyncRealTimeClient+HandleRequest.swift
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,111 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
|
||
import Foundation | ||
import Combine | ||
import Amplify | ||
|
||
extension AppSyncRealTimeClient { | ||
/** | ||
Submit an AppSync request to real-time server. | ||
- Returns: | ||
Void indicates request is finished successfully | ||
- Throws: | ||
Error is throwed when request is failed | ||
*/ | ||
func sendRequest( | ||
_ request: AppSyncRealTimeRequest, | ||
timeout: TimeInterval = 5 | ||
) async throws { | ||
var responseSubscriptions = Set<AnyCancellable>() | ||
try await withCheckedThrowingContinuation { [weak self] (continuation: CheckedContinuation<Void, Swift.Error>) in | ||
guard let self else { | ||
Self.log.debug("[AppSyncRealTimeClient] client has already been disposed") | ||
continuation.resume(returning: ()) | ||
return | ||
} | ||
|
||
// listen to response | ||
self.subject | ||
.setFailureType(to: AppSyncRealTimeRequest.Error.self) | ||
.flatMap { Self.filterResponse(request: request, response: $0) } | ||
.timeout(.seconds(timeout), scheduler: DispatchQueue.global(qos: .userInitiated), customError: { .timeout }) | ||
.first() | ||
.sink(receiveCompletion: { completion in | ||
switch completion { | ||
case .finished: | ||
continuation.resume(returning: ()) | ||
case .failure(let error): | ||
continuation.resume(throwing: error) | ||
} | ||
}, receiveValue: { _ in }) | ||
.store(in: &responseSubscriptions) | ||
|
||
// sending request; error is discarded and will be classified as timeout | ||
Task { | ||
do { | ||
let decoratedRequest = await self.requestInterceptor.interceptRequest( | ||
event: request, | ||
url: self.endpoint | ||
) | ||
let requestJSON = String(data: try Self.jsonEncoder.encode(decoratedRequest), encoding: .utf8) | ||
|
||
try await self.webSocketClient.write(message: requestJSON!) | ||
} catch { | ||
Self.log.debug("[AppSyncRealTimeClient]Failed to send AppSync request \(request), error: \(error)") | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static func filterResponse( | ||
request: AppSyncRealTimeRequest, | ||
response: AppSyncRealTimeResponse | ||
) -> AnyPublisher<AppSyncRealTimeResponse, AppSyncRealTimeRequest.Error> { | ||
let justTheResponse = Just(response) | ||
.setFailureType(to: AppSyncRealTimeRequest.Error.self) | ||
.eraseToAnyPublisher() | ||
|
||
switch (request, response.type) { | ||
case (.connectionInit, .connectionAck): | ||
return justTheResponse | ||
|
||
case (.start(let startRequest), .startAck) where startRequest.id == response.id: | ||
return justTheResponse | ||
|
||
case (.stop(let id), .stopAck) where id == response.id: | ||
return justTheResponse | ||
|
||
case (_, .error) | ||
where request.id != nil | ||
&& request.id == response.id | ||
&& response.payload?.errors != nil: | ||
let errorsJson: JSONValue = (response.payload?.errors)! | ||
let errors = errorsJson.asArray ?? [errorsJson] | ||
let reqeustErrors = errors.compactMap(AppSyncRealTimeRequest.parseResponseError(error:)) | ||
if reqeustErrors.isEmpty { | ||
return Empty( | ||
outputType: AppSyncRealTimeResponse.self, | ||
failureType: AppSyncRealTimeRequest.Error.self | ||
).eraseToAnyPublisher() | ||
} else { | ||
return Fail( | ||
outputType: AppSyncRealTimeResponse.self, | ||
failure: reqeustErrors.first! | ||
).eraseToAnyPublisher() | ||
} | ||
|
||
default: | ||
return Empty( | ||
outputType: AppSyncRealTimeResponse.self, | ||
failureType: AppSyncRealTimeRequest.Error.self | ||
).eraseToAnyPublisher() | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.