Skip to content

Commit 68118dd

Browse files
shepazonbrmur
authored andcommitted
Add HTTP config example - checkpoint
1 parent 226815a commit 68118dd

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed
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]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)