|
| 1 | +// Code generated by smithy-swift-codegen. DO NOT EDIT! |
| 2 | + |
| 3 | +import AWSClientRuntime |
| 4 | +import ClientRuntime |
| 5 | + |
| 6 | +public struct EndpointParams { |
| 7 | + /// Override the endpoint used to send this request |
| 8 | + public let endpoint: Swift.String? |
| 9 | + /// The AWS region used to dispatch the request. |
| 10 | + public let region: Swift.String |
| 11 | + /// When true, use the dual-stack endpoint. If the configured endpoint does not support dual-stack, dispatching the request MAY return an error. |
| 12 | + public let useDualStack: Swift.Bool |
| 13 | + /// When true, send this request to the FIPS-compliant regional endpoint. If the configured endpoint does not have a FIPS compliant endpoint, dispatching the request will return an error. |
| 14 | + public let useFIPS: Swift.Bool |
| 15 | + |
| 16 | + public init( |
| 17 | + endpoint: Swift.String? = nil, |
| 18 | + region: Swift.String, |
| 19 | + useDualStack: Swift.Bool = false, |
| 20 | + useFIPS: Swift.Bool = false |
| 21 | + ) |
| 22 | + { |
| 23 | + self.endpoint = endpoint |
| 24 | + self.region = region |
| 25 | + self.useDualStack = useDualStack |
| 26 | + self.useFIPS = useFIPS |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +public protocol EndpointResolver { |
| 31 | + func resolve(params: EndpointParams) throws -> ClientRuntime.Endpoint |
| 32 | +} |
| 33 | + |
| 34 | +public struct DefaultEndpointResolver: EndpointResolver { |
| 35 | + |
| 36 | + private let engine: AWSClientRuntime.AWSEndpointsRuleEngine |
| 37 | + private let partitions = "{\"version\":\"1.1\",\"partitions\":[{\"id\":\"aws\",\"regionRegex\":\"^(us|eu|ap|sa|ca|me|af)-\\\\w+-\\\\d+$\",\"regions\":{\"af-south-1\":{},\"ap-east-1\":{},\"ap-northeast-1\":{},\"ap-northeast-2\":{},\"ap-northeast-3\":{},\"ap-south-1\":{},\"ap-southeast-1\":{},\"ap-southeast-2\":{},\"ap-southeast-3\":{},\"ca-central-1\":{},\"eu-central-1\":{},\"eu-north-1\":{},\"eu-south-1\":{},\"eu-west-1\":{},\"eu-west-2\":{},\"eu-west-3\":{},\"me-central-1\":{},\"me-south-1\":{},\"sa-east-1\":{},\"us-east-1\":{},\"us-east-2\":{},\"us-west-1\":{},\"us-west-2\":{},\"aws-global\":{}},\"outputs\":{\"name\":\"aws\",\"dnsSuffix\":\"amazonaws.com\",\"dualStackDnsSuffix\":\"api.aws\",\"supportsFIPS\":true,\"supportsDualStack\":true}},{\"id\":\"aws-us-gov\",\"regionRegex\":\"^us\\\\-gov\\\\-\\\\w+\\\\-\\\\d+$\",\"regions\":{\"us-gov-west-1\":{},\"us-gov-east-1\":{},\"aws-us-gov-global\":{}},\"outputs\":{\"name\":\"aws-us-gov\",\"dnsSuffix\":\"amazonaws.com\",\"dualStackDnsSuffix\":\"api.aws\",\"supportsFIPS\":true,\"supportsDualStack\":true}},{\"id\":\"aws-cn\",\"regionRegex\":\"^cn\\\\-\\\\w+\\\\-\\\\d+$\",\"regions\":{\"cn-north-1\":{},\"cn-northwest-1\":{},\"aws-cn-global\":{}},\"outputs\":{\"name\":\"aws-cn\",\"dnsSuffix\":\"amazonaws.com.cn\",\"dualStackDnsSuffix\":\"api.amazonwebservices.com.cn\",\"supportsFIPS\":true,\"supportsDualStack\":true}},{\"id\":\"aws-iso\",\"regionRegex\":\"^us\\\\-iso\\\\-\\\\w+\\\\-\\\\d+$\",\"outputs\":{\"name\":\"aws-iso\",\"dnsSuffix\":\"c2s.ic.gov\",\"supportsFIPS\":true,\"supportsDualStack\":false,\"dualStackDnsSuffix\":\"c2s.ic.gov\"},\"regions\":{\"us-iso-east-1\":{},\"us-iso-west-1\":{},\"aws-iso-global\":{}}},{\"id\":\"aws-iso-b\",\"regionRegex\":\"^us\\\\-isob\\\\-\\\\w+\\\\-\\\\d+$\",\"outputs\":{\"name\":\"aws-iso-b\",\"dnsSuffix\":\"sc2s.sgov.gov\",\"supportsFIPS\":true,\"supportsDualStack\":false,\"dualStackDnsSuffix\":\"sc2s.sgov.gov\"},\"regions\":{\"us-isob-east-1\":{},\"aws-iso-b-global\":{}}}]}" |
| 38 | + private let ruleSet = "{\"version\":\"1.0\",\"parameters\":{\"Region\":{\"builtIn\":\"AWS::Region\",\"required\":true,\"documentation\":\"The AWS region used to dispatch the request.\",\"type\":\"String\"},\"UseDualStack\":{\"builtIn\":\"AWS::UseDualStack\",\"required\":true,\"default\":false,\"documentation\":\"When true, use the dual-stack endpoint. If the configured endpoint does not support dual-stack, dispatching the request MAY return an error.\",\"type\":\"Boolean\"},\"UseFIPS\":{\"builtIn\":\"AWS::UseFIPS\",\"required\":true,\"default\":false,\"documentation\":\"When true, send this request to the FIPS-compliant regional endpoint. If the configured endpoint does not have a FIPS compliant endpoint, dispatching the request will return an error.\",\"type\":\"Boolean\"},\"Endpoint\":{\"builtIn\":\"SDK::Endpoint\",\"required\":false,\"documentation\":\"Override the endpoint used to send this request\",\"type\":\"String\"}},\"rules\":[{\"conditions\":[{\"fn\":\"aws.partition\",\"argv\":[{\"ref\":\"Region\"}],\"assign\":\"PartitionResult\"}],\"type\":\"tree\",\"rules\":[{\"conditions\":[{\"fn\":\"isSet\",\"argv\":[{\"ref\":\"Endpoint\"}]},{\"fn\":\"parseURL\",\"argv\":[{\"ref\":\"Endpoint\"}],\"assign\":\"url\"}],\"type\":\"tree\",\"rules\":[{\"conditions\":[{\"fn\":\"booleanEquals\",\"argv\":[{\"ref\":\"UseFIPS\"},true]}],\"error\":\"Invalid Configuration: FIPS and custom endpoint are not supported\",\"type\":\"error\"},{\"conditions\":[],\"type\":\"tree\",\"rules\":[{\"conditions\":[{\"fn\":\"booleanEquals\",\"argv\":[{\"ref\":\"UseDualStack\"},true]}],\"error\":\"Invalid Configuration: Dualstack and custom endpoint are not supported\",\"type\":\"error\"},{\"conditions\":[],\"endpoint\":{\"url\":{\"ref\":\"Endpoint\"},\"properties\":{},\"headers\":{}},\"type\":\"endpoint\"}]}]},{\"conditions\":[{\"fn\":\"booleanEquals\",\"argv\":[{\"ref\":\"UseFIPS\"},true]},{\"fn\":\"booleanEquals\",\"argv\":[{\"ref\":\"UseDualStack\"},true]}],\"type\":\"tree\",\"rules\":[{\"conditions\":[{\"fn\":\"booleanEquals\",\"argv\":[true,{\"fn\":\"getAttr\",\"argv\":[{\"ref\":\"PartitionResult\"},\"supportsFIPS\"]}]},{\"fn\":\"booleanEquals\",\"argv\":[true,{\"fn\":\"getAttr\",\"argv\":[{\"ref\":\"PartitionResult\"},\"supportsDualStack\"]}]}],\"type\":\"tree\",\"rules\":[{\"conditions\":[],\"endpoint\":{\"url\":\"https://arc-zonal-shift-fips.{Region}.{PartitionResult#dualStackDnsSuffix}\",\"properties\":{},\"headers\":{}},\"type\":\"endpoint\"}]},{\"conditions\":[],\"error\":\"FIPS and DualStack are enabled, but this partition does not support one or both\",\"type\":\"error\"}]},{\"conditions\":[{\"fn\":\"booleanEquals\",\"argv\":[{\"ref\":\"UseFIPS\"},true]}],\"type\":\"tree\",\"rules\":[{\"conditions\":[{\"fn\":\"booleanEquals\",\"argv\":[true,{\"fn\":\"getAttr\",\"argv\":[{\"ref\":\"PartitionResult\"},\"supportsFIPS\"]}]}],\"type\":\"tree\",\"rules\":[{\"conditions\":[],\"endpoint\":{\"url\":\"https://arc-zonal-shift-fips.{Region}.{PartitionResult#dnsSuffix}\",\"properties\":{},\"headers\":{}},\"type\":\"endpoint\"}]},{\"conditions\":[],\"error\":\"FIPS is enabled but this partition does not support FIPS\",\"type\":\"error\"}]},{\"conditions\":[{\"fn\":\"booleanEquals\",\"argv\":[{\"ref\":\"UseDualStack\"},true]}],\"type\":\"tree\",\"rules\":[{\"conditions\":[{\"fn\":\"booleanEquals\",\"argv\":[true,{\"fn\":\"getAttr\",\"argv\":[{\"ref\":\"PartitionResult\"},\"supportsDualStack\"]}]}],\"type\":\"tree\",\"rules\":[{\"conditions\":[],\"endpoint\":{\"url\":\"https://arc-zonal-shift.{Region}.{PartitionResult#dualStackDnsSuffix}\",\"properties\":{},\"headers\":{}},\"type\":\"endpoint\"}]},{\"conditions\":[],\"error\":\"DualStack is enabled but this partition does not support DualStack\",\"type\":\"error\"}]},{\"conditions\":[],\"endpoint\":{\"url\":\"https://arc-zonal-shift.{Region}.{PartitionResult#dnsSuffix}\",\"properties\":{},\"headers\":{}},\"type\":\"endpoint\"}]}]}" |
| 39 | + |
| 40 | + public init() throws { |
| 41 | + engine = try AWSClientRuntime.AWSEndpointsRuleEngine(partitions: partitions, ruleSet: ruleSet) |
| 42 | + } |
| 43 | + |
| 44 | + public func resolve(params: EndpointParams) throws -> ClientRuntime.Endpoint { |
| 45 | + let context = try AWSClientRuntime.AWSEndpointsRequestContext() |
| 46 | + try context.add(name: "Endpoint", value: params.endpoint) |
| 47 | + try context.add(name: "Region", value: params.region) |
| 48 | + try context.add(name: "UseDualStack", value: params.useDualStack) |
| 49 | + try context.add(name: "UseFIPS", value: params.useFIPS) |
| 50 | + |
| 51 | + guard let crtResolvedEndpoint = try engine.resolve(context: context) else { |
| 52 | + throw EndpointError.unresolved("Failed to resolved endpoint") |
| 53 | + } |
| 54 | + |
| 55 | + if crtResolvedEndpoint.getType() == .error { |
| 56 | + let error = try crtResolvedEndpoint.getError() |
| 57 | + throw EndpointError.unresolved(error) |
| 58 | + } |
| 59 | + |
| 60 | + guard let url = try crtResolvedEndpoint.getURL() else { |
| 61 | + assertionFailure("This must be a bug in either CRT or the rule engine, if the endpoint is not an error, it must have a url") |
| 62 | + throw EndpointError.unresolved("Failed to resolved endpoint") |
| 63 | + } |
| 64 | + |
| 65 | + let headers = try crtResolvedEndpoint.getHeaders() ?? [:] |
| 66 | + let properties = try crtResolvedEndpoint.getProperties() ?? [:] |
| 67 | + return try Endpoint(urlString: url, headers: Headers(headers), properties: properties) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +public struct EndpointResolverMiddleware<OperationStackOutput: ClientRuntime.HttpResponseBinding, OperationStackError: ClientRuntime.HttpResponseBinding>: ClientRuntime.Middleware { |
| 72 | + public let id: Swift.String = "EndpointResolverMiddleware" |
| 73 | + |
| 74 | + let endpointResolver: EndpointResolver |
| 75 | + |
| 76 | + let endpointParams: EndpointParams |
| 77 | + |
| 78 | + public init(endpointResolver: EndpointResolver, endpointParams: EndpointParams) { |
| 79 | + self.endpointResolver = endpointResolver |
| 80 | + self.endpointParams = endpointParams |
| 81 | + } |
| 82 | + |
| 83 | + public func handle<H>(context: Context, |
| 84 | + input: ClientRuntime.SdkHttpRequestBuilder, |
| 85 | + next: H) async throws -> ClientRuntime.OperationOutput<OperationStackOutput> |
| 86 | + where H: Handler, |
| 87 | + Self.MInput == H.Input, |
| 88 | + Self.MOutput == H.Output, |
| 89 | + Self.Context == H.Context |
| 90 | + { |
| 91 | + let endpoint = try endpointResolver.resolve(params: endpointParams) |
| 92 | + |
| 93 | + let authScheme = endpoint.authScheme(name: "sigv4") |
| 94 | + let awsEndpoint = AWSEndpoint(endpoint: endpoint, signingName: authScheme?["signingName"] as? String, signingRegion: authScheme?["signingRegion"] as? String) |
| 95 | + |
| 96 | + var host = "" |
| 97 | + if let hostOverride = context.getHost() { |
| 98 | + host = hostOverride |
| 99 | + } else { |
| 100 | + host = "\(context.getHostPrefix() ?? "")\(awsEndpoint.endpoint.host)" |
| 101 | + } |
| 102 | + |
| 103 | + if let protocolType = awsEndpoint.endpoint.protocolType { |
| 104 | + input.withProtocol(protocolType) |
| 105 | + } |
| 106 | + |
| 107 | + var updatedContext = context |
| 108 | + if let signingRegion = awsEndpoint.signingRegion { |
| 109 | + updatedContext.attributes.set(key: HttpContext.signingRegion, value: signingRegion) |
| 110 | + } |
| 111 | + if let signingName = awsEndpoint.signingName { |
| 112 | + updatedContext.attributes.set(key: HttpContext.signingName, value: signingName) |
| 113 | + } |
| 114 | + |
| 115 | + if let headers = endpoint.headers { |
| 116 | + input.withHeaders(headers) |
| 117 | + } |
| 118 | + |
| 119 | + input.withMethod(context.getMethod()) |
| 120 | + .withHost(host) |
| 121 | + .withPort(awsEndpoint.endpoint.port) |
| 122 | + .withPath(awsEndpoint.endpoint.path.appendingPathComponent(context.getPath())) |
| 123 | + .withHeader(name: "Host", value: host) |
| 124 | + |
| 125 | + return try await next.handle(context: updatedContext, input: input) |
| 126 | + } |
| 127 | + |
| 128 | + public typealias MInput = ClientRuntime.SdkHttpRequestBuilder |
| 129 | + public typealias MOutput = ClientRuntime.OperationOutput<OperationStackOutput> |
| 130 | + public typealias Context = ClientRuntime.HttpContext |
| 131 | +} |
0 commit comments