Python

Features ›› Customer ›› getCustomerPreference ›› Sample Code ››
Parent Previous Next

Get Customer Preference


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.




Next, we create a function getCustomerPreference

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 functions import getRecord


def getCustomerPreference():

   OTP = '123',

   PIN = '123',

   serviceName = 'getCustomerPreference',

   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':

       PreferenceObj = response.json()['Content']['ServiceResponse']['Preference']

       RMModeOfCommunicationObj = PreferenceObj['RMModeOfCommunication']

       print("RMModeOfCommunication")

       print("ViaPhone: {}".format(RMModeOfCommunicationObj['ViaPhone']))

       print("ViaEmail: {}".format(RMModeOfCommunicationObj['ViaEmail']))

       print("ViaSMS: {}".format(RMModeOfCommunicationObj['ViaSMS']))

       print("")

       

       MonthlyStatementObj = PreferenceObj['MonthlyStatement']

       print("MonthlyStatement")

       print("ViaEmail: {}".format(MonthlyStatementObj['ViaEmail']))

       print("ViaSnailMail: {}".format(MonthlyStatementObj['ViaSnailMail']))

       print("")


       TriggerObj = PreferenceObj['TriggerList']

       if TriggerObj =={}:

          print("No record found!")


       else:

          TriggerList = TriggerObj['Trigger']

          recordCount = getRecord(TriggerList)

          print("\nNo.\t     TriggerType\t\tAccountID\tViaEmail\tAmount\tViaSMS")

          if recordCount >1:

              for i in range(recordCount):

                  item = TriggerList[i]

                  print("{}.\t{:>15}\t\t{}\t{}\t{}\t{}".format(i+1,item['TriggerType'],item['AccountID'],item['ViaEmail'],item['Amount'],item['ViaSMS']))

          else:

              print("{}.\t{:>15}\t\t{}\t{}\t{}\t{}".format(1,TriggerList['TriggerType'],TriggerList['AccountID'],TriggerList['ViaEmail'],TriggerList['Amount'],TriggerList['ViaSMS']))


         

   elif errorCode == '010041':

       print("OTP has expired.\nYou will receiving a SMS")


   else:

       print(serviceRespHeader['ErrorText'])


getCustomerPreference()





Download


Go to Common Stuff Section

Created with the Personal Edition of HelpNDoc: Create help files for the Qt Help Framework