Get Customer Accounts
We will first import "request" and "json" module, as we need those module for calling the API
Then, we will import our own url and getRecord functions from a file called functions.py, which can be referred to in the "Common Stuff" section linked at the bottom.
We will also be importing a reference data API 'getProductTypes', as one of the output for 'getCustomerAccounts' is 'productID', and we want to show the productName instead.
The Reference Data API can be referred under Reference Data Sub-Menu
Next, we create a function getCustomerAccounts
Inside the function, we will create the input variables.
Then we build the header object.
Then, we will create the final url, where we will use the url() function to get the API url, and append the headerObj and contentObj to the API url.
We will then post the final_url and store the server response in the variable "response".
The GlobalErrorID is then retrieved and stored in variable "errorCode".
If the errorCode is '010000', it means that there is no error, and we will proceed to check whether there are any records. If there are no records, we will display 'No record Found'.
If there are records, we will use the getRecord() function to check whether there is only 1 record, or more than 1 records, and display the results accordingly.
**NOTE: JSON output for 1 record and more than 1 record is different due to JSON structure. Hence, we use getRecord() function to retrieve the number of records.
If the errorCode is '010041', it means that the OTP provided has expired. We will then display 'OTP has expired. You will be receiving a SMS'.
Else, we will display the ErrorText.
Entire Code:
import requests, json
from functions import url,getRecord
from getProductTypes import getProductTypes
def getCustomerAccounts():
#Header
serviceName = 'getCustomerAccounts'
userID = 'bobsmith1'
PIN = '123456'
OTP = '123456'
headerObj = {
'Header': {
'serviceName': serviceName,
'userID': userID,
'PIN': PIN,
'OTP':OTP
}
}
final_url="{0}?Header={1}".format(url(),json.dumps(headerObj))
response = requests.post(final_url)
serviceRespHeader = response.json()['Content']['ServiceResponse']['ServiceRespHeader']
errorCode = serviceRespHeader['GlobalErrorID']
if errorCode == '010000':
acc_list = response.json()['Content']['ServiceResponse']['AccountList']
if acc_list == {}:
print("No record found!")
else:
acc_list = acc_list['account']
recordCount = getRecord(acc_list)
if recordCount > 1:
for i in range(0,recordCount,1):
account = acc_list[i]
acc_type = getProductTypes(account['productID'])
print('\nBalance: {}'.format(account['balance']))
print('Account ID: {}'.format(account['accountID']))
print('Account Open Date: {}'.format(account['accountOpenDate']))
print('Home Branch: {}'.format(account['homeBranch']))
print('Last Maintenance Officer: {}'.format(account['maintenancehistory']['lastMaintenanceOfficer']))
print('Last Transaction Branch: {}'.format(account['maintenancehistory']['lastTransactionBranch']))
print('Parent Account Flag: {}'.format(account['parentAccountFlag']))
print('Interest Rate: {}'.format(account['interestRate']))
print('Product Type: {}'.format(acc_type))
print('Current Status: {}'.format(account['currentStatus']))
print('Officer ID: {}'.format(account['officerID']))
print('Currency: {}'.format(account['currency']))
elif recordCount == 0:
acc_type = getProductTypes(acc_list['productID'])
print('Balance: {}'.format(acc_list['balance']))
print('Account ID: {}'.format(acc_list['accountID']))
print('Account Open Date: {}'.format(acc_list['accountOpenDate']))
print('Home Branch: {}'.format(acc_list['homeBranch']))
print('Last Maintenance Officer: {}'.format(acc_list['maintenancehistory']['lastMaintenanceOfficer']))
print('Last Transaction Branch: {}'.format(acc_list['maintenancehistory']['lastTransactionBranch']))
print('Parent Account Flag: {}'.format(acc_list['parentAccountFlag']))
print('Interest Rate: {}'.format(acc_list['interestRate']))
print('Product Type: {}'.format(acc_type))
print('Current Status: {}'.format(acc_list['currentStatus']))
print('Officer ID: {}'.format(acc_list['officerID']))
print('Currency: {}'.format(acc_list['currency']))
elif errorCode == '010041':
print("OTP has expired.\nYou will receiving a SMS")
else:
print(serviceRespHeader['ErrorText'])
getCustomerAccounts()
Created with the Personal Edition of HelpNDoc: Free EPub producer