Python

Parent Previous Next

Get Transaction Types


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 getTransactionTypes, which require tID 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 TransactionType from the 'response' variable and store in 'types' variable.

After that, we create 2 empty list, ID_List and Name_List.



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

As we loop through the items in 'types', we will append the TransactionTypeID and TransactionTypeName to the ID_List and Name_List respectively.


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

If the tID passed in, is in the ID_List, we will retrieve the index of the tID in the ID_List, and proceed to return the TransactionTypeName in Name_List.

Else, we return "Not Found".



Entire Code:


import requests, json

from functions import url


def getTransactionTypes(tID):

   serviceName = 'getTransactionTypes'

   headerObj = {

                       'Header': {

                       'serviceName': serviceName,

                       'userID': '',

                       'PIN': '',

                       'OTP': ''

                       }

                       }

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

   response = requests.post(final_url)


   types = response.json()['Content']['ServiceResponse']['TransactionTypeList']['TransactionType']

   ID_List= []

   Name_List = []

   Code_List = []

   

   for i in range(len(types)):

       transactionType = types[i]

       ID_List.append(transactionType['TransactionTypeID'])

       Name_List.append(transactionType['TransactionTypeName'])

       Code_List.append(transactionType['TransactionCode'])


   if tID in ID_List:

       index = ID_List.index(tID)

       return Name_List[index]

   else:

       return 'Record not found'


Download


Go to Common Stuff Section

Created with the Personal Edition of HelpNDoc: Full-featured Help generator