forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Swift: Add pagination example (awsdocs#6949)
- Loading branch information
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
65
swift/example_code/swift-sdk/pagination/Sources/entry.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash | ||
echo "No automated tests available." |