Get Customer Stocks
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 functions from a file called functions.py, which can be referred to in the "Common Stuff" section linked at the bottom.
We will also import a reference data API getStockSymbols as one of the output is symbol, and we want to show symbol company name.
The Reference Data API can be referred under Reference Data Sub-Menu
Next, we create a function getCustomerStocks
Inside the function, we will create the input variables.
Then we build the header object
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".
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,getRecord
from getStockSymbols import getStockSymbols
def getCustomerStocks():
#Header
serviceName = 'getCustomerStocks'
userID = 'KelvanTan'
PIN = '000000'
OTP = '000000'
headerObj = {
'Header': {
'serviceName': serviceName,
'userID': userID,
'PIN': PIN,
'OTP':OTP
}
}
final_url="{0}?Header={1}".format(url(),json.dumps(headerObj))
response = requests.post(final_url)
serviceRespHeader = response.json()['Content']['ServiceResponse']['ServiceRespHeader']
errorCode = serviceRespHeader['GlobalErrorID']
if errorCode == '010000':
depository_list = response.json()['Content']['ServiceResponse']['DepositoryList']
if depository_list == {}:
print("No record found!")
else:
depository_list = depository_list['Depository']
recordCount = getRecord(depository_list)
if recordCount > 1:
for i in range(0,recordCount,1):
depository = depository_list[i]
symbol_company = getStockSymbols(depository['symbol'])
print("\nSymbol Name: {}".format(symbol_company))
print("Quantity: {}".format(depository['quantity']))
print("Price: {}".format(depository['price']))
print("Trading Date: {}".format(depository['tradingDate']))
print("Customer ID: {}".format(depository['customerID']))
elif recordCount == 0:
symbol_company = getStockSymbols(depository_list['symbol'])
print("\nSymbol Name: {}".format(symbol_company))
print("Quantity: {}".format(depository_list['quantity']))
print("Price: {}".format(depository_list['price']))
print("Trading Date: {}".format(depository_list['tradingDate']))
print("Customer ID: {}".format(depository_list['customerID']))
elif errorCode == '010041':
print("OTP has expired.\nYou will receiving a SMS")
else:
print(serviceRespHeader['ErrorText'])
getCustomerStocks()
Created with the Personal Edition of HelpNDoc: Easily create Web Help sites