22// SPDX-License-Identifier: Apache-2.0
33
44// snippet-start:[cognitoidentity.swift.handler-imports]
5- import Foundation
65import ClientRuntime
6+ import Foundation
7+
78// snippet-start:[cognitoidentity.swift.import]
89import AWSCognitoIdentity
10+
911// snippet-end:[cognitoidentity.swift.import]
1012// snippet-end:[cognitoidentity.swift.handler-imports]
1113
@@ -22,6 +24,7 @@ public class CognitoIdentityHandler {
2224 public init ( ) async throws {
2325 cognitoIdentityClient = try await CognitoIdentityClient ( )
2426 }
27+
2528 // snippet-end:[cognitoidentity.swift.init]
2629
2730 // snippet-start:[cognitoidentity.swift.get-pool-id]
@@ -34,44 +37,39 @@ public class CognitoIdentityHandler {
3437 /// or `nil` on error or if not found.
3538 ///
3639 func getIdentityPoolID( name: String ) async throws -> String ? {
37- var token : String ? = nil
38-
39- // Iterate over the identity pools until a match is found.
40+ let listPoolsInput = ListIdentityPoolsInput ( maxResults: 25 )
41+ // Use "Paginated" to get all the objects.
42+ // This lets the SDK handle the 'nextToken' field in "ListIdentityPoolsOutput".
43+ let pages = cognitoIdentityClient. listIdentityPoolsPaginated ( input: listPoolsInput)
4044
41- repeat {
42- /// `token` is a value returned by `ListIdentityPools()` if the
43- /// returned list of identity pools is only a partial list. You
44- /// use the `token` to tell Amazon Cognito that you want to
45- /// continue where you left off previously. If you specify `nil`
46- /// or you don't provide the token, Amazon Cognito will start at
47- /// the beginning.
48-
49- let listPoolsInput = ListIdentityPoolsInput ( maxResults: 25 , nextToken: token)
50-
51- /// Read pages of identity pools from Cognito until one is found
52- /// whose name matches the one specified in the `name` parameter.
53- /// Return the matching pool's ID. Each time we ask for the next
54- /// page of identity pools, we pass in the token given by the
55- /// previous page.
56-
57- let output = try await cognitoIdentityClient. listIdentityPools ( input: listPoolsInput)
45+ do {
46+ for try await page in pages {
47+ guard let identityPools = page. identityPools else {
48+ print ( " ERROR: listIdentityPoolsPaginated returned nil contents. " )
49+ continue
50+ }
51+
52+ /// Read pages of identity pools from Cognito until one is found
53+ /// whose name matches the one specified in the `name` parameter.
54+ /// Return the matching pool's ID.
5855
59- if let identityPools = output. identityPools {
6056 for pool in identityPools {
6157 if pool. identityPoolName == name {
6258 return pool. identityPoolId!
6359 }
6460 }
6561 }
66-
67- token = output. nextToken
68- } while token != nil
62+ } catch {
63+ print ( " ERROR: getIdentityPoolID: " , dump ( error) )
64+ throw error
65+ }
6966
7067 return nil
7168 }
69+
7270 // snippet-end:[cognitoidentity.swift.get-pool-id]
7371
74- // snippet-start:[cognitoidentity.swift.get-or-create-pool-id]
72+ // snippet-start:[cognitoidentity.swift.get-or-create-pool-id]
7573 /// Return the ID of the identity pool with the specified name.
7674 ///
7775 /// - Parameters:
@@ -83,12 +81,18 @@ public class CognitoIdentityHandler {
8381 public func getOrCreateIdentityPoolID( name: String ) async throws -> String ? {
8482 // See if the pool already exists. If it doesn't, create it.
8583
86- guard let poolId = try await self . getIdentityPoolID ( name: name) else {
87- return try await self . createIdentityPool ( name: name)
84+ do {
85+ guard let poolId = try await getIdentityPoolID ( name: name) else {
86+ return try await createIdentityPool ( name: name)
87+ }
88+
89+ return poolId
90+ } catch {
91+ print ( " ERROR: getOrCreateIdentityPoolID: " , dump ( error) )
92+ throw error
8893 }
89-
90- return poolId
9194 }
95+
9296 // snippet-end:[cognitoidentity.swift.get-or-create-pool-id]
9397
9498 // snippet-start:[cognitoidentity.swift.create-identity-pool]
@@ -101,16 +105,22 @@ public class CognitoIdentityHandler {
101105 /// if an error occurred.
102106 ///
103107 func createIdentityPool( name: String ) async throws -> String ? {
104- let cognitoInputCall = CreateIdentityPoolInput ( developerProviderName: " com.exampleco.CognitoIdentityDemo " ,
105- identityPoolName: name)
106-
107- let result = try await cognitoIdentityClient. createIdentityPool ( input: cognitoInputCall)
108- guard let poolId = result. identityPoolId else {
109- return nil
108+ do {
109+ let cognitoInputCall = CreateIdentityPoolInput ( developerProviderName: " com.exampleco.CognitoIdentityDemo " ,
110+ identityPoolName: name)
111+
112+ let result = try await cognitoIdentityClient. createIdentityPool ( input: cognitoInputCall)
113+ guard let poolId = result. identityPoolId else {
114+ return nil
115+ }
116+
117+ return poolId
118+ } catch {
119+ print ( " ERROR: createIdentityPool: " , dump ( error) )
120+ throw error
110121 }
111-
112- return poolId
113122 }
123+
114124 // snippet-end:[cognitoidentity.swift.create-identity-pool]
115125
116126 // snippet-start:[cognitoidentity.swift.delete-identity-pool]
@@ -120,11 +130,16 @@ public class CognitoIdentityHandler {
120130 /// - id: The ID of the identity pool to delete.
121131 ///
122132 func deleteIdentityPool( id: String ) async throws {
123- let input = DeleteIdentityPoolInput (
124- identityPoolId: id
125- )
126-
127- _ = try await cognitoIdentityClient. deleteIdentityPool ( input: input)
133+ do {
134+ let input = DeleteIdentityPoolInput (
135+ identityPoolId: id
136+ )
137+
138+ _ = try await cognitoIdentityClient. deleteIdentityPool ( input: input)
139+ } catch {
140+ print ( " ERROR: deleteIdentityPool: " , dump ( error) )
141+ throw error
142+ }
128143 }
129144 // snippet-end:[cognitoidentity.swift.delete-identity-pool]
130145}
0 commit comments