|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | +// snippet-start:[swift.http-config] |
| 5 | +// An example demonstrating how to customize the configuration of the HTTP |
| 6 | +// client used by an Amazon Web Services (AWS) service client. |
| 7 | + |
| 8 | +import ArgumentParser |
| 9 | +import AWSClientRuntime |
| 10 | +import ClientRuntime |
| 11 | +import AWSS3 |
| 12 | +import Foundation |
| 13 | +import SmithyHTTPAPI |
| 14 | +import AwsCommonRuntimeKit |
| 15 | + |
| 16 | +struct ExampleCommand: ParsableCommand { |
| 17 | + @Option(help: "Name of the Amazon Region to use") |
| 18 | + var region = "us-east-1" |
| 19 | + |
| 20 | + static var configuration = CommandConfiguration( |
| 21 | + commandName: "http-config", |
| 22 | + abstract: """ |
| 23 | + Demonstrates how to configure the HTTP client used by an AWS service client. |
| 24 | + """, |
| 25 | + discussion: """ |
| 26 | + """ |
| 27 | + ) |
| 28 | + |
| 29 | + /// Called by ``main()`` to run the bulk of the example. |
| 30 | + func runAsync() async throws { |
| 31 | + //await SDKLoggingSystem().initialize(logLevel: .trace) |
| 32 | + //SDKDefaultIO.setLogLevel(level: .trace) |
| 33 | + // snippet-start:[swift.http-config.configure] |
| 34 | + let config = try await S3Client.S3ClientConfiguration( |
| 35 | + region: region, |
| 36 | + httpClientConfiguration: HttpClientConfiguration( |
| 37 | + connectTimeout: 2, |
| 38 | + socketTimeout: 5, |
| 39 | + defaultHeaders: Headers( |
| 40 | + [ |
| 41 | + "X-My-Custom-Header": "CustomHeaderValue", |
| 42 | + "X-Another-Custom-Header": "AnotherCustomValue" |
| 43 | + ] |
| 44 | + ) |
| 45 | + ) |
| 46 | + ) |
| 47 | + let s3Client = S3Client(config: config) |
| 48 | + // snippet-end:[swift.http-config.configure] |
| 49 | + |
| 50 | + print("*** Getting list of buckets ***") |
| 51 | + let output: ListBucketsOutput = try await s3Client.listBuckets(input: ListBucketsInput()) |
| 52 | + |
| 53 | + print("*** Getting bucket list with very short timeout ***") |
| 54 | + |
| 55 | + // snippet-start: [swift.http-config.timeouts] |
| 56 | + do { |
| 57 | + let config = try await S3Client.S3ClientConfiguration( |
| 58 | + region: region, |
| 59 | + httpClientConfiguration: HttpClientConfiguration( |
| 60 | + connectTimeout: 0.001, |
| 61 | + socketTimeout: 0.005 |
| 62 | + ) |
| 63 | + ) |
| 64 | + let s3Client = S3Client(config: config) |
| 65 | + let output: ListBucketsOutput = try await s3Client.listBuckets(input: ListBucketsInput()) |
| 66 | + } catch CommonRunTimeError.crtError(let crtError) { |
| 67 | + print("*** An error occurred accessing the bucket list: \(crtError.message)") |
| 68 | + } catch { |
| 69 | + print("*** Unexpected error requesting bucket list.") |
| 70 | + } |
| 71 | + // snippet-end: [swift.http-config.timeouts] |
| 72 | + |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/// The program's asynchronous entry point. |
| 77 | +@main |
| 78 | +struct Main { |
| 79 | + static func main() async { |
| 80 | + let args = Array(CommandLine.arguments.dropFirst()) |
| 81 | + |
| 82 | + do { |
| 83 | + let command = try ExampleCommand.parse(args) |
| 84 | + try await command.runAsync() |
| 85 | + } catch { |
| 86 | + ExampleCommand.exit(withError: error) |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | +// snippet-end:[swift.http-config] |
0 commit comments