-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: finish interceptors migration (#1631)
This commit includes the codegen changes that move all services to use orchestrator + interceptors, instead of the operation stack using middleware. It also removes all references to the old types related to Middleware, in Swift libraries and in the code generator. Since this is already a huge PR, I tried to limit any refactoring, which may have made it harder to review. For example, I didn't change the names of middleware or their file names. This commit also adds two new interceptors, AmzSdkRequestMiddleware and AmzSdkInvocationIdMiddleware, which add corresponding headers to requests. They are added in codegen through a SwiftIntegration. Previously, these interceptors were implemented as a part of RetryMiddleware in smithy-swift. But unlike all other middleware, RetryMiddleware does not have an interceptor implementation - its logic is inlined within Orchestrator - so the logic for adding these amz-sdk headers had to be extracted. Plus, they seem to be aws headers, so I moved them here into the SDK. I also copied the RetryIntegrationTests test, which was testing the headers' functionality. Other than that, there were some codegen changes I made which impacted the ordering that interceptors are rendered into operation bodies, which meant I needed to fix some codegen tests. The order interceptors are added to orchestrator shouldn't matter.
- Loading branch information
1 parent
ee4cf62
commit 46e00d6
Showing
545 changed files
with
509,166 additions
and
226,814 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
38 changes: 38 additions & 0 deletions
38
.../AWSClientRuntime/Sources/AWSClientRuntime/Middlewares/AmzSdkInvocationIdMiddleware.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,38 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import ClientRuntime | ||
import Smithy | ||
import SmithyHTTPAPI | ||
import struct Foundation.UUID | ||
|
||
private let AMZ_SDK_INVOCATION_ID_HEADER = "amz-sdk-invocation-id" | ||
|
||
/// Adds the amz-sdk-invocation-id header to requests. | ||
public struct AmzSdkInvocationIdMiddleware<InputType, OperationStackOutput> { | ||
public var id: String { "AmzSdkInvocationId" } | ||
|
||
// The UUID string used to uniquely identify an API call and all of its subsequent retries. | ||
private let invocationId = UUID().uuidString.lowercased() | ||
|
||
public init() {} | ||
|
||
private func addHeader(builder: HTTPRequestBuilder) { | ||
builder.withHeader(name: AMZ_SDK_INVOCATION_ID_HEADER, value: invocationId) | ||
} | ||
} | ||
|
||
extension AmzSdkInvocationIdMiddleware: HttpInterceptor { | ||
public typealias InputType = InputType | ||
public typealias OutputType = OperationStackOutput | ||
|
||
public func modifyBeforeRetryLoop(context: some MutableRequest<InputType, HTTPRequest>) async throws { | ||
let builder = context.getRequest().toBuilder() | ||
addHeader(builder: builder) | ||
context.updateRequest(updated: builder.build()) | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
.../Core/AWSClientRuntime/Sources/AWSClientRuntime/Middlewares/AmzSdkRequestMiddleware.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,75 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import ClientRuntime | ||
import Smithy | ||
import SmithyHTTPAPI | ||
import class Foundation.DateFormatter | ||
import struct Foundation.Locale | ||
import struct Foundation.TimeInterval | ||
import struct Foundation.TimeZone | ||
import struct Foundation.UUID | ||
|
||
private let AMZ_SDK_REQUEST_HEADER = "amz-sdk-request" | ||
|
||
/// Adds the amz-sdk-request header to requests. | ||
public class AmzSdkRequestMiddleware<InputType, OperationStackOutput> { | ||
public var id: String { "AmzSdkRequest" } | ||
|
||
// Max number of retries configured for retry strategy. | ||
private var maxRetries: Int | ||
private var attempt: Int = 0 | ||
|
||
public init(maxRetries: Int) { | ||
self.maxRetries = maxRetries | ||
} | ||
|
||
private func addHeader(builder: HTTPRequestBuilder, context: Context) { | ||
self.attempt += 1 | ||
|
||
// Only compute ttl after first attempt | ||
if self.attempt == 1 { | ||
builder.withHeader(name: AMZ_SDK_REQUEST_HEADER, value: "attempt=1; max=\(maxRetries)") | ||
} else { | ||
let estimatedSkew = context.estimatedSkew ?? { | ||
context.getLogger()?.info("Estimated skew not found; defaulting to zero.") | ||
return 0 | ||
}() | ||
let socketTimeout = context.socketTimeout ?? { | ||
context.getLogger()?.info("Socket timeout value not found; defaulting to 60 seconds.") | ||
return 60.0 | ||
}() | ||
let ttlDateUTCString = awsGetTTL(now: Date(), estimatedSkew: estimatedSkew, socketTimeout: socketTimeout) | ||
builder.updateHeader( | ||
name: AMZ_SDK_REQUEST_HEADER, | ||
value: "ttl=\(ttlDateUTCString); attempt=\(self.attempt); max=\(maxRetries)" | ||
) | ||
} | ||
} | ||
|
||
} | ||
|
||
extension AmzSdkRequestMiddleware: HttpInterceptor { | ||
public typealias InputType = InputType | ||
public typealias OutputType = OperationStackOutput | ||
|
||
public func modifyBeforeSigning(context: some MutableRequest<InputType, HTTPRequest>) async throws { | ||
let builder = context.getRequest().toBuilder() | ||
addHeader(builder: builder, context: context.getAttributes()) | ||
context.updateRequest(updated: builder.build()) | ||
} | ||
} | ||
|
||
// Calculates & returns TTL datetime in strftime format `YYYYmmddTHHMMSSZ`. | ||
func awsGetTTL(now: Date, estimatedSkew: TimeInterval, socketTimeout: TimeInterval) -> String { | ||
let dateFormatter = DateFormatter() | ||
dateFormatter.dateFormat = "yyyyMMdd'T'HHmmss'Z'" | ||
dateFormatter.locale = Locale(identifier: "en_US_POSIX") | ||
dateFormatter.timeZone = TimeZone(abbreviation: "UTC") | ||
let ttlDate = now.addingTimeInterval(estimatedSkew + socketTimeout) | ||
return dateFormatter.string(from: ttlDate) | ||
} |
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
Oops, something went wrong.