Skip to content

Swift: Add HTTP configuration example #7421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions swift/example_code/swift-sdk/http-config/Package.swift
Original file line number Diff line number Diff line change
@@ -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]
88 changes: 88 additions & 0 deletions swift/example_code/swift-sdk/http-config/Sources/entry.swift
Original file line number Diff line number Diff line change
@@ -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]
Loading