-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: finish interceptors migration #1631
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it weird that we're naming this middleware even though its acting as an interceptor? or is it actually a middleware we just don't need the middleware protocol anymore?