Python

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

Get Customer 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 getCustomerTypes, which require customerID 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 CustomerType from the 'response' variable and store in 'customerType' 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 'customerType' variable.

As we loop through the items in 'customerType', we will append the CustomerTypeID and CustomerTypeName to the ID_List and Name_List respectively.


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

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

Else, we return "Not Found".



Entire Code:


import requests, json

from functions import url


def getCustomerTypes(customerID):

   serviceName = 'getCustomerTypes'

   headerObj = {

                       'Header': {

                       'serviceName': serviceName,

                       'userID': '',

                       'PIN': '',

                       'OTP': ''

                       }

                       }

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

   response = requests.post(final_url)


   customerType = response.json()['Content']['ServiceResponse']['CustomerTypeList']['CustomerType']

   ID_List= []

   Name_List = []

   

   for i in range(len(customerType)):

       customer = customerType[i]

       ID_List.append(customer['CustomerTypeID'])

       Name_List.append(customer['CustomerTypeName'])


   if customerID in ID_List:

       index = ID_List.index(customerID)

       return Name_List[index]

   else:

       return 'Record not found'


Download


Go to Common Stuff Section

Created with the Personal Edition of HelpNDoc: Easily create Web Help sites