|
| 1 | +import json |
| 2 | +import sys |
| 3 | +import time |
| 4 | + |
| 5 | +from fds.analyticsapi.engines import ComponentSummary |
| 6 | +from fds.analyticsapi.engines.api.components_api import ComponentsApi |
| 7 | +from fds.analyticsapi.engines.api_client import ApiClient |
| 8 | +from fds.analyticsapi.engines.configuration import Configuration |
| 9 | +from fds.analyticsapi.engines.api.spar_calculations_api import SPARCalculationsApi |
| 10 | +from fds.analyticsapi.engines.models.spar_date_parameters import SPARDateParameters |
| 11 | +from fds.analyticsapi.engines.models.spar_calculation_parameters import SPARCalculationParameters |
| 12 | +from fds.analyticsapi.engines.models.spar_identifier import SPARIdentifier |
| 13 | +from fds.protobuf.stach.Package_pb2 import Package |
| 14 | + |
| 15 | +from google.protobuf import json_format |
| 16 | +from google.protobuf.json_format import MessageToJson |
| 17 | +from google.protobuf.json_format import MessageToDict |
| 18 | +from urllib3 import Retry |
| 19 | + |
| 20 | +# Copy 'Converting API output to Table Format' snippet to a file with name 'stach_extensions.py' to use below import statement |
| 21 | +from stach_extensions import StachExtensions |
| 22 | + |
| 23 | +def print_result(response): |
| 24 | + # converting the data to Package object |
| 25 | + result = json_format.Parse(json.dumps(response), Package()) |
| 26 | + # print(MessageToJson(result)) # To print the result object as a JSON |
| 27 | + # print(MessageToDict(result)) # To print the result object as a Dictionary |
| 28 | + tables = StachExtensions.convert_to_table_format(result) # To convert result to 2D tables. |
| 29 | + print(tables[0]) # Prints the result in 2D table format. |
| 30 | + # StachExtensions.generate_excel(result) # To get the result in table format exported to excel file. |
| 31 | + |
| 32 | +def print_error(response): |
| 33 | + print("Calculation Failed!!!") |
| 34 | + print("Status Code: " + str(response[1])) |
| 35 | + print("Request Key: " + response[2].get("x-datadirect-request-key")) |
| 36 | + print(response[0]) |
| 37 | + |
| 38 | +host = "https://api.factset.com" |
| 39 | +username = "<username-serial>" |
| 40 | +password = "<apiKey>" |
| 41 | + |
| 42 | +spar_document_name = "pmw_root:/spar_documents/Factset Default Document" |
| 43 | +spar_component_name = "Returns Table" |
| 44 | +spar_component_category = "Raw Data / Returns" |
| 45 | +spar_benchmark_r_1000 = "R.1000" |
| 46 | +spar_benchmark_russell_pr_2000 = "RUSSELL_P:R.2000" |
| 47 | +spar_benchmark_russell_prefix = "RUSSELL" |
| 48 | +spar_benchmark_russell_return_type = "GTR" |
| 49 | +startdate = "20180101" |
| 50 | +enddate = "20181231" |
| 51 | +frequency = "Monthly" |
| 52 | + |
| 53 | +config = Configuration() |
| 54 | +config.host = host |
| 55 | +config.username = username |
| 56 | +config.password = password |
| 57 | +# add proxy and/or disable ssl verification according to your development environment |
| 58 | +# config.proxy = "<proxyUrl>" |
| 59 | +config.verify_ssl = False |
| 60 | + |
| 61 | +# Setting configuration to retry api calls on http status codes of 429 and 503. |
| 62 | +config.retries = Retry(total=3, status=3, status_forcelist=frozenset([429, 503]), backoff_factor=2, raise_on_status=False) |
| 63 | + |
| 64 | +api_client = ApiClient(config) |
| 65 | + |
| 66 | +components_api = ComponentsApi(api_client) |
| 67 | + |
| 68 | +components = components_api.get_spar_components(spar_document_name) |
| 69 | +component_desc = ComponentSummary(name=spar_component_name, category=spar_component_category) |
| 70 | +component_id = [id for id in list(components.keys()) if components[id] == component_desc][0] |
| 71 | + |
| 72 | +spar_account_identifier = SPARIdentifier(spar_benchmark_r_1000, spar_benchmark_russell_return_type, spar_benchmark_russell_prefix) |
| 73 | +spar_accounts = [spar_account_identifier] |
| 74 | +spar_benchmark_identifier = SPARIdentifier(spar_benchmark_russell_pr_2000, spar_benchmark_russell_return_type, spar_benchmark_russell_prefix) |
| 75 | +spar_dates = SPARDateParameters(startdate, enddate, frequency) |
| 76 | + |
| 77 | + |
| 78 | +spar_calculation_parameters = SPARCalculationParameters(component_id, spar_accounts, spar_benchmark_identifier, spar_dates) |
| 79 | + |
| 80 | +print(spar_calculation_parameters) |
| 81 | + |
| 82 | +spar_calculations_api = SPARCalculationsApi(api_client) |
| 83 | +run_calculation_response = spar_calculations_api.run_spar_calculation_with_http_info(spar_calculation_parameters=spar_calculation_parameters) |
| 84 | + |
| 85 | +if run_calculation_response[1] != 202 and run_calculation_response[1] != 201: |
| 86 | + print_error(run_calculation_response) |
| 87 | + sys.exit() |
| 88 | + |
| 89 | +if run_calculation_response[1] == 201: |
| 90 | + print_result(run_calculation_response[0]) |
| 91 | + sys.exit() |
| 92 | + |
| 93 | + |
| 94 | +calculation_id = run_calculation_response[2].get("location").split("/")[-1] |
| 95 | +print("Calculation Id: " + calculation_id) |
| 96 | + |
| 97 | +status_response = spar_calculations_api.get_spar_calculation_by_id_with_http_info(calculation_id) |
| 98 | +while status_response[1] == 202: |
| 99 | + max_age = '5' |
| 100 | + age_value = status_response[2].get("cache-control") |
| 101 | + if age_value is not None: |
| 102 | + max_age = age_value.replace("max-age=", "") |
| 103 | + print('Sleeping: ' + max_age) |
| 104 | + time.sleep(int(max_age)) |
| 105 | + status_response = spar_calculations_api.get_spar_calculation_by_id_with_http_info(calculation_id) |
| 106 | + |
| 107 | +if status_response[1] != 200: |
| 108 | + print_error(status_response) |
| 109 | + sys.exit() |
| 110 | + |
| 111 | +print_result(status_response[0]) |
0 commit comments