Skip to content

Commit

Permalink
Swift: Add pagination example (awsdocs#6949)
Browse files Browse the repository at this point in the history
  • Loading branch information
shepazon authored Oct 2, 2024
1 parent fae5994 commit 3c8eb77
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
34 changes: 34 additions & 0 deletions swift/example_code/swift-sdk/pagination/Package.swift
Original file line number Diff line number Diff line change
@@ -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"),
]
)
65 changes: 65 additions & 0 deletions swift/example_code/swift-sdk/pagination/Sources/entry.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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 {
// snippet-start:[swift.pagination]
let PAGE_SIZE = 10
let client: S3Client

// Create the Amazon S3 client.

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.

// 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 {
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 ?? "<unknown>"))
}
}
} catch {
print("ERROR: Unable to process bucket list pages.")
}
// snippet-end:[swift.process-paginator]
}
// snippet-end:[swift.pagination]
}
2 changes: 2 additions & 0 deletions swift/example_code/swift-sdk/pagination/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
echo "No automated tests available."

0 comments on commit 3c8eb77

Please sign in to comment.