Python

Parent Previous Next

Get Standing Instruction List


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.

The Reference Data API can be referred under Reference Data Sub-Menu



Next, we create a function getStandingInstructionList

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


def getStandingInstructionList():

   #Header

   serviceName = 'getStandingInstructionList'

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

       instruction_list = response.json()['Content']['ServiceResponse']['StandingInstructionList']

       if instruction_list == {}:

           print("No record found!")

       else:

           instruction_list = instruction_list['StandingInstruction']

           recordCount = getRecord(instruction_list)

           if recordCount > 1:

               for i in range(0,recordCount,1):

                   instruction = instruction_list[i]

                   print('\nStanding Instruction ID: {}'.format(instruction['StandingInstructionID']))

                   print('\nTo Account: {}'.format(instruction['ToAccount']))

                   print('\nNext Date Time: {}'.format(instruction['NextDateTime']))

                   print('\nFrom Account: {}'.format(instruction['FromAccount']))

                   print('\nAmount: {}'.format(instruction['Amount']))

                   print('\nIs Recurring: {}'.format(instruction['isRecurring']))

                   print('\nCustomer ID: {}'.format(instruction['CustomerID']))

                   print('\nLast Date Time: {}'.format(instruction['LastDateTime']))

                   print('\nTransaction Reference Number: {}'.format(instruction['TransactionReferenceNumber']))

                   print('\nMemo: {}'.format(instruction['Memo']))

                   print('\nWeekly or Monthly: {}'.format(instruction['Weekly_Monthly']))

                   

           elif recordCount == 0:

                   print('\nStanding Instruction ID: {}'.format(instruction['StandingInstructionID']))

                   print('\nTo Account: {}'.format(instruction['ToAccount']))

                   print('\nNext Date Time: {}'.format(instruction['NextDateTime']))

                   print('\nFrom Account: {}'.format(instruction['FromAccount']))

                   print('\nAmount: {}'.format(instruction['Amount']))

                   print('\nIs Recurring: {}'.format(instruction['isRecurring']))

                   print('\nCustomer ID: {}'.format(instruction['CustomerID']))

                   print('\nLast Date Time: {}'.format(instruction['Last Date Time']))

                   print('\nTransaction Reference Number: {}'.format(instruction['TransactionReferenceNumber']))

                   print('\nMemo: {}'.format(instruction['Memo']))

                   print('\nWeekly or Monthly: {}'.format(instruction['Weekly_Monthly']))

   elif errorCode == '010041':

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

   else:

       print(serviceRespHeader['ErrorText'])


getStandingInstructionList()        



Download


Go to Common Stuff Section

Created with the Personal Edition of HelpNDoc: Easily create PDF Help documents