Python

Features ›› Reference Data ›› getCurrencyList ›› Sample Code ››
Parent Previous Next

Get Currency 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 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 getCurrencyList, which require CurrencyCode to be passed in.

Inside the function, we will declare the serviceName and build the headerObj straight.



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".




We will then retrieve a list of Currency from the 'response' variable and store in Currency variable.

After that, we create 3 empty list, CurrencyCodeList, CountryNameList and CurrencyNameList.



We will create a for-loop where we will loop through all the items in Currency variable.

As we loop through the items in 'Currency', we will append the CurrencyCode, CountryName and CurrencyName to CurrencyCodeList, CountryNameList and CurrencyNameList respectively.



Then we create a 'if' condition, where we will check if the AffinityID passed in, is in the ID_List.

If the CurrencyCode passed in, is in the CurrencyCodeList, we will retrieve the index of the CurrencyCode in the CurrencyCodeList, and proceed to return the CountryName and CurrencyName in CountryNameList and CurrencyNameList respectively. respectively.

Else, we return "Not Found".




Entire Code:


import requests, json

from functions import url


def getCurrencyList(CurrencyCode):

   serviceName = 'getCurrencyList',

   headerObj = {

                       'Header': {

                       'serviceName': serviceName,

                       'userID': '',

                       'PIN': '',

                       'OTP': ''

                       }

                       }

   


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

   response = requests.post(final_url)


   Currency = response.json()['Content']['ServiceResponse']['CurrencyList']['Currency']


   CurrencyCodeList = []


   CountryNameList = []


   CurrencyNameList = []


 

   for i in range(len(Currency)):

       CurrencyType = Currency[i]

       CurrencyCodeList.append(CurrencyType['CurrencyCode'])

       CountryNameList.append(CurrencyType['CountryName'])

       CurrencyNameList.append(CurrencyType['CurrencyName'])


   if CurrencyCode in CurrencyCodeList:

      index = CurrencyCodeList.index(CurrencyCode)

      return CountryNameList[index], CurrencyNameList[index]


   else:

       return 'Record not found'



Download


Go to Common Stuff Section

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