-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from amzn/async_await_support
Add initial async/await support.
- Loading branch information
Showing
7 changed files
with
276 additions
and
8 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
61 changes: 61 additions & 0 deletions
61
Sources/_SmokeHTTPClientConcurrency/HTTPOperationsClient+executeRetriableWithOutput.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,61 @@ | ||
// Copyright 2018-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// A copy of the License is located at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
// | ||
// HTTPOperationsClient+executeRetriableWithOutput.swift | ||
// _SmokeHTTPClientConcurrency | ||
// | ||
|
||
#if compiler(>=5.4) && $AsyncAwait | ||
|
||
import Foundation | ||
import NIO | ||
import NIOHTTP1 | ||
import SmokeHTTPClient | ||
|
||
public extension HTTPOperationsClient { | ||
|
||
/** | ||
Submits a request that will return a response body to this client asynchronously. | ||
|
||
- Parameters: | ||
- endpointPath: The endpoint path for this request. | ||
- httpMethod: The http method to use for this request. | ||
- input: the input body data to send with this request. | ||
- invocationContext: context to use for this invocation. | ||
- retryConfiguration: the retry configuration for this request. | ||
- retryOnError: function that should return if the provided error is retryable. | ||
- Returns: the response body. | ||
- Throws: If an error occurred during the request. | ||
*/ | ||
func executeRetriableWithOutput<InputType, OutputType, | ||
InvocationReportingType: HTTPClientInvocationReporting, HandlerDelegateType: HTTPClientInvocationDelegate>( | ||
endpointOverride: URL? = nil, | ||
endpointPath: String, | ||
httpMethod: HTTPMethod, | ||
input: InputType, | ||
invocationContext: HTTPClientInvocationContext<InvocationReportingType, HandlerDelegateType>, | ||
retryConfiguration: HTTPClientRetryConfiguration, | ||
retryOnError: @escaping (HTTPClientError) -> Bool) async throws -> OutputType | ||
where InputType: HTTPRequestInputProtocol, OutputType: HTTPResponseOutputProtocol { | ||
return try await executeAsEventLoopFutureRetriableWithOutput( | ||
endpointOverride: endpointOverride, | ||
endpointPath: endpointPath, | ||
httpMethod: httpMethod, | ||
input: input, | ||
invocationContext: invocationContext, | ||
retryConfiguration: retryConfiguration, | ||
retryOnError: retryOnError).get() | ||
} | ||
} | ||
|
||
#endif |
60 changes: 60 additions & 0 deletions
60
Sources/_SmokeHTTPClientConcurrency/HTTPOperationsClient+executeRetriableWithoutOutput.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,60 @@ | ||
// Copyright 2018-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// A copy of the License is located at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
// | ||
// HTTPOperationsClient+executeRetriableWithoutOutput.swift | ||
// _SmokeHTTPClientConcurrency | ||
// | ||
|
||
#if compiler(>=5.4) && $AsyncAwait | ||
|
||
import Foundation | ||
import NIO | ||
import NIOHTTP1 | ||
import SmokeHTTPClient | ||
|
||
public extension HTTPOperationsClient { | ||
|
||
/** | ||
Submits a request that will not return a response body to this client asynchronously. | ||
|
||
- Parameters: | ||
- endpointPath: The endpoint path for this request. | ||
- httpMethod: The http method to use for this request. | ||
- input: the input body data to send with this request. | ||
- invocationContext: context to use for this invocation. | ||
- retryConfiguration: the retry configuration for this request. | ||
- retryOnError: function that should return if the provided error is retryable. | ||
- Throws: If an error occurred during the request. | ||
*/ | ||
func executeRetriableWithoutOutput<InputType, | ||
InvocationReportingType: HTTPClientInvocationReporting, HandlerDelegateType: HTTPClientInvocationDelegate>( | ||
endpointOverride: URL? = nil, | ||
endpointPath: String, | ||
httpMethod: HTTPMethod, | ||
input: InputType, | ||
invocationContext: HTTPClientInvocationContext<InvocationReportingType, HandlerDelegateType>, | ||
retryConfiguration: HTTPClientRetryConfiguration, | ||
retryOnError: @escaping (HTTPClientError) -> Bool) async throws | ||
where InputType: HTTPRequestInputProtocol { | ||
return try await executeAsEventLoopFutureRetriableWithoutOutput( | ||
endpointOverride: endpointOverride, | ||
endpointPath: endpointPath, | ||
httpMethod: httpMethod, | ||
input: input, | ||
invocationContext: invocationContext, | ||
retryConfiguration: retryConfiguration, | ||
retryOnError: retryOnError).get() | ||
} | ||
} | ||
|
||
#endif |
56 changes: 56 additions & 0 deletions
56
Sources/_SmokeHTTPClientConcurrency/HTTPOperationsClient+executeWithOutput.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,56 @@ | ||
// Copyright 2018-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// A copy of the License is located at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
// | ||
// HTTPOperationsClient+executeWithOutput.swift | ||
// _SmokeHTTPClientConcurrency | ||
// | ||
|
||
#if compiler(>=5.4) && $AsyncAwait | ||
|
||
import Foundation | ||
import NIO | ||
import NIOHTTP1 | ||
import SmokeHTTPClient | ||
|
||
public extension HTTPOperationsClient { | ||
|
||
/** | ||
Submits a request that will return a response body to this client asynchronously. | ||
|
||
- Parameters: | ||
- endpointPath: The endpoint path for this request. | ||
- httpMethod: The http method to use for this request. | ||
- input: the input body data to send with this request. | ||
- completion: Completion handler called with the response body or any error. | ||
- invocationContext: context to use for this invocation. | ||
- Returns: the response body. | ||
- Throws: If an error occurred during the request. | ||
*/ | ||
func executeWithOutput<InputType, OutputType, | ||
InvocationReportingType: HTTPClientInvocationReporting, HandlerDelegateType: HTTPClientInvocationDelegate>( | ||
endpointOverride: URL? = nil, | ||
endpointPath: String, | ||
httpMethod: HTTPMethod, | ||
input: InputType, | ||
invocationContext: HTTPClientInvocationContext<InvocationReportingType, HandlerDelegateType>) async throws -> OutputType | ||
where InputType: HTTPRequestInputProtocol, OutputType: HTTPResponseOutputProtocol { | ||
return try await executeAsEventLoopFutureWithOutput( | ||
endpointOverride: endpointOverride, | ||
endpointPath: endpointPath, | ||
httpMethod: httpMethod, | ||
input: input, | ||
invocationContext: invocationContext).get() | ||
} | ||
} | ||
|
||
#endif |
77 changes: 77 additions & 0 deletions
77
Sources/_SmokeHTTPClientConcurrency/HTTPOperationsClient+executeWithoutOutput.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,77 @@ | ||
// Copyright 2018-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// A copy of the License is located at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
// | ||
// HTTPOperationsClient+executeWithoutOutput.swift | ||
// _SmokeHTTPClientConcurrency | ||
// | ||
|
||
#if compiler(>=5.4) && $AsyncAwait | ||
|
||
import Foundation | ||
import NIO | ||
import NIOHTTP1 | ||
import SmokeHTTPClient | ||
import _Concurrency | ||
|
||
// TODO: Remove this when provided by swift-nio itself | ||
extension EventLoopFuture { | ||
/// Get the value/error from an `EventLoopFuture` in an `async` context. | ||
/// | ||
/// This function can be used to bridge an `EventLoopFuture` into the `async` world. Ie. if you're in an `async` | ||
/// function and want to get the result of this future. | ||
public func get() async throws -> Value { | ||
return try await withUnsafeThrowingContinuation { cont in | ||
self.whenComplete { result in | ||
switch result { | ||
case .success(let value): | ||
cont.resume(returning: value) | ||
case .failure(let error): | ||
cont.resume(throwing: error) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
public extension HTTPOperationsClient { | ||
|
||
/** | ||
Submits a request that will not return a response body to this client asynchronously. | ||
|
||
- Parameters: | ||
- endpointPath: The endpoint path for this request. | ||
- httpMethod: The http method to use for this request. | ||
- input: the input body data to send with this request. | ||
- completion: Completion handler called with an error if one occurs or nil otherwise. | ||
- asyncResponseInvocationStrategy: The invocation strategy for the response from this request. | ||
- invocationContext: context to use for this invocation. | ||
- Throws: If an error occurred during the request. | ||
*/ | ||
func executeWithoutOutput<InputType, | ||
InvocationReportingType: HTTPClientInvocationReporting, HandlerDelegateType: HTTPClientInvocationDelegate>( | ||
endpointOverride: URL? = nil, | ||
endpointPath: String, | ||
httpMethod: HTTPMethod, | ||
input: InputType, | ||
invocationContext: HTTPClientInvocationContext<InvocationReportingType, HandlerDelegateType>) async throws | ||
where InputType: HTTPRequestInputProtocol { | ||
return try await executeAsEventLoopFutureWithoutOutput( | ||
endpointOverride: endpointOverride, | ||
endpointPath: endpointPath, | ||
httpMethod: httpMethod, | ||
input: input, | ||
invocationContext: invocationContext).get() | ||
} | ||
} | ||
|
||
#endif |