Skip to content

Commit f29b6fa

Browse files
authored
Add Cognito ListUsers example for Swift (#7474)
In addition, this PR makes a minor change to a previous Cognito example to adjust which lines are embedded when an SoS tag is used.
1 parent 74b08e4 commit f29b6fa

File tree

6 files changed

+133
-2
lines changed

6 files changed

+133
-2
lines changed

.doc_gen/metadata/cognito-identity-provider_metadata.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,14 @@ cognito-identity-provider_ListUsers:
429429
- description:
430430
snippet_tags:
431431
- javascript.v3.cognito-idp.actions.ListUsers
432+
Swift:
433+
versions:
434+
- sdk_version: 1
435+
github: swift/example_code/cognito-identity-provider
436+
excerpts:
437+
- description:
438+
snippet_tags:
439+
- swift.cognito-identity-provider.ListUsers
432440
services:
433441
cognito-identity-provider: {ListUsers}
434442
cognito-identity-provider_AdminInitiateAuth:
Lines changed: 47 additions & 0 deletions
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: "listusers",
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: "listusers",
39+
dependencies: [
40+
.product(name: "AWSCognitoIdentityProvider", 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]
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
// An example demonstrating how to get a list of the users in an Amazon
5+
// Cognito User Pool.
6+
import ArgumentParser
7+
import Foundation
8+
import AWSClientRuntime
9+
import AWSCognitoIdentityProvider
10+
11+
struct ExampleCommand: ParsableCommand {
12+
@Argument(help: "The user pool ID to use.")
13+
var poolId: String
14+
@Option(help: "Name of the Amazon Region to use.")
15+
var region = "us-east-1"
16+
17+
static var configuration = CommandConfiguration(
18+
commandName: "listusers",
19+
abstract: """
20+
Lists the users in the specified user pool.
21+
""",
22+
discussion: """
23+
"""
24+
)
25+
26+
/// Called by ``main()`` to run the bulk of the example.
27+
func runAsync() async throws {
28+
let config = try await CognitoIdentityProviderClient.CognitoIdentityProviderClientConfiguration(region: region)
29+
let cognitoClient = CognitoIdentityProviderClient(config: config)
30+
31+
// snippet-start:[swift.cognito-identity-provider.ListUsers]
32+
do {
33+
let output = try await cognitoClient.listUsers(
34+
input: ListUsersInput(
35+
userPoolId: poolId
36+
)
37+
)
38+
39+
guard let users = output.users else {
40+
print("No users found.")
41+
return
42+
}
43+
44+
print("\(users.count) user(s) found.")
45+
for user in users {
46+
print(" \(user.username ?? "<unknown>")")
47+
}
48+
} catch _ as NotAuthorizedException {
49+
print("*** Please authenticate with AWS before using this command.")
50+
return
51+
} catch _ as ResourceNotFoundException {
52+
print("*** The specified User Pool was not found.")
53+
return
54+
} catch {
55+
print("*** An unexpected type of error occurred.")
56+
return
57+
}
58+
// snippet-end:[swift.cognito-identity-provider.ListUsers]
59+
}
60+
}
61+
62+
/// The program's asynchronous entry point.
63+
@main
64+
struct Main {
65+
static func main() async {
66+
let args = Array(CommandLine.arguments.dropFirst())
67+
68+
do {
69+
let command = try ExampleCommand.parse(args)
70+
try await command.runAsync()
71+
} catch {
72+
ExampleCommand.exit(withError: error)
73+
}
74+
}
75+
}

swift/example_code/cognito-identity-provider/ListUsers/test.sh

Whitespace-only changes.

swift/example_code/cognito-identity-provider/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Code excerpts that show you how to call individual service functions.
3838
- [AdminRespondToAuthChallenge](scenario/Sources/entry.swift#L377)
3939
- [AssociateSoftwareToken](scenario/Sources/entry.swift#L298)
4040
- [ConfirmSignUp](scenario/Sources/entry.swift#L215)
41+
- [ListUsers](ListUsers/Sources/entry.swift#L31)
4142
- [ResendConfirmationCode](scenario/Sources/entry.swift#L180)
4243
- [SignUp](scenario/Sources/entry.swift#L131)
4344
- [VerifySoftwareToken](scenario/Sources/entry.swift#L334)
@@ -47,7 +48,7 @@ Code excerpts that show you how to call individual service functions.
4748
Code examples that show you how to accomplish a specific task by calling multiple
4849
functions within the same service.
4950

50-
- [Sign up a user with a user pool that requires MFA](scenario/Package.swift)
51+
- [Sign up a user with a user pool that requires MFA](../swift-sdk/http-config/Package.swift)
5152

5253

5354
<!--custom.examples.start-->

swift/example_code/cognito-identity-provider/scenario/Sources/entry.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
// 9. Invokes the AdminRespondToAuthChallenge to get back a token.
3434

3535
import ArgumentParser
36-
import AWSClientRuntime
3736
import Foundation
3837

3938
// snippet-start:[swift.cognito-identity-provider.import]
39+
import AWSClientRuntime
4040
import AWSCognitoIdentityProvider
4141
// snippet-end:[swift.cognito-identity-provider.import]
4242

0 commit comments

Comments
 (0)