Skip to content

Commit 3a33d2d

Browse files
authored
feat: refactor everything to be async (#389)
1 parent 158bf51 commit 3a33d2d

File tree

81 files changed

+630
-1070
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+630
-1070
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88
workflow_dispatch:
99

1010
env:
11-
BUILDER_VERSION: v0.8.19
11+
BUILDER_VERSION: v0.9.11
1212
BUILDER_SOURCE: releases
1313
# host owned by CRT team to host aws-crt-builder releases. Contact their on-call with any issues
1414
BUILDER_HOST: https://d19elf31gohf1l.cloudfront.net
@@ -22,6 +22,8 @@ env:
2222
jobs:
2323
downstream:
2424
runs-on: macos-11
25+
env:
26+
DEVELOPER_DIR: /Applications/Xcode_13.2.1.app
2527
steps:
2628
- name: Checkout sources
2729
uses: actions/checkout@v2

Packages/ClientRuntime/Sources/Middleware/AnyHandler.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
// SPDX-License-Identifier: Apache-2.0.
33

44
/// Type erased Handler
5-
public struct AnyHandler<MInput, MOutput, Context: MiddlewareContext, MError: Error>: Handler {
6-
private let _handle: (Context, MInput) -> Result<MOutput, MError>
5+
public struct AnyHandler<MInput, MOutput, Context: MiddlewareContext>: Handler {
6+
private let _handle: (Context, MInput) async throws -> MOutput
77

88
public init<H: Handler> (_ realHandler: H)
9-
where H.Input == MInput, H.Output == MOutput, H.Context == Context, H.MiddlewareError == MError {
10-
if let alreadyErased = realHandler as? AnyHandler<MInput, MOutput, Context, MError> {
9+
where H.Input == MInput, H.Output == MOutput, H.Context == Context {
10+
if let alreadyErased = realHandler as? AnyHandler<MInput, MOutput, Context> {
1111
self = alreadyErased
1212
return
1313
}
1414
self._handle = realHandler.handle
1515
}
1616

17-
public func handle(context: Context, input: MInput) -> Result<MOutput, MError> {
18-
return _handle(context, input)
17+
public func handle(context: Context, input: MInput) async throws -> MOutput {
18+
return try await _handle(context, input)
1919
}
2020
}

Packages/ClientRuntime/Sources/Middleware/AnyMiddleware.swift

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
// SPDX-License-Identifier: Apache-2.0.
33

44
/// type erase the Middleware protocol
5-
public struct AnyMiddleware<MInput, MOutput, Context: MiddlewareContext, MError: Error>: Middleware {
5+
public struct AnyMiddleware<MInput, MOutput, Context: MiddlewareContext>: Middleware {
66

7-
private let _handle: (Context, MInput, AnyHandler<MInput, MOutput, Context, MError>) -> Result<MOutput, MError>
7+
private let _handle: (Context, MInput, AnyHandler<MInput, MOutput, Context>) async throws -> MOutput
88

99
public var id: String
1010

1111
public init<M: Middleware>(_ realMiddleware: M)
12-
where M.MInput == MInput, M.MOutput == MOutput, M.Context == Context, M.MError == MError {
13-
if let alreadyErased = realMiddleware as? AnyMiddleware<MInput, MOutput, Context, MError> {
12+
where M.MInput == MInput, M.MOutput == MOutput, M.Context == Context {
13+
if let alreadyErased = realMiddleware as? AnyMiddleware<MInput, MOutput, Context> {
1414
self = alreadyErased
1515
return
1616
}
@@ -21,20 +21,18 @@ public struct AnyMiddleware<MInput, MOutput, Context: MiddlewareContext, MError:
2121

2222
public init<H: Handler>(handler: H, id: String) where H.Input == MInput,
2323
H.Output == MOutput,
24-
H.Context == Context,
25-
H.MiddlewareError == MError {
24+
H.Context == Context {
2625

2726
self._handle = { context, input, handler in
28-
handler.handle(context: context, input: input)
27+
try await handler.handle(context: context, input: input)
2928
}
3029
self.id = id
3130
}
3231

33-
public func handle<H: Handler>(context: Context, input: MInput, next: H) -> Result<MOutput, MError>
32+
public func handle<H: Handler>(context: Context, input: MInput, next: H) async throws -> MOutput
3433
where H.Input == MInput,
3534
H.Output == MOutput,
36-
H.Context == Context,
37-
H.MiddlewareError == MError {
38-
return _handle(context, input, next.eraseToAnyHandler())
35+
H.Context == Context {
36+
return try await _handle(context, input, next.eraseToAnyHandler())
3937
}
4038
}

Packages/ClientRuntime/Sources/Middleware/ComposedHandler.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,28 @@
22
// SPDX-License-Identifier: Apache-2.0.
33

44
// handler chain, used to decorate a handler with middleware
5-
public struct ComposedHandler<MInput, MOutput, Context: MiddlewareContext, MError: Error> {
5+
public struct ComposedHandler<MInput, MOutput, Context: MiddlewareContext> {
66
// the next handler to call
7-
let next: AnyHandler<MInput, MOutput, Context, MError>
7+
let next: AnyHandler<MInput, MOutput, Context>
88

99
// the middleware decorating 'next'
10-
let with: AnyMiddleware<MInput, MOutput, Context, MError>
10+
let with: AnyMiddleware<MInput, MOutput, Context>
1111

1212
public init<H: Handler, M: Middleware> (_ realNext: H, _ realWith: M)
1313
where H.Input == MInput,
1414
H.Output == MOutput,
1515
M.MInput == MInput,
1616
M.MOutput == MOutput,
1717
M.Context == Context,
18-
H.Context == Context,
19-
H.MiddlewareError == MError,
20-
M.MError == MError {
18+
H.Context == Context {
2119

2220
self.next = AnyHandler(realNext)
2321
self.with = AnyMiddleware(realWith)
2422
}
2523
}
2624

2725
extension ComposedHandler: Handler {
28-
public func handle(context: Context, input: MInput) -> Result<MOutput, MError> {
29-
return with.handle(context: context, input: input, next: next)
26+
public func handle(context: Context, input: MInput) async throws -> MOutput {
27+
return try await with.handle(context: context, input: input, next: next)
3028
}
3129
}

Packages/ClientRuntime/Sources/Middleware/Handler.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ public protocol Handler {
55
associatedtype Input
66
associatedtype Output
77
associatedtype Context: MiddlewareContext
8-
associatedtype MiddlewareError: Error
98

10-
func handle(context: Context, input: Input) -> Result<Output, MiddlewareError>
9+
func handle(context: Context, input: Input) async throws -> Output
1110
}
1211

1312
extension Handler {
14-
public func eraseToAnyHandler() -> AnyHandler<Input, Output, Context, MiddlewareError> {
13+
public func eraseToAnyHandler() -> AnyHandler<Input, Output, Context> {
1514
return AnyHandler(self)
1615
}
1716
}

Packages/ClientRuntime/Sources/Middleware/Middleware.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@ public protocol Middleware {
55
associatedtype MInput
66
associatedtype MOutput
77
associatedtype Context: MiddlewareContext
8-
associatedtype MError: Error
98

109
/// The middleware ID
1110
var id: String { get }
1211

1312
func handle<H: Handler>(context: Context,
1413
input: MInput,
15-
next: H) -> Result<MOutput, MError>
16-
where H.Input == MInput, H.Output == MOutput, H.Context == Context, H.MiddlewareError == MError
14+
next: H) async throws -> MOutput
15+
where H.Input == MInput, H.Output == MOutput, H.Context == Context
1716
}
1817

1918
extension Middleware {
20-
public func eraseToAnyMiddleware() -> AnyMiddleware<MInput, MOutput, Context, MError> {
19+
public func eraseToAnyMiddleware() -> AnyMiddleware<MInput, MOutput, Context> {
2120
return AnyMiddleware(self)
2221
}
2322
}

Packages/ClientRuntime/Sources/Middleware/MiddlewareFunction.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33

44
public typealias MiddlewareFunction<MInput,
55
MOutput,
6-
Context: MiddlewareContext,
7-
MError: Error> = (Context,
6+
Context: MiddlewareContext> = (Context,
87
MInput,
98
AnyHandler<MInput,
109
MOutput,
11-
Context,
12-
MError>) -> Result<MOutput, MError>
10+
Context>) async throws -> MOutput

Packages/ClientRuntime/Sources/Middleware/MiddlewareStep.swift

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,36 @@
44
/// An instance of MiddlewareStep will be contained in the operation stack, and recognized as a single
55
/// step (initialize, build, etc..) that contains an ordered list of middlewares. This class is
66
/// responsible for ordering these middlewares so that they are executed in the correct order.
7-
public struct MiddlewareStep<StepContext: MiddlewareContext, Input, Output, MError: Error>: Middleware {
7+
public struct MiddlewareStep<StepContext: MiddlewareContext, Input, Output>: Middleware {
88
public typealias Context = StepContext
99
public typealias MInput = Input
1010
public typealias MOutput = Output
11-
public typealias MError = MError
1211

1312
var orderedMiddleware: OrderedGroup<MInput,
1413
MOutput,
15-
Context,
16-
MError> = OrderedGroup<MInput, MOutput, Context, MError>()
14+
Context> = OrderedGroup<MInput, MOutput, Context>()
1715

1816
public let id: String
1917

2018
public init(id: String) {
2119
self.id = id
2220
}
2321

24-
func get(id: String) -> AnyMiddleware<MInput, MOutput, Context, MError>? {
22+
func get(id: String) -> AnyMiddleware<MInput, MOutput, Context>? {
2523
return orderedMiddleware.get(id: id)
2624
}
2725

2826
/// This execute will execute the stack and use your next as the last closure in the chain
2927
public func handle<H: Handler>(context: Context,
3028
input: MInput,
31-
next: H) -> Result<MOutput, MError>
32-
where H.Input == MInput, H.Output == MOutput, H.Context == Context, H.MiddlewareError == MError {
29+
next: H) async throws -> MOutput
30+
where H.Input == MInput, H.Output == MOutput, H.Context == Context {
3331

3432
var handler = next.eraseToAnyHandler()
3533
let order = orderedMiddleware.orderedItems
3634

3735
guard !order.isEmpty else {
38-
return handler.handle(context: context, input: input)
36+
return try await handler.handle(context: context, input: input)
3937
}
4038
let numberOfMiddlewares = order.count
4139
let reversedCollection = (0...(numberOfMiddlewares-1)).reversed()
@@ -44,12 +42,11 @@ public struct MiddlewareStep<StepContext: MiddlewareContext, Input, Output, MErr
4442
handler = composedHandler.eraseToAnyHandler()
4543
}
4644

47-
let result = handler.handle(context: context, input: input)
48-
return result
45+
return try await handler.handle(context: context, input: input)
4946
}
5047

5148
public mutating func intercept<M: Middleware>(position: Position, middleware: M)
52-
where M.MInput == MInput, M.MOutput == MOutput, M.Context == Context, M.MError == MError {
49+
where M.MInput == MInput, M.MOutput == MOutput, M.Context == Context {
5350
orderedMiddleware.add(middleware: middleware.eraseToAnyMiddleware(), position: position)
5451
}
5552

@@ -61,7 +58,7 @@ public struct MiddlewareStep<StepContext: MiddlewareContext, Input, Output, MErr
6158
///
6259
public mutating func intercept(position: Position,
6360
id: String,
64-
middleware: @escaping MiddlewareFunction<MInput, MOutput, Context, MError>) {
61+
middleware: @escaping MiddlewareFunction<MInput, MOutput, Context>) {
6562
let middleware = WrappedMiddleware(middleware, id: id)
6663
orderedMiddleware.add(middleware: middleware.eraseToAnyMiddleware(), position: position)
6764
}

Packages/ClientRuntime/Sources/Middleware/NoopHandler.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77

8-
public struct NoopHandler<Output: HttpResponseBinding, OutputError: HttpResponseBinding>: Handler {
9-
public init() {
10-
}
11-
public func handle(context: HttpContext, input: SdkHttpRequest) -> Result<OperationOutput<Output>, SdkError<OutputError>> {
12-
let output = OperationOutput<Output>(httpResponse: HttpResponse())
13-
return .success(output)
8+
public struct NoopHandler<Output: HttpResponseBinding>: Handler {
9+
public init() {}
10+
11+
public func handle(context: HttpContext, input: SdkHttpRequest) async throws -> OperationOutput<Output> {
12+
return OperationOutput<Output>(httpResponse: HttpResponse())
1413
}
1514
}

Packages/ClientRuntime/Sources/Middleware/OperationStack.swift

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,76 +7,68 @@ public struct OperationStack<OperationStackInput,
77

88
/// returns the unique id for the operation stack as middleware
99
public var id: String
10-
public var initializeStep: InitializeStep<OperationStackInput, OperationStackOutput, OperationStackError>
11-
public var serializeStep: SerializeStep<OperationStackInput, OperationStackOutput, OperationStackError>
12-
public var buildStep: BuildStep<OperationStackOutput, OperationStackError>
13-
public var finalizeStep: FinalizeStep<OperationStackOutput, OperationStackError>
14-
public var deserializeStep: DeserializeStep<OperationStackOutput, OperationStackError>
10+
public var initializeStep: InitializeStep<OperationStackInput, OperationStackOutput>
11+
public var serializeStep: SerializeStep<OperationStackInput, OperationStackOutput>
12+
public var buildStep: BuildStep<OperationStackOutput>
13+
public var finalizeStep: FinalizeStep<OperationStackOutput>
14+
public var deserializeStep: DeserializeStep<OperationStackOutput>
1515

1616
public init(id: String) {
1717
self.id = id
1818
self.initializeStep = InitializeStep<OperationStackInput,
19-
OperationStackOutput,
20-
OperationStackError>(id: InitializeStepId)
19+
OperationStackOutput>(id: InitializeStepId)
2120
self.serializeStep = SerializeStep<OperationStackInput,
22-
OperationStackOutput,
23-
OperationStackError>(id: SerializeStepId)
24-
self.buildStep = BuildStep<OperationStackOutput,
25-
OperationStackError>(id: BuildStepId)
26-
self.finalizeStep = FinalizeStep<OperationStackOutput,
27-
OperationStackError>(id: FinalizeStepId)
28-
self.deserializeStep = DeserializeStep<OperationStackOutput,
29-
OperationStackError>(id: DeserializeStepId)
21+
OperationStackOutput>(id: SerializeStepId)
22+
self.buildStep = BuildStep<OperationStackOutput>(id: BuildStepId)
23+
self.finalizeStep = FinalizeStep<OperationStackOutput>(id: FinalizeStepId)
24+
self.deserializeStep = DeserializeStep<OperationStackOutput>(id: DeserializeStepId)
3025

3126
}
3227

3328
/// This execute will execute the stack and use your next as the last closure in the chain
3429
public func handleMiddleware<H: Handler>(context: HttpContext,
3530
input: OperationStackInput,
36-
next: H) -> SdkResult<OperationStackOutput, OperationStackError>
31+
next: H) async throws -> OperationStackOutput
3732
where H.Input == SdkHttpRequest,
3833
H.Output == OperationOutput<OperationStackOutput>,
39-
H.Context == HttpContext,
40-
H.MiddlewareError == SdkError<OperationStackError> {
41-
42-
let deserialize = compose(next: DeserializeStepHandler(handler: next), with: deserializeStep)
43-
let finalize = compose(next: FinalizeStepHandler(handler: deserialize), with: finalizeStep)
44-
let build = compose(next: BuildStepHandler(handler: finalize), with: buildStep)
45-
let serialize = compose(next: SerializeStepHandler(handler: build), with: serializeStep)
46-
let initialize = compose(next: InitializeStepHandler(handler: serialize), with: initializeStep)
47-
48-
let result = initialize.handle(context: context, input: input)
49-
return result.flatMap { operationOutput in
50-
return .success(operationOutput.output!)
51-
}
52-
}
34+
H.Context == HttpContext {
35+
36+
let deserialize = compose(next: DeserializeStepHandler(handler: next), with: deserializeStep)
37+
let finalize = compose(next: FinalizeStepHandler(handler: deserialize), with: finalizeStep)
38+
let build = compose(next: BuildStepHandler(handler: finalize), with: buildStep)
39+
let serialize = compose(next: SerializeStepHandler(handler: build), with: serializeStep)
40+
let initialize = compose(next: InitializeStepHandler(handler: serialize), with: initializeStep)
41+
42+
let result = try await initialize.handle(context: context, input: input)
43+
guard let output = result.output else {
44+
throw ClientError.unknownError("Something went terribly wrong where the output was not set on the response. Please open a ticket with us at https://github.com/awslabs/aws-sdk-swift")
45+
}
46+
return output
47+
}
5348

5449
mutating public func presignedRequest<H: Handler>(context: HttpContext,
5550
input: OperationStackInput,
56-
next: H) -> SdkHttpRequestBuilder?
51+
next: H) async throws -> SdkHttpRequestBuilder?
5752
where H.Input == SdkHttpRequest,
5853
H.Output == OperationOutput<OperationStackOutput>,
59-
H.Context == HttpContext,
60-
H.MiddlewareError == SdkError<OperationStackError> {
54+
H.Context == HttpContext {
6155
var builder: SdkHttpRequestBuilder?
6256
self.finalizeStep.intercept(position: .after,
63-
middleware: PresignerShim(handler: { buildInMiddleware in
57+
middleware: PresignerShim<OperationStackOutput, OperationStackError>(handler: { buildInMiddleware in
6458
builder = buildInMiddleware
6559
}))
66-
_ = handleMiddleware(context: context, input: input, next: next)
60+
_ = try await handleMiddleware(context: context, input: input, next: next)
6761
return builder
6862
}
6963

7064
/// Compose (wrap) the handler with the given middleware or essentially build out the linked list of middleware
7165
private func compose<H: Handler, M: Middleware>(next handler: H,
7266
with middlewares: M...) -> AnyHandler<H.Input,
7367
H.Output,
74-
H.Context,
75-
H.MiddlewareError>
68+
H.Context>
7669
where M.MOutput == H.Output,
7770
M.MInput == H.Input,
78-
H.Context == M.Context,
79-
H.MiddlewareError == M.MError {
71+
H.Context == M.Context {
8072
guard !middlewares.isEmpty,
8173
let lastMiddleware = middlewares.last else {
8274
return handler.eraseToAnyHandler()

0 commit comments

Comments
 (0)