Python

Features ›› Product ›› getAllProductDetails ›› Sample Code ››
Parent Previous Next

Get All Product Details


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

Then, we will import our own url and getrecord 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 getAllProductDetails

Inside the function, we will create the input variables.




Then we build both the header and content object.


Moving on, we will create the final URL, where we will use the url() function to get the API URL, and append the headerObj and contentObj to 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 proceed to check whether there are any records. If there are no records, we will display 'No record Found'. If there are records, we will use the getRecord() function to check whether there is only 1 record, or more than 1 records, and display the results accordingly.


**NOTE: JSON output for 1 record and more than 1 record is different due to JSON structure. Hence, we use getRecord() function to retrieve the number of records.


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 functions import getRecord


def getAllProductDetails():

   #Header

   serviceName = 'getAllProductDetails'

   userID = ''

   PIN = ''

   OTP = ''


   #Content

   bankID = '1'

   

   headerObj = {

                       'Header': {

                       'serviceName': serviceName,

                       'userID': userID,

                       'PIN': PIN,

                       'OTP':OTP

                       }

                       }

   contentObj = {

                       'Content': {

                       'bankID': bankID

                       }

                       }

   

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

       product_list = response.json()['Content']['ServiceResponse']['Product_ParametersList_Read-Response']['ProductList']

       if product_list == {}:

           print("No record found!")

       else:

           product_list = product_list['Product']

           recordCount = getRecord(product_list)

           if recordCount > 1:

               for i in range(0,recordCount,1):

                   product = product_list[i]

                   print('\nProduct Name: {}'.format(product['ProductName']))

                   print('Min Opening Balance: {}'.format(product['MinOpeningBalance']))

                   print('Penalty Rate: {}'.format(product['PenaltyRate']))

                   print('Product ID: {}'.format(product['ProductID']))

                   print('Max Ltv Ratio: {}'.format(product['MaxLtvRatio']))

                   print('Repayment Penalty Threshold: {}'.format(product['RepaymentPenaltyThreshold']))

                   print('Term: {}'.format(product['Term']))

                   print('Interest Rate: {}'.format(product['InterestRate']))


                   

           elif recordCount == 0:

                   print('\nProduct Name: {}'.format(product_list['ProductName']))

                   print('Min Opening Balance: {}'.format(product_list['MinOpeningBalance']))

                   print('Penalty Rate: {}'.format(product_list['PenaltyRate']))

                   print('Product ID: {}'.format(product_list['ProductID']))

                   print('Max Ltv Ratio: {}'.format(product_list['MaxLtvRatio']))

                   print('Repayment Penalty Threshold: {}'.format(product_list['RepaymentPenaltyThreshold']))

                   print('Term: {}'.format(product_list['Term']))

                   print('Interest Rate: {}'.format(product_list['InterestRate']))

   elif errorCode == '010041':

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

   else:

       print(serviceRespHeader['ErrorText'])


getAllProductDetails()        


     

 


Download


Go to Common Stuff Section

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