Python

Features ›› Account ›› getMonthlyBalanceTrend ›› Sample Code ››
Parent Previous Next

Get Monthly Balance Trend


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 function 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 getMonthlyBalanceTrend

Inside the function, we will create the input variables.




Then we build both the header and content 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

from functions import getRecord


def getMonthlyBalanceTrend():

   #Header

   serviceName = 'getMonthlyBalanceTrend'

   userID = 'demoAcct'

   PIN = '123456'

   OTP = '123456'

   accountID = '5016'

   numMonths = "6"

     

   

   headerObj = {

                       'Header': {

                       'serviceName': serviceName,

                       'userID': userID,

                       'PIN': PIN,

                       'OTP': OTP

                       }

                       }

   

   contentObj = {

                       'Content': {

                       'accountID': accountID,

                       'numMonths': numMonths

                       }

                       }

   

   final_url="{0}?Header={1}&Content={2}".format(url(),json.dumps(headerObj),json.dumps(contentObj))

   response = requests.post(final_url)

   serviceRespHeader = response.json()['Content']['ServiceResponse']['ServiceRespHeader']

   errorCode = serviceRespHeader['GlobalErrorID']

   

   if errorCode == '010000':

      trenddata = response.json()['Content']['ServiceResponse']['TrendData']      

      if trenddata =={}:

          print("No record found!")

      else:

          MonthEndBalanceList = trenddata['MonthEndBalance']

          recordCount = getRecord(MonthEndBalanceList)

          print("\nNo.\t     Year_Month\t\tBalance")

          if recordCount >1:

              for i in range(recordCount):

                  item = MonthEndBalanceList[i]

                  print("{}.\t{:>15}\t\t{}".format(i+1,item['Year_Month'],item['Balance']))

          else:

              print("{}.\t{:>15}\t\t{}".format(1,MonthEndBalanceList['Year_Month'],MonthEndBalanceList['Balance']))


   elif errorCode == '010041':

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

 

 else:

       print(serviceRespHeader['ErrorText'])

           

getMonthlyBalanceTrend()




Download


Go to Common Stuff Section

Created with the Personal Edition of HelpNDoc: Generate EPub eBooks with ease