Open FX Forward Contract
We will first import the "request" and "json" module, as we need those module to call 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.
Next, we will create a function called openFXForwardContract.
Inside the function, we will create the input variables.
Afterwards, we will 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 display the result.
If the errorCode is '010041', it means that the OTP provided has expired. We will then display the message 'OTP has expired. You will be receiving a SMS'.
Else, we will display the ErrorText.
Entire Code:
import requests, json
from functions import url
def openFXForwardContract():
#Header
serviceName = 'openFXForwardContract'
userID = 'alan'
PIN = '987654'
OTP = '987654'
#Content
openDate = "2019-08-01";
maturityDate = "2020-02-01";
period = "6M";
amount = "6000";
openDate2 = "2020-02-01";
maturityDate2 = "2020-05-01";
period2 = "3M";
amount2 = "3000";
openDate3 = "2020-05-01";
maturityDate3 = "2020-06-01";
period3 = "1M";
amount3 = "1000";
openDate4 = "2020-06-01";
maturityDate4 = "2020-07-01";
period4 = "1M";
amount4 = "1000";
referenceNumber = "19";
baseCurrencyAccountID = "468";
quoteCurrencyAccountID = "5458";
baseCurrency = "USD";
quoteCurrency = "GBP";
headerObj = {
'Header': {
'serviceName': serviceName,
'userID': userID,
'PIN': PIN,
'OTP': OTP
}
}
FXForwardSegment1Obj = {
'openDate' : openDate,
'maturityDate' : maturityDate,
'period' : period,
'amount' : amount
}
FXForwardSegment2Obj = {
'openDate' : openDate2,
'maturityDate' : maturityDate2,
'period' : period2,
'amount' : amount2
}
FXForwardSegment3Obj = {
'openDate' : openDate3,
'maturityDate' : maturityDate3,
'period' : period3,
'amount' : amount3
}
FXForwardSegment4Obj = {
'openDate' : openDate4,
'maturityDate' : maturityDate4,
'period' : period4,
'amount' : amount4
}
List = [FXForwardSegment1Obj,FXForwardSegment2Obj,FXForwardSegment3Obj,FXForwardSegment4Obj]
contentObj = {
'Content': {
'referenceNumber': referenceNumber,
'baseCurrencyAccountID': baseCurrencyAccountID,
'quoteCurrencyAccountID': quoteCurrencyAccountID,
'baseCurrency': baseCurrency,
'quoteCurrency': quoteCurrency,
'FXForwardSegment' : List
}
}
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':
FXforwardContract = response.json()['Content']['ServiceResponse']['FXForwardContract']
print("You have successfully opened a FX Forward Contract. The Account ID is {}.".format(FXforwardContract['FX_ForwardID']))
print (contentObj)
elif errorCode == '010041':
print("OTP has expired.\nYou will receiving a SMS")
else:
print(serviceRespHeader['ErrorText'])
openFXForwardContract()
Created with the Personal Edition of HelpNDoc: Easily create CHM Help documents