Python

Features ›› Customer ›› updateCustomerDetails ›› Sample Code ››
Parent Previous Next

Update Customer 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 from a file called functions.py, which can be referred to in the "Common Stuff" section linked at the bottom.

We will also import urllib.parse as we would need to replace special character such as "#" in the Content JSON.



Next, we create a function updateCustomerDetails

Inside the function, we will create the input variables.





Then we build both 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 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 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

import urllib.parse


def updateCustomerDetails():

   OTP ='123456',

   PIN ='123456',

   serviceName ='updateCustomerDetails',

   userID ='alan1960'

   

   familyName = "Megargel"

   givenName = "Alan"

   dateOfBirth = "1960-12-11"

   gender = "Male"

   occupation = "Professor"

   streetAddress ="BLK 898, Woodlands St. 81, #01-123"

   city = "Singapore"

   state = "Singapore"

   country = "Singapore"

   postalCode = "730808"

   emailAddress = "alanmegargel@smu.edu.sg"

   countryCode = "+65"

   mobileNumber = "96690924"

   

   headerObj = {

                           'Header': {

                           'serviceName': serviceName,

                           'userID': userID,

                           'PIN': PIN,

                           'OTP': OTP

                           }

                           }

   

   contentObj = {

                       'Content': {                    

                       'familyName': familyName,

                       'givenName': givenName,

                       'dateOfBirth': dateOfBirth,

                       'gender': gender,

                       'occupation': occupation,

                       'streetAddress': streetAddress,

                       'city': city,

                       'state': state,

                       'country': country,

                       'postalCode': postalCode,

                       'emailAddress': emailAddress,

                       'countryCode': countryCode,

                       'mobileNumber': mobileNumber

                       }

                       }


   final_url="{0}?Header={1}&Content={2}".format(url(),json.dumps(headerObj),urllib.parse.quote(json.dumps(contentObj)))

   response = requests.post(final_url)


   serviceRespHeader = response.json()['Content']['ServiceResponse']['ServiceRespHeader']

   errorCode = serviceRespHeader['GlobalErrorID']


   if errorCode == '010000':

       print("Customer Details Updated")

       


   elif errorCode == '010041':

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


   else:

       print(serviceRespHeader['ErrorText'])


updateCustomerDetails()



Download


Go to Common Stuff Section

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