Python

Parent Previous Next

Get Billing Organizations


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 getBillingOrganizations, which require accID 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 BillingOrg 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 Billing organization's AccountID and BillingOrgName to the ID_List and Name_List respectively.


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

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

Else, we return "Not Found".



Entire Code:


import requests, json

from functions import url


def getBillingOrganizations(accID):

   serviceName = 'getBillingOrganizations'

   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']['BillingOrgList']['BillingOrg']

   ID_List= []

   Name_List = []

   

   for i in range(len(types)):

       billingOrg = types[i]

       ID_List.append(billingOrg['AccountID'])

       Name_List.append(billingOrg['BillingOrgName'])

   ID = [s.lstrip("0") for s in ID_List]

   accID = str(accID)


   if accID in ID_List:

       index = ID_List.index(accID)

       return Name_List[index]

   else:

       return 'Not found'


Download


Go to Common Stuff Section

Created with the Personal Edition of HelpNDoc: Easily create CHM Help documents