• June 6, 2022
  • Zanibal
  • 0
import json
import requests
import urllib3
urllib3.disable_warnings()

#Global variable that will store the token used in the samples
token = ''

#REST endpoint base url
api_url = "https://api-demo.zanibal.com/api"

#API username for accessing the REST services
api_user = "apiuser"

#API password or key for accessing the REST services
api_key = "xxxxxxxxx"


#Getting an access token. This will use the configured username and password to retrieve an bearer token
def get_access_token():
    global token
    url =   api_url + "/v1/security/request/access-token"
    file = dict(username = api_user, password = api_key)
    response = requests.post(url, files=file, verify=False)
    response = response.json()

    #Save the token
    token = response['access_token']

    #Optionall print out the access token
    #print(token)


# Login and retrieve a clients request passcode.
# The passcode will then be used to authenticate and retrieve the client's details
def login_customer():
    url = api_url + "/v1/security/login/customer"
    username = "john"
    password = "********"
    file = dict(username=username, password=password)

    Headers = {'Authorization': 'Bearer ' + token}
    response = requests.post(url, verify=False, headers=Headers, files=file)
    response = response.json()
    loginToken = response['msgCode']
    loginResult = response['success']
    print("Login success ->[" + str(loginResult) + "] & Private Key ->[" + loginToken + "]")

    #retrieve the client details using the private token from the login step
    url = api_url + "/v1/security/customer/username/" + username
    Headers = {'Authorization': 'Bearer ' + token, 'X-Auth-Token': loginToken}
    response = requests.get(url, verify=False, headers=Headers, files=file)
    print("\n\n" + json.dumps(response.json(), indent=4, sort_keys=True))

#Creating a customer
def create_customer():
    url = api_url + "/v1/partner/customer/create"
    payload = json.dumps({
        "active": True,
        "channel": "WEB",
        "firstName": "John",
        "lastName": "Smith",
        "cellPhone": "08037355772",
        "emailAddress1": "john.smith@mail.com",
        "partnerType": "INDIVIDUAL",
        # "customerGroupName": "RETAIL",  # The default value configured in application will be used if not specified
        # "businessOfficeName": "HQ" # The default value configured in application will be used if not specified
    })
    Headers = {'Authorization': 'Bearer ' + token, 'Content-Type':'application/json'}
    response = requests.post(url, verify=False, headers=Headers, data=payload)
    response = response.json()
    customerId = response['msgCode']

    print("Created new client with ID ->[" + response['msgCode'] + "] & Request Status ->[" + str(response['success']) + "]")

    #Use the ID returned by this API to get details of the created record
    find_customer_by_id(customerId)

# Find a customer using an ID
def find_customer_by_id(customerId):
    url = api_url + "/v1/partner/customer/id/" + customerId
    Headers = {'Authorization': 'Bearer ' + token}
    response = requests.get(url, verify=False, headers=Headers)
    print("\n\n" + json.dumps(response.json(), indent=4, sort_keys=True))


get_access_token()
login_customer()
create_customer()