Swiftpack.co - Package - pace/cloud-sdk-ios

PACE Cloud SDK for iOS

Getting Started

Link the CloudSDK.framework with your project as Embedded Binary.

Usage

OAuth Setup

To authenticate with PACE via OAuth provide the following information to CloudSDK:

  • clientId: String
  • idHost: String
  • redirectUrl: String
  • scope: String
  • session: ApiRequest.TokenResponseData?; the current session object - nil for the initial setup
// Create a configuration object
let config = OAuthConnection.Configuration(clientId: "CLIENT_ID")
config.idHost = "ID_HOST"

// Setup the OAuthConnection with the created config and your session
OAuthConnection.setup(configuration: config, session: SESSION_OBJECT)

Authorization - Retrieving the Access Token

Receiving an access token requires two steps:

  1. Requesting the authorization which will return a code
  2. Exchanging the code for a session object which will contain the actual accessToken

Note

CloudSDK will not store the created session objects / access tokens. It's up to you to either store the session and eventually keep refreshing it or to request a new one every time.

OAuthConnection.shared.authorize(clientId: String, redirectUrl: String, scope: String) { result in
             
    switch result {
    case .success(let code):
        
        // Request an exchange with the retrieved `code`
         OAuthConnection.shared.exchange(withHost: String, clientId: String, redirectUrl: String, code: String) { result in
                         
            switch result {
            case .success(let sessionObject):
                let token = sessionObject.accessToken
                
                // Store the sessionObject

            case .failure(let error):
                // Error handling
            }
        }

    case .failure(let error):
         // Error handling
    }
}

Performing requests

Once you've retrieved an access token, you can perform authenticated requests with the PACE backend. Requests to an URL other than the PACE API will be rejected.

OAuthConnection.shared.request(ApiRequest, expect: EXPECTED_TYPE, session: ApiRequest.TokenResponseData?) { result in
    switch result {
    case .success(let data):
        // ...
    
    case .failure(let error):
        // Error handling
    }
}

To request data that does not need an authentication, do the following (you can simply omit your session object):

OAuthConnection.shared.request(ApiRequest, expect: EXPECTED_TYPE) { result in
    switch result {
    case .success(let data):
        // ...
    
    case .failure(let error):
        // Error handling
    }
}

The request parameter needs to be of type ApiRequest. The following requests can be performed:

// Poi Endpoints
static func queryLocationBasedApps(host: String, latitude: Float, longitude: Float, appType: AppTypeg) -> ApiRequest
static func getListOfApps(host: String, appType: AppType, cachePolicy: CachePolicy, since: Date) -> ApiRequest
static func getLocationBasedApp(host: String, byId id: String) -> ApiRequest

// Auth Endpoints
static func authorize(host: String, clientId: String, redirectUrl: String, scope: String, responseType: ResponseType, codeChallengeMethod: CodeChallengeMethod, codeChallenge: String, userAuthToken: String?, deviceUUID: String?, state: String, code: String) -> ApiRequest
static func token(host: String, accessTokenData: AccessTokenData) -> ApiRequest
static func token(host: String, refreshTokenData: RefreshTokenData) -> ApiRequest
static func revoke(host: String, revokeTokenData: RevokeTokenData) -> ApiRequest
static func introspect(host: String, introspectData: IntrospectData) -> ApiRequest
static func getAuthorizedApps(host: String) -> ApiRequest
static func revokeAccess(host: String, appUUID: String) -> ApiRequest

// User Endpoint
static func userInfo(host: String) -> ApiRequest

// Payment Endpoints
static func registerSepaDirectMethod(host: String, body: RegisterSepaDirectMethodRequest) -> ApiRequest
static func deletePaymentMethod(host: String, paymentMethodId: String) -> ApiRequest
static func getAllPaymentMethods(host: String) -> ApiRequest
static func getReadyPaymentMethods(host: String) -> ApiRequest
static func getPreAuthorizedPaymentMethods(host: String) -> ApiRequest
static func getPaymentMethodKinds(host: String, language: String) -> ApiRequest

Example request: Location Based Apps

OAuthConnection.shared.request(.queryLocationBasedApps(host: String, latitude: Float, longitude: Float), expect: ApiRequest.QueryLocationBasedAppsResponse.self) { result in
    switch result {
    case .success(let data):
        // ...
        
    case .failure(let error):
        // Error handling
    }
}

Session & Token Management

Except for storing, CloudSDK provides several methods to manage your session. This includes:

  • creating / authorizing
  • refreshing
  • revoking / resetting

Here is a quick overview:

func authorize(host: String?, clientId: String?, redirectUrl: String, scope: String, codeChallenge: String?, session: ApiRequest.TokenResponseData?, then: @escaping (Result<String, Error>) -> Void)
func exchange(withHost: String?, clientId: String?, redirectUrl: String, code: String, codeVerifier: String?, session: ApiRequest.TokenResponseData?, createNewSession: Bool, then: @escaping (Result<ApiRequest.TokenResponseData, Error>) -> Void)
func isSessionValid(session: ApiRequest.TokenResponseData?) -> Bool
func refreshSession(session: ApiRequest.TokenResponseData?, completion: @escaping ((ApiRequest.TokenResponseData?) -> Void))

// Combines `isSessionValid(...)`and `refreshSession(...)`
func checkAndRefreshSession(session: ApiRequest.TokenResponseData?, force: Bool, completion: @escaping ((ApiRequest.TokenResponseData?) -> Void))
func reset(session: ApiRequest.TokenResponseData?, completion: ((Error?) -> Void)?)

Github

link
Stars: 5

Dependencies

Used By

Total: 0

Releases

Forecast: Cloudy - 2018-08-23 07:21:45

First public release