Python

Features ›› Market Data ›› getExchangeRate ›› Sample Code ››
Parent Previous Next

Get Exchange Rate

We will first import "request" and "json" module, as we need those module for calling the API

Then, we will import our own url from a file called functions.py, which can be referred to in the "Common Stuff" section linked at the bottom.

We will also import getCurrencyList from a file called getCurrencyList.py, which can be referred to in the "Reference Data"



Next, we create a function getExchangeRate

Inside the function, we will create the input variables.




Then we build 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 t and contentObj o 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 display 'the result.

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 getCurrencyList import getCurrencyList


def getExchangeRate():

   #Header

   serviceName = 'getExchangeRate'

   userID = ''

   PIN = ''

   OTP = ''

   baseCurrency = 'SGD'

   quoteCurrency = 'USD'

     

   

   headerObj = {

                       'Header': {

                       'serviceName': serviceName,

                       'userID': userID,

                       'PIN': PIN,

                       'OTP': OTP

                       }

                       }

   

   contentObj = {

                       'Content': {

                       'baseCurrency': baseCurrency,

                       'quoteCurrency': quoteCurrency

                       }

                       }

   

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

       base_currency_country_name = getCurrencyList(baseCurrency)[0]

       base_currency_currency_name = getCurrencyList(baseCurrency)[1]

       

       quote_currency_country_name = getCurrencyList(quoteCurrency)[0]

       quote_currency_currency_name = getCurrencyList(quoteCurrency)[1]


       print("Base Currency")

       print("Currency Code = {0}".format(baseCurrency))

       print("Country Name = {0}".format(base_currency_country_name))

       print("Currency Name = {0}".format(base_currency_currency_name))


       print()

       

       print("Quote Currency")

       print("Currency Code = {0}".format(quoteCurrency))

       print("Country Name = {0}".format(quote_currency_country_name))

       print("Currency Name = {0}".format(quote_currency_currency_name))


       print()

       

       FXSpotRateReadResponseObj = response.json()['Content']['ServiceResponse']['FX_SpotRate_Read-Response']

       print('Rate: {}'.format(FXSpotRateReadResponseObj['Rate']))

       

   elif errorCode == '010041':


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


   else:

      print(serviceRespHeader['ErrorText'])

           

getExchangeRate()


   


Download


Go to Common Stuff Section

Created with the Personal Edition of HelpNDoc: Easily create iPhone documentation