diff --git a/swift/example_code/swift-sdk/http-config/Package.swift b/swift/example_code/swift-sdk/http-config/Package.swift new file mode 100644 index 00000000000..6c70287af94 --- /dev/null +++ b/swift/example_code/swift-sdk/http-config/Package.swift @@ -0,0 +1,47 @@ +// swift-tools-version: 5.9 +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// (swift-tools-version has two lines here because it needs to be the first +// line in the file, but it should also appear in the snippet below) +// +// snippet-start:[swift.cognito-identity-provider.scenario.package] +// swift-tools-version: 5.9 +// +// The swift-tools-version declares the minimum version of Swift required to +// build this package. + +import PackageDescription + +let package = Package( + name: "http-config", + // Let Xcode know the minimum Apple platforms supported. + platforms: [ + .macOS(.v13), + .iOS(.v15) + ], + dependencies: [ + // Dependencies declare other packages that this package depends on. + .package( + url: "https://github.com/awslabs/aws-sdk-swift", + from: "1.0.0"), + .package( + url: "https://github.com/apple/swift-argument-parser.git", + branch: "main" + ) + ], + targets: [ + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products + // from dependencies. + .executableTarget( + name: "cognito-scenario", + dependencies: [ + .product(name: "AWSS3", package: "aws-sdk-swift"), + .product(name: "ArgumentParser", package: "swift-argument-parser") + ], + path: "Sources") + + ] +) +// snippet-end:[swift.cognito-identity-provider.scenario.package] diff --git a/swift/example_code/swift-sdk/http-config/Sources/entry.swift b/swift/example_code/swift-sdk/http-config/Sources/entry.swift new file mode 100644 index 00000000000..f60991ac53f --- /dev/null +++ b/swift/example_code/swift-sdk/http-config/Sources/entry.swift @@ -0,0 +1,88 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// snippet-start:[swift.http-config] +// An example demonstrating how to customize the configuration of the HTTP +// client used by an Amazon Web Services (AWS) service client. + +import ArgumentParser +// snippet-start:[swift.http-config.imports] +import ClientRuntime +import AWSS3 +import SmithyHTTPAPI +import AwsCommonRuntimeKit +// snippet-end:[swift.http-config.imports] + +struct ExampleCommand: ParsableCommand { + @Option(help: "Name of the Amazon Region to use") + var region = "us-east-1" + + static var configuration = CommandConfiguration( + commandName: "http-config", + abstract: """ + Demonstrates how to configure the HTTP client used by an AWS service client. + """, + discussion: """ + """ + ) + + /// Called by ``main()`` to run the bulk of the example. + func runAsync() async throws { + // snippet-start:[swift.http-config.headers] + let config = try await S3Client.S3ClientConfiguration( + region: region, + httpClientConfiguration: HttpClientConfiguration( + defaultHeaders: Headers( + [ + "X-My-Custom-Header": "CustomHeaderValue", + "X-Another-Custom-Header": "AnotherCustomValue" + ] + ) + ) + ) + let s3Client = S3Client(config: config) + // snippet-end:[swift.http-config.headers] + + print("*** Getting list of buckets...") + _ = try await s3Client.listBuckets(input: ListBucketsInput()) + print("*** Success!\n") + + print("*** Getting bucket list with custom timeouts...") + + // snippet-start: [swift.http-config.timeouts] + do { + let config = try await S3Client.S3ClientConfiguration( + region: region, + httpClientConfiguration: HttpClientConfiguration( + connectTimeout: 2, + socketTimeout: 5 + ) + ) + let s3Client = S3Client(config: config) + _ = try await s3Client.listBuckets(input: ListBucketsInput()) + print("*** Success!") + } catch CommonRunTimeError.crtError(let crtError) { + print("*** An error occurred accessing the bucket list: \(crtError.message)") + } catch { + print("*** Unexpected error occurred requesting the bucket list.") + } + // snippet-end: [swift.http-config.timeouts] + + } +} + +/// The program's asynchronous entry point. +@main +struct Main { + static func main() async { + let args = Array(CommandLine.arguments.dropFirst()) + + do { + let command = try ExampleCommand.parse(args) + try await command.runAsync() + } catch { + ExampleCommand.exit(withError: error) + } + } +} +// snippet-end:[swift.http-config]