Skip to content
Permalink
Browse files

add API code

  • Loading branch information
Stormacq, Sebastien
Stormacq, Sebastien committed Oct 25, 2019
1 parent 5b45887 commit 9ec7a9a76395f49e324781fb2cad055d9e7d087a
@@ -2,14 +2,19 @@
import UIKit
import AWSMobileClient
import AWSAppSync

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

public let userData = UserData()
var appSyncClient: AWSAppSyncClient?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {


//init appsync
self.appSyncInit()

AWSMobileClient.default().addUserStateListener(self) { (userState, info) in

// notify our subscriber the value changed
@@ -21,10 +26,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

case .signedOut:
print("user just signed out")
self.userData.landmarks = []

case .signedIn:
print("user just signed in.")
print("username : \(String(describing: AWSMobileClient.default().username))")

print("Loading data")
self.queryLandmarks()

AWSMobileClient.default().getUserAttributes(completionHandler: { (attributes, error) in
print("error : \(String(describing: error))")
@@ -34,7 +43,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
AWSMobileClient.default().getTokens({ (tokens, error) in
print("error : \(String(describing: error))")
print("token : \(String(describing: tokens))")
print("")
print("")
})
})

@@ -61,6 +70,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}
}

if (self.userData.isSignedIn) {
print("Loading data")
self.queryLandmarks()
}


return true
}

@@ -120,4 +135,60 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
AWSMobileClient.default().signOut()
}

// MARK: AWSAppSync
func appSyncInit() {
do {
// You can choose the directory in which AppSync stores its persistent cache databases
let cacheConfiguration = try AWSAppSyncCacheConfiguration()

// AppSync configuration & client initialization
let appSyncServiceConfig = try AWSAppSyncServiceConfig()
let appSyncConfig = try AWSAppSyncClientConfiguration(appSyncServiceConfig: appSyncServiceConfig,
userPoolsAuthProvider: AWSMobileClient.default() as AWSCognitoUserPoolsAuthProvider,
cacheConfiguration: cacheConfiguration)
self.appSyncClient = try AWSAppSyncClient(appSyncConfig: appSyncConfig)
print("Initialized appsync client.")
} catch {
print("Error initializing appsync client. \(error)")
}
}

func queryLandmarks() {
print("Query landmarks")
self.appSyncClient?.fetch(query: ListLandmarksQuery(limit:100), cachePolicy: .fetchIgnoringCacheData) {(result, error) in
if error != nil {
print(error?.localizedDescription ?? "")
return
}
print("Landmarks query complete.")
result?.data?.listLandmarks?.items!.forEach {

// convert the AppSync jsonObject (aka Dictionary<String, Any> to Data
// the code below assumes there is no casting / nil error
// TODO should add guard statement and handle errors
// https://nacho4d-nacho4d.blogspot.com/2016/05/dictionary-to-json-string-and-json.html
let jsonData = try! JSONSerialization.data(withJSONObject: $0?.jsonObject as Any, options: [])
// this allows to create a Landmark object using the Decodable protocol
let l : Landmark = try! JSONDecoder().decode(Landmark.self, from: jsonData)
self.userData.landmarks.append(l);

}
}
}
}

// Make sure AWSMobileClient is a Cognito User Pool credentails providers
// this makes it easy to AWSMobileClient shared instance with AppSync Client
// read https://github.com/awslabs/aws-mobile-appsync-sdk-ios/issues/157 for details
extension AWSMobileClient: AWSCognitoUserPoolsAuthProviderAsync {
public func getLatestAuthToken(_ callback: @escaping (String?, Error?) -> Void) {
getTokens { (tokens, error) in
if error != nil {
callback(nil, error)
} else {
callback(tokens?.idToken?.tokenString, nil)
}
}
}
}
@@ -10,6 +10,6 @@ import SwiftUI

final class UserData: ObservableObject {
@Published var showFavoritesOnly = false
@Published var landmarks = landmarkData
@Published var landmarks : [Landmark] = []
@Published var isSignedIn : Bool = false
}
@@ -9,5 +9,6 @@ target 'Landmarks' do
pod 'AWSMobileClient', '~> 2.12.0' # Required dependency
pod 'AWSAuthUI', '~> 2.12.0' # Optional dependency required to use drop-in UI
pod 'AWSUserPoolsSignIn', '~> 2.12.0' # Optional dependency required to use drop-in UI

pod 'AWSAppSync', '~> 2.15.0' # For AppSync GraphQL API

end
@@ -1,4 +1,8 @@
PODS:
- AWSAppSync (2.15.0):
- AWSCore (~> 2.12.0)
- ReachabilitySwift (~> 4.3.1)
- SQLite.swift (~> 0.12.2)
- AWSAuthCore (2.12.1):
- AWSCore (= 2.12.1)
- AWSAuthUI (2.12.1):
@@ -15,31 +19,42 @@ PODS:
- AWSUserPoolsSignIn (2.12.1):
- AWSAuthCore (= 2.12.1)
- AWSCognitoIdentityProvider (= 2.12.1)
- ReachabilitySwift (4.3.1)
- SQLite.swift (0.12.2):
- SQLite.swift/standard (= 0.12.2)
- SQLite.swift/standard (0.12.2)

DEPENDENCIES:
- AWSAppSync (~> 2.15.0)
- AWSAuthUI (~> 2.12.0)
- AWSMobileClient (~> 2.12.0)
- AWSUserPoolsSignIn (~> 2.12.0)

SPEC REPOS:
trunk:
- AWSAppSync
- AWSAuthCore
- AWSAuthUI
- AWSCognitoIdentityProvider
- AWSCognitoIdentityProviderASF
- AWSCore
- AWSMobileClient
- AWSUserPoolsSignIn
- ReachabilitySwift
- SQLite.swift

SPEC CHECKSUMS:
AWSAppSync: 38d681c93c2909e7fb8d4e1d1f0e816684f9f64f
AWSAuthCore: fa908a4df2feb3c978dfabb9a0927dffc69b112d
AWSAuthUI: 8255ccc55b4f268326c6013a532442fee86691ae
AWSCognitoIdentityProvider: 4b453fa8ae677dd9646fe5cb137b520105f33d08
AWSCognitoIdentityProviderASF: f94f1a502e72ef3d0a1de93e10bf7a79c8698118
AWSCore: 0a4431f74d9dbf084d420b887a6a21042b423c5c
AWSMobileClient: b178cd21ba500030e727c6afcc00dda438b1fecd
AWSUserPoolsSignIn: bdf072cb106595dd23b9b957f52ac7628ee94cdd
ReachabilitySwift: 4032e2f59586e11e3b0ebe15b167abdd587a388b
SQLite.swift: d2b4642190917051ce6bd1d49aab565fe794eea3

PODFILE CHECKSUM: 8dc32192e3e7c79aa6578727058d9546d744bcd5
PODFILE CHECKSUM: 8ca04af49bee2e723627f6c24c72e50407eb3b34

COCOAPODS: 1.8.4

0 comments on commit 9ec7a9a

Please sign in to comment.
You can’t perform that action at this time.