From cb6b2fb7e5bff73742bce260af68cfbef4505c47 Mon Sep 17 00:00:00 2001 From: Eric Shepherd Date: Tue, 1 Oct 2024 21:01:45 +0000 Subject: [PATCH 1/4] Add pagination example --- .../swift-sdk/pagination/Package.swift | 34 +++++++++++ .../swift-sdk/pagination/Sources/entry.swift | 57 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 swift/example_code/swift-sdk/pagination/Package.swift create mode 100644 swift/example_code/swift-sdk/pagination/Sources/entry.swift diff --git a/swift/example_code/swift-sdk/pagination/Package.swift b/swift/example_code/swift-sdk/pagination/Package.swift new file mode 100644 index 00000000000..f3de0efd8c2 --- /dev/null +++ b/swift/example_code/swift-sdk/pagination/Package.swift @@ -0,0 +1,34 @@ +// swift-tools-version:5.9 +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// The swift-tools-version declares the minimum version of Swift required to +// build this package. + +import PackageDescription + +let package = Package( + name: "pagination", + // Let Xcode know the minimum Apple platforms supported. + platforms: [ + .macOS(.v11), + .iOS(.v13) + ], + dependencies: [ + // Dependencies declare other packages that this package depends on. + .package( + url: "https://github.com/awslabs/aws-sdk-swift", + from: "1.0.0" + ), + ], + 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: "retry", + dependencies: [ + .product(name: "AWSS3", package: "aws-sdk-swift"), + ], + path: "Sources"), + ] +) diff --git a/swift/example_code/swift-sdk/pagination/Sources/entry.swift b/swift/example_code/swift-sdk/pagination/Sources/entry.swift new file mode 100644 index 00000000000..a10e94a2cc5 --- /dev/null +++ b/swift/example_code/swift-sdk/pagination/Sources/entry.swift @@ -0,0 +1,57 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// An example to demonstrate the use of pagination with the AWS SDK +// for Swift. + +import AWSS3 +import Foundation + +@main +struct PaginatorExample { + static func main() async { + let PAGE_SIZE = 10 + let client: S3Client + + do { + client = try await S3Client() + } catch { + print("ERROR: Unable to create the Amazon S3 client.") + return + } + + // Start pagination by using the `Paginated` version of the + // `listBuckets(input:)` function. Each page has up to 10 buckets in + // it. + + let pages = client.listBucketsPaginated( + input: ListBucketsInput(maxBuckets: PAGE_SIZE) + ) + + // Go through the pages, printing each page's buckets to the console. + // The paginator handles the continuation tokens automatically. + + var pageNumber = 0 + + do { + for try await page in pages { + pageNumber += 1 + + guard let pageBuckets = page.buckets else { + print("ERROR: No buckets returned in page \(pageNumber)") + continue + } + + print("\nPage \(pageNumber):") + + // Print this page's bucket names. + + for bucket in pageBuckets { + print(" " + (bucket.name ?? "")) + } + } + } catch { + print("ERROR: Unable to process bucket list pages.") + } + } +} From 06ea7c6f6e645ebe8536a8d28f75f3c54a82b497 Mon Sep 17 00:00:00 2001 From: Eric Shepherd Date: Tue, 1 Oct 2024 21:05:44 +0000 Subject: [PATCH 2/4] Add missing snippet tags --- swift/example_code/swift-sdk/pagination/Sources/entry.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/swift/example_code/swift-sdk/pagination/Sources/entry.swift b/swift/example_code/swift-sdk/pagination/Sources/entry.swift index a10e94a2cc5..d1e688cbb37 100644 --- a/swift/example_code/swift-sdk/pagination/Sources/entry.swift +++ b/swift/example_code/swift-sdk/pagination/Sources/entry.swift @@ -13,6 +13,7 @@ struct PaginatorExample { let PAGE_SIZE = 10 let client: S3Client + // snippet-start:[swift.pagination] do { client = try await S3Client() } catch { @@ -24,13 +25,16 @@ struct PaginatorExample { // `listBuckets(input:)` function. Each page has up to 10 buckets in // it. + // snippet-start:[swift.create-paginator] let pages = client.listBucketsPaginated( input: ListBucketsInput(maxBuckets: PAGE_SIZE) ) + // snippet-end:[swift.create-paginator] // Go through the pages, printing each page's buckets to the console. // The paginator handles the continuation tokens automatically. + // snippet-start:[swift.process-paginator] var pageNumber = 0 do { @@ -53,5 +57,7 @@ struct PaginatorExample { } catch { print("ERROR: Unable to process bucket list pages.") } + // snippet-end:[swift.process-paginator] } + // snippet-end:[swift.pagination] } From 1ff3dcea3ad98da36f08ad107ace15553d259b29 Mon Sep 17 00:00:00 2001 From: Eric Shepherd Date: Wed, 2 Oct 2024 14:46:07 +0000 Subject: [PATCH 3/4] Tidy up a bit --- swift/example_code/swift-sdk/pagination/Sources/entry.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/swift/example_code/swift-sdk/pagination/Sources/entry.swift b/swift/example_code/swift-sdk/pagination/Sources/entry.swift index d1e688cbb37..2a486aaa9b7 100644 --- a/swift/example_code/swift-sdk/pagination/Sources/entry.swift +++ b/swift/example_code/swift-sdk/pagination/Sources/entry.swift @@ -10,10 +10,12 @@ import Foundation @main struct PaginatorExample { static func main() async { + // snippet-start:[swift.pagination] let PAGE_SIZE = 10 let client: S3Client - // snippet-start:[swift.pagination] + // Create the Amazon S3 client. + do { client = try await S3Client() } catch { From 2e37078806576f869b763dca6131d81589a09846 Mon Sep 17 00:00:00 2001 From: Eric Shepherd Date: Wed, 2 Oct 2024 14:50:23 +0000 Subject: [PATCH 4/4] Add no-test test.sh file --- swift/example_code/swift-sdk/pagination/test.sh | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 swift/example_code/swift-sdk/pagination/test.sh diff --git a/swift/example_code/swift-sdk/pagination/test.sh b/swift/example_code/swift-sdk/pagination/test.sh new file mode 100644 index 00000000000..ba51b7ed1d3 --- /dev/null +++ b/swift/example_code/swift-sdk/pagination/test.sh @@ -0,0 +1,2 @@ +#!/bin/bash +echo "No automated tests available."