1+ import json
2+ import sys
3+ import time
4+ import pandas as pd
5+ import uuid
6+ import os
7+
8+ from fds .analyticsapi .engines .api .fi_calculations_api import FICalculationsApi
9+ from fds .analyticsapi .engines .api_client import ApiClient
10+ from fds .analyticsapi .engines .configuration import Configuration
11+ from fds .analyticsapi .engines .models .fi_calculation_parameters import FICalculationParameters
12+ from fds .analyticsapi .engines .models .fi_security import FISecurity
13+ from fds .analyticsapi .engines .models .fi_job_settings import FIJobSettings
14+ from fds .analyticsapi .engines .stach_extensions import StachExtensions
15+ from fds .protobuf .stach .Package_pb2 import Package
16+
17+ from google .protobuf import json_format
18+ from urllib3 import Retry
19+
20+ host = "https://api.factset.com"
21+ username = os .environ ["ANALYTICS_API_USERNAME_SERIAL" ]
22+ password = os .environ ["ANALYTICS_API_PASSWORD" ]
23+
24+
25+ def main ():
26+ config = Configuration ()
27+ config .host = host
28+ config .username = username
29+ config .password = password
30+ # add proxy and/or disable ssl verification according to your development environment
31+ # config.proxy = "<proxyUrl>"
32+ config .verify_ssl = False
33+
34+ # Setting configuration to retry api calls on http status codes of 429 and 503.
35+ config .retries = Retry (total = 3 , status = 3 , status_forcelist = frozenset ([429 , 503 ]), backoff_factor = 2 ,
36+ raise_on_status = False )
37+
38+ api_client = ApiClient (config )
39+
40+ calculations = ["Security Type" ,
41+ "Security Name" ,
42+ "Run Status" ,
43+ "Elapse Time (seconds)" ,
44+ "Calc From Method" ,
45+ "Option Pricing Model" ,
46+ "Yield Curve Date" ,
47+ "Settlement Date" ,
48+ "Discount Curve" ,
49+ "Price" ,
50+ "Yield to No Call" ,
51+ "OAS" ,
52+ "Effective Duration" ,
53+ "Effective Convexity" ]
54+
55+ security1 = FISecurity ("Price" , 100.285 , 10000.0 , "912828ZG8" , "20201202" , "UST" )
56+ security2 = FISecurity ("Price" , 101.138 , 200000.0 , "US037833AR12" , "20201203" , "UST" )
57+
58+ securities = [security1 , security2 ]
59+
60+ jobSettings = FIJobSettings ("20201201" )
61+
62+ fi_calculation_parameters = FICalculationParameters (securities , calculations , jobSettings )
63+
64+ print (fi_calculation_parameters )
65+
66+ fi_calculations_api = FICalculationsApi (api_client )
67+ run_calculation_response = fi_calculations_api .run_fi_calculation_with_http_info (
68+ fi_calculation_parameters = fi_calculation_parameters )
69+
70+ if run_calculation_response [1 ] != 202 and run_calculation_response [1 ] != 201 :
71+ print_error (run_calculation_response )
72+ sys .exit ()
73+
74+ if run_calculation_response [1 ] == 201 :
75+ print_result (run_calculation_response [0 ])
76+ sys .exit ()
77+
78+ calculation_id = run_calculation_response [2 ].get ("location" ).split ("/" )[- 1 ]
79+ print ("Calculation Id: " + calculation_id )
80+
81+ status_response = fi_calculations_api .get_fi_calculation_by_id_with_http_info (calculation_id )
82+ while status_response [1 ] == 202 :
83+ max_age = '5'
84+ age_value = status_response [2 ].get ("cache-control" )
85+ if age_value is not None :
86+ max_age = age_value .replace ("max-age=" , "" )
87+ print ('Sleeping: ' + max_age )
88+ time .sleep (int (max_age ))
89+ status_response = fi_calculations_api .get_fi_calculation_by_id_with_http_info (calculation_id )
90+
91+ if status_response [1 ] != 200 :
92+ print_error (status_response )
93+ sys .exit ()
94+
95+ print_result (status_response [0 ])
96+
97+
98+ def print_result (response ):
99+ # converting the data to Package object
100+ result = json_format .Parse (json .dumps (response ), Package ())
101+ # print(MessageToJson(result)) # To print the result object as a JSON
102+ # print(MessageToDict(result)) # To print the result object as a Dictionary
103+ tables = StachExtensions .convert_to_table_format (result ) # To convert result to 2D tables.
104+ print (tables [0 ]) # Prints the result in 2D table format.
105+ # generate_excel(result) # Uncomment this line to get the result in table format exported to excel file.
106+
107+
108+ def generate_excel (package ):
109+ for table in StachExtensions .convert_to_table_format (package ):
110+ writer = pd .ExcelWriter (str (uuid .uuid1 ()) + ".xlsx" )
111+ table .to_excel (excel_writer = writer )
112+ writer .save ()
113+ writer .close ()
114+
115+
116+ def print_error (response ):
117+ print ("Calculation Failed!!!" )
118+ print ("Status Code: " + str (response [1 ]))
119+ print ("Request Key: " + response [2 ].get ("x-datadirect-request-key" ))
120+ print (response [0 ])
121+
122+
123+ if __name__ == '__main__' :
124+ main ()
0 commit comments