Python

Parent Previous Next

Get Bench Mark Interest Rates


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 getBenchmarkInterestRates

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.

Else, we will display the ErrorText.




Entire Code:


import requests, json

from functions import url

from functions import getRecord


def getBenchmarkInterestRates():

   #Header

   serviceName = 'getBenchmarkInterestRates'

   userID = ''

   PIN = ''

   OTP = ''

         

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

      InterestRateList = response.json()['Content']['ServiceResponse']['InterestRateList']

      if InterestRateList =={}:

          print("No record found!")

      else:

          InterestRateItem = InterestRateList['InterestRateItem']

          recordCount = len(InterestRateItem)

          for i in getRecord(recordCount):

              item = InterestRateItem[i]

              print(i)

              print('interestRate: {}'.format(item['interestRate']))

              print('country: {}'.format(item['country']))

              print('period: {}'.format(item['period']))

              print('currency: {}'.format(item['currency']))

              print()



   else:

       print(serviceRespHeader['ErrorText'])

           

getBenchmarkInterestRates()



Download


Go to Common Stuff Section

Created with the Personal Edition of HelpNDoc: Create HTML Help, DOC, PDF and print manuals from 1 single source