Get Customer Affinities
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 from a file called functions.py which can be referred to in the "Common Stuff" section linked at the bottom.
We will also import getAffinityList from a file called getAffinityList which can be referred to in the "Reference Data" section.
Next, we create a function getCustomerAffinities
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 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
from getAffinityList import getAffinityList
from functions import getRecord
def getCustomerAffinities():
OTP = '123',
PIN = '123',
serviceName = 'getCustomerAffinities',
userID = 'alan1960'
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':
PartyAffinityList = response.json()['Content']['ServiceResponse']['PartyAffinityList']
if PartyAffinityList =={}:
print("No record found!")
else:
AffinityList = PartyAffinityList['Affinity']
recordCount = getRecord(AffinityList)
print("no.\t AffinityID \tActive \tLevel1 \tLevel2")
if recordCount >1:
for i in range(recordCount):
item = AffinityList[i]
levelone = getAffinityList(item['AffinityID'])[0]
leveltwo = getAffinityList(item['AffinityID'])[1]
print("{}.\t{:>12}\t\t{}\t\t{}\t{}".format(i+1,item['AffinityID'],item['Active'],levelone, leveltwo))
else:
print("{}.\t{:>15}\t\t{}".format(1,item['AffinityID'],item['Active']))
elif errorCode == '010041':
print("OTP has expired.\nYou will receiving a SMS")
else:
print(serviceRespHeader['ErrorText'])
getCustomerAffinities()
Created with the Personal Edition of HelpNDoc: Single source CHM, PDF, DOC and HTML Help creation