Technical Documentation

Swift SDK

2 views 0

The following guide provides documentation for the Zephr Swift SDK. If you need access to this SDK, please email support@zephr.com.

The client-side SDK requires you configure it with your tenantId. Do this in your AppDelegate:

import UIKit
import Blaize

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        BlaizeConfiguration.shared().tenantId = 'yourTenantId'
        // do any other necessary launch configuration
        return true
    }
}

Optionally, Blaize environment can be provided, too:

BlaizeConfiguration.shared().env = "staging"

It is possible to specify a base URL for the Blaize service, if it is non-standard (not normally needed):

BlaizeConfiguration.shared().overrideBaseUrl  = "http://localhost:8080"

For client-side implementations the SDK supports the following functions:

Validate field

let validator = Blaize.validator()

let validity = validator.validate("first-name", yourNameFieldValue)

switch (validity) {
	case (.success, _):
		success()
	case .failure(let reason):
		fail("First name is invalid: \(reason)")
}

Register

import Blaize

func register() {
  var attributes = ["first-name": "John",
					"last-name": "Citizen"]

  let registerResponse = BlaizePublic.client.register((email: "johncit@blaize.io", password: "SomePassword1"))
  switch (registerResponse) {
    .success(let blaizeSessionId): 
 		// persist blaizeSessionId
	  	print("Logged in")

	.userAlreadyExists:
		print("A user with that email address already exists... try resetting your password")
	.emailDomainBlacklisted:
	    print("The email address you have supplied is not allowed for registration")
    .badRequest:
		print("One of more of the mandatory fields were not provided or are incorrect")
  }
}

Login

import Blaize

func viewDidLoad() {    
  let myLoginButton = UIButton(type: .Custom)]
  myLoginButton.backgroundColor = UIColor.orange()
  myLoginButton.frame = CGRect(0, 0, 180, 40);
  myLoginButton.center = view.center;
  myLoginButton.setTitle("Login via blaize" forState: .Normal)

  // Handle clicks on the button
  myLoginButton.addTarget(self, action: @selector(self.loginButtonClicked) forControlEvents: .TouchUpInside) 

  // Add the button to the view
  view.addSubview(myLoginButton)
}

@objc func loginButtonClicked() {
  BlaizePublic.client.login((email: "johncit@blaize.io", password: "SomePassword1")) { loginResult in
    switch loginResult {
    case .Failed(let error): 
      print(error)
    case .Success(let blaizeSessionId):
      //persist blaizeSessionId
      print("Logged in!")
  }
}

Logout

Blaize.client.logout()

Forget me

Blaize.client.forgetMe()

Entitlement challenge

import Blaize

func checkEntitlements() {
	let articleEntitlementId = "5341dc17-f91b-4311-b9ee-6906024c87a2"
	let videoEntitlementId = "2b7fa1f5-795d-459e-84eb-8e0c62fb018f"
	let response = BlaizePublic.client.challenge([articleEntitlementId, videoEntitlementId])

	if response[articleEntitlementId] == nil {
		print("You cannot view articles")
	}
    	
	if response[videoEntitlementId] == nil {
		print("You cannot view videos")
	}

}

Request Rule decision

import Blaize

func checkDecisionAPI() {
	// assumes client already logged in/registered
	let decision = BlaizePublic.client.accessDecision("/some/content", "GET", ["Referer": "https://www.facebook.com"])
	print(decision.status)
	print(decision.body)
}

Server-side SDK

The server-side SDK is essentially a REST client which performs HMAC request signing for the API. This SDK requires the implementor to specify the host, path, method, body, headers and query parameters for a request and returns the response as a JSON object or plain text.
In order to make requests to the Admin API you will need to first create a keypair, which you can do in the Blaize Admin Console.

You can also use the server-side SDK to sign request for use with a different HTTP client.

Example REST request

import UIKit
import Blaize

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        BlaizeConfiguration.shared().tenantId = 'yourTenantId'
		BlaizeConfiguration.shared().accessKey = 'yourAccessKey'
	    BlaizeConfiguration.shared().secretKey = 'yourSecretKey'
        // do any other necessary launch configuration
        return true
    }
}

func getUser(userId: String) {
	let userResponse = BlaizeAdmin.client.getUser(userId)
	switch (userResponse) {
		case .success(let user): 
			print("user email is \(user.validators.email)")
		case .failure(let error): 
			print("Error: \(error)")
	}

}