Skip to content

Commit 6134b0d

Browse files
authored
Swift: Add HTTP configuration example (#7421)
* Add HTTP config example - checkpoint * Finished example.
1 parent a4213ac commit 6134b0d

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// swift-tools-version: 5.9
2+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// (swift-tools-version has two lines here because it needs to be the first
6+
// line in the file, but it should also appear in the snippet below)
7+
//
8+
// snippet-start:[swift.cognito-identity-provider.scenario.package]
9+
// swift-tools-version: 5.9
10+
//
11+
// The swift-tools-version declares the minimum version of Swift required to
12+
// build this package.
13+
14+
import PackageDescription
15+
16+
let package = Package(
17+
name: "http-config",
18+
// Let Xcode know the minimum Apple platforms supported.
19+
platforms: [
20+
.macOS(.v13),
21+
.iOS(.v15)
22+
],
23+
dependencies: [
24+
// Dependencies declare other packages that this package depends on.
25+
.package(
26+
url: "https://github.com/awslabs/aws-sdk-swift",
27+
from: "1.0.0"),
28+
.package(
29+
url: "https://github.com/apple/swift-argument-parser.git",
30+
branch: "main"
31+
)
32+
],
33+
targets: [
34+
// Targets are the basic building blocks of a package, defining a module or a test suite.
35+
// Targets can depend on other targets in this package and products
36+
// from dependencies.
37+
.executableTarget(
38+
name: "cognito-scenario",
39+
dependencies: [
40+
.product(name: "AWSS3", package: "aws-sdk-swift"),
41+
.product(name: "ArgumentParser", package: "swift-argument-parser")
42+
],
43+
path: "Sources")
44+
45+
]
46+
)
47+
// snippet-end:[swift.cognito-identity-provider.scenario.package]
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
// snippet-start:[swift.http-config.imports]
10+
import ClientRuntime
11+
import AWSS3
12+
import SmithyHTTPAPI
13+
import AwsCommonRuntimeKit
14+
// snippet-end:[swift.http-config.imports]
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+
// snippet-start:[swift.http-config.headers]
32+
let config = try await S3Client.S3ClientConfiguration(
33+
region: region,
34+
httpClientConfiguration: HttpClientConfiguration(
35+
defaultHeaders: Headers(
36+
[
37+
"X-My-Custom-Header": "CustomHeaderValue",
38+
"X-Another-Custom-Header": "AnotherCustomValue"
39+
]
40+
)
41+
)
42+
)
43+
let s3Client = S3Client(config: config)
44+
// snippet-end:[swift.http-config.headers]
45+
46+
print("*** Getting list of buckets...")
47+
_ = try await s3Client.listBuckets(input: ListBucketsInput())
48+
print("*** Success!\n")
49+
50+
print("*** Getting bucket list with custom timeouts...")
51+
52+
// snippet-start: [swift.http-config.timeouts]
53+
do {
54+
let config = try await S3Client.S3ClientConfiguration(
55+
region: region,
56+
httpClientConfiguration: HttpClientConfiguration(
57+
connectTimeout: 2,
58+
socketTimeout: 5
59+
)
60+
)
61+
let s3Client = S3Client(config: config)
62+
_ = try await s3Client.listBuckets(input: ListBucketsInput())
63+
print("*** Success!")
64+
} catch CommonRunTimeError.crtError(let crtError) {
65+
print("*** An error occurred accessing the bucket list: \(crtError.message)")
66+
} catch {
67+
print("*** Unexpected error occurred requesting the bucket list.")
68+
}
69+
// snippet-end: [swift.http-config.timeouts]
70+
71+
}
72+
}
73+
74+
/// The program's asynchronous entry point.
75+
@main
76+
struct Main {
77+
static func main() async {
78+
let args = Array(CommandLine.arguments.dropFirst())
79+
80+
do {
81+
let command = try ExampleCommand.parse(args)
82+
try await command.runAsync()
83+
} catch {
84+
ExampleCommand.exit(withError: error)
85+
}
86+
}
87+
}
88+
// snippet-end:[swift.http-config]

0 commit comments

Comments
 (0)