-
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: Support RPCv2 CBOR wire protocol (#1850)
- Loading branch information
Showing
22 changed files
with
350 additions
and
26 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
...time/Sources/AWSClientRuntime/Middlewares/RpcV2CborValidateResponseHeaderMiddleware.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,47 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import ClientRuntime | ||
import SmithyHTTPAPI | ||
|
||
public struct CborValidateResponseHeaderMiddleware<Input, Output> { | ||
public let id: Swift.String = "CborValidateResponseHeaderMiddleware" | ||
|
||
public init() {} | ||
} | ||
|
||
public enum ServiceResponseError: Error { | ||
case missingHeader(String) | ||
case badHeaderValue(String) | ||
} | ||
|
||
extension CborValidateResponseHeaderMiddleware: Interceptor { | ||
|
||
public typealias InputType = Input | ||
public typealias OutputType = Output | ||
public typealias RequestType = HTTPRequest | ||
public typealias ResponseType = HTTPResponse | ||
|
||
public func readBeforeDeserialization( | ||
context: some BeforeDeserialization<InputType, RequestType, ResponseType> | ||
) async throws { | ||
let response = context.getResponse() | ||
let smithyProtocolHeader = response.headers.value(for: "smithy-protocol") | ||
|
||
guard let smithyProtocolHeader else { | ||
throw ServiceResponseError.missingHeader( | ||
"smithy-protocol header is missing from a response over RpcV2 Cbor!" | ||
) | ||
} | ||
|
||
guard smithyProtocolHeader == "rpc-v2-cbor" else { | ||
throw ServiceResponseError.badHeaderValue( | ||
"smithy-protocol header is set to \(smithyProtocolHeader) instead of expected value rpc-v2-cbor" | ||
) | ||
} | ||
} | ||
} |
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
65 changes: 65 additions & 0 deletions
65
...s/Core/AWSClientRuntime/Sources/AWSClientRuntime/Protocols/RpcV2Cbor/RpcV2CborError.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,65 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import protocol ClientRuntime.BaseError | ||
import enum ClientRuntime.BaseErrorDecodeError | ||
import class SmithyHTTPAPI.HTTPResponse | ||
@_spi(SmithyReadWrite) import class SmithyCBOR.Reader | ||
|
||
public struct RpcV2CborError: BaseError { | ||
public let code: String | ||
public let message: String? | ||
public let requestID: String? | ||
@_spi(SmithyReadWrite) public var errorBodyReader: Reader { responseReader } | ||
|
||
public let httpResponse: HTTPResponse | ||
private let responseReader: Reader | ||
|
||
@_spi(SmithyReadWrite) | ||
public init(httpResponse: HTTPResponse, responseReader: Reader, noErrorWrapping: Bool, code: String? = nil) throws { | ||
switch responseReader.cborValue { | ||
case .map(let errorDetails): | ||
if case let .text(errorCode) = errorDetails["__type"] { | ||
self.code = sanitizeErrorType(errorCode) | ||
} else { | ||
self.code = "UnknownError" | ||
} | ||
|
||
if case let .text(errorMessage) = errorDetails["Message"] { | ||
self.message = errorMessage | ||
} else { | ||
self.message = nil | ||
} | ||
default: | ||
self.code = "UnknownError" | ||
self.message = nil | ||
} | ||
|
||
self.httpResponse = httpResponse | ||
self.responseReader = responseReader | ||
self.requestID = nil | ||
} | ||
} | ||
|
||
// support awsQueryCompatible trait | ||
extension RpcV2CborError { | ||
@_spi(SmithyReadWrite) | ||
public static func makeQueryCompatibleError( | ||
httpResponse: HTTPResponse, | ||
responseReader: Reader, | ||
noErrorWrapping: Bool, | ||
errorDetails: String? | ||
) throws -> RpcV2CborError { | ||
let errorCode = try AwsQueryCompatibleErrorDetails.parse(errorDetails).code | ||
return try RpcV2CborError( | ||
httpResponse: httpResponse, | ||
responseReader: responseReader, | ||
noErrorWrapping: noErrorWrapping, | ||
code: errorCode | ||
) | ||
} | ||
} |
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
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
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.