Skip to content

Commit

Permalink
Merge pull request #79 from amzn/async_await_support
Browse files Browse the repository at this point in the history
Add initial async/await support.
  • Loading branch information
tachyonics authored Apr 14, 2021
2 parents f694868 + 3cbff4a commit ab695d0
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ matrix:
services: docker
env: DOCKER_IMAGE_TAG=swiftlang/swift:nightly-5.4-amazonlinux2 ONLY_RUN_SWIFT_LINT=no USE_APT_GET=no

# Verify against nightly of Swift mainline
- os: linux
dist: xenial
sudo: required
services: docker
env: DOCKER_IMAGE_TAG=swiftlang/swift:nightly-amazonlinux2 ONLY_RUN_SWIFT_LINT=no USE_APT_GET=no

# Use a docker image that contains the SwiftLint executable the verify the code against the linter.
- os: linux
dist: xenial
Expand Down
16 changes: 8 additions & 8 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
"repositoryURL": "https://github.com/swift-server/async-http-client.git",
"state": {
"branch": null,
"revision": "a72c5adce3986ff6b5092ae0464a8f2675087860",
"version": "1.2.3"
"revision": "0dda95cffcdf3d96ca551e5701efd1661cf31374",
"version": "1.2.5"
}
},
{
"package": "swift-log",
"repositoryURL": "https://github.com/apple/swift-log.git",
"state": {
"branch": null,
"revision": "12d3a8651d32295794a850307f77407f95b8c881",
"version": "1.4.1"
"revision": "5d66f7ba25daf4f94100e7022febf3c75e37a6c7",
"version": "1.4.2"
}
},
{
Expand All @@ -33,8 +33,8 @@
"repositoryURL": "https://github.com/apple/swift-nio.git",
"state": {
"branch": null,
"revision": "6d3ca7e54e06a69d0f2612c2ce8bb8b7319085a4",
"version": "2.26.0"
"revision": "3be4e0980075de10a4bc8dee07491d49175cfd7a",
"version": "2.27.0"
}
},
{
Expand All @@ -51,8 +51,8 @@
"repositoryURL": "https://github.com/apple/swift-nio-ssl.git",
"state": {
"branch": null,
"revision": "4c933e955b8797f5a5a90bd2a0fb411fdb11bb94",
"version": "2.10.3"
"revision": "07c160b8724ee53a4b776328122be6338ff12bf2",
"version": "2.11.0"
}
},
{
Expand Down
7 changes: 7 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ let package = Package(
.library(
name: "SmokeHTTPClient",
targets: ["SmokeHTTPClient"]),
.library(
name: "_SmokeHTTPClientConcurrency",
targets: ["_SmokeHTTPClientConcurrency"]),
.library(
name: "QueryCoding",
targets: ["QueryCoding"]),
Expand Down Expand Up @@ -52,6 +55,10 @@ let package = Package(
.product(name: "NIOSSL", package: "swift-nio-ssl"),
.product(name: "AsyncHTTPClient", package: "async-http-client"),
]),
.target(
name: "_SmokeHTTPClientConcurrency", dependencies: [
.target(name: "SmokeHTTPClient"),
]),
.target(
name: "QueryCoding", dependencies: [
.target(name: "ShapeCoding"),
Expand Down
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
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
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
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

0 comments on commit ab695d0

Please sign in to comment.