コード例 #1
0
    def test_a_create_unlinked_pa_template(self):
        unlinked_pa_template_parameters = UnlinkedPATemplateParameters(
            directory="Personal:SDKTests/DoNotModify/UnlinkedPATemplates/",
            template_type_id=
            "996E90B981AEE83F14029ED3D309FB3F03EC6E2ACC7FD42C22CBD5D279502CFD",
            description=
            "This is an unlinked PA template that only returns security level data",
            accounts=[
                PAIdentifier(id="SPN:SP50", holdingsmode="B&H"),
                PAIdentifier(id="MSCI_USA:984000", holdingsmode="B&H")
            ],
            benchmarks=[
                PAIdentifier(id="SPN:SP50", holdingsmode="B&H"),
                PAIdentifier(id="DJGX:AMERICAS", holdingsmode="B&H")
            ],
            columns=[
                PACalculationColumn(
                    id=
                    "BD1720474AB8A80BDD79777F5B9CA594F4151C0554E30F9C916BA73BFAFC1FE0",
                    statistics=["eb9d6d91416e4224bacadc261787e56f"])
            ],
            dates=PADateParameters(startdate="20200101",
                                   enddate="20201215",
                                   frequency="Monthly"),
            groups=[
                PACalculationGroup(
                    id=
                    "5BCFFD17598FAEBD88EB4934EFB5FEF53849867D607ECEF232CD42D3369BBBCA"
                )
            ],
            currencyisocode="USD",
            componentdetail="GROUPS",
            content=TemplateContentTypes(mandatory=["accounts", "benchmarks"],
                                         optional=[
                                             "groups", "columns",
                                             "currencyisocode",
                                             "componentdetail"
                                         ],
                                         locked=["dates"]))

        unlinked_pa_template_parameters_root = UnlinkedPATemplateParametersRoot(
            data=unlinked_pa_template_parameters)

        response = self.unlinked_pa_templates_api.create_unlinked_pa_templates(
            unlinked_pa_template_parameters_root=
            unlinked_pa_template_parameters_root)

        firsttemplate = response[0].data['id']

        self.assertEqual(response[1], 201, "Response should be 201 - Success")
        self.assertEqual(
            type(response[0].data), UnlinkedPATemplatePostSummary,
            "Response should be of UnlinkedPATemplatePostSummary type.")
        self.assertEqual(type(response[0].data['id']), str,
                         "Response should be of String type.")
        self.assertGreater(len(response[0].data['id']), 0,
                           "Response result should not be an empty list.")
        def create_calculation(test_context):
            print("Creating single unit calculation")
            components = self.components_api.get_pa_components(
                document="PA_DOCUMENTS:DEFAULT", _return_http_data_only=True)
            component_summary = ComponentSummary(
                name="Weights", category="Weights / Exposures")
            component_id = [
                id for id in list(components.data.keys())
                if components.data[id].name == component_summary.name
                and components.data[id].category == component_summary.category
            ][0]
            pa_accounts = [PAIdentifier(id="BENCH:SP50")]
            pa_benchmarks = [PAIdentifier(id="BENCH:R.1000")]
            pa_dates = PADateParameters(startdate="20180101",
                                        enddate="20181231",
                                        frequency="Monthly")

            pa_calculation_parameters = {
                "1":
                PACalculationParameters(componentid=component_id,
                                        accounts=pa_accounts,
                                        benchmarks=pa_benchmarks,
                                        dates=pa_dates)
            }

            pa_calculation_parameter_root = PACalculationParametersRoot(
                data=pa_calculation_parameters)

            post_and_calculate_response = self.pa_calculations_api.post_and_calculate(
                pa_calculation_parameters_root=pa_calculation_parameter_root)

            self.assertTrue(
                post_and_calculate_response[1] == 201
                or post_and_calculate_response[1] == 202,
                "Response for create_calculation should have been 201 or 202")

            if post_and_calculate_response[1] == 201:
                return {
                    "continue_workflow": False,
                    "next_request": None,
                    "test_context": None
                }
            elif post_and_calculate_response[1] == 202:
                test_context["calculation_id"] = post_and_calculate_response[
                    2]["X-Factset-Api-Calculation-Id"]
                return {
                    "continue_workflow": True,
                    "next_request": read_status_step_name,
                    "test_context": test_context
                }
    def test_c_delete_templated_pa_component(self):
        global parent_template_id

        templated_pa_component_parameters = TemplatedPAComponentParameters(
            directory="Personal:SDKTests/DoNotModify/TemplatedPAComponents/",
            parent_template_id=parent_template_id,
            description="This is a templated PA component",
            component_data=PAComponentData(
                accounts=[
                    PAIdentifier(id="SPN:SP50", holdingsmode="B&H"),
                    PAIdentifier(id="MSCI_USA:984000", holdingsmode="B&H")
                ],
                benchmarks=[
                    PAIdentifier(id="SPN:SP50", holdingsmode="B&H"),
                    PAIdentifier(id="DJGX:AMERICAS", holdingsmode="B&H")
                ],
                columns=[
                    PACalculationColumn(
                        id=
                        "BD1720474AB8A80BDD79777F5B9CA594F4151C0554E30F9C916BA73BFAFC1FE0",
                        statistics=["eb9d6d91416e4224bacadc261787e56f"])
                ],
                groups=[
                    PACalculationGroup(
                        id=
                        "5BCFFD17598FAEBD88EB4934EFB5FEF53849867D607ECEF232CD42D3369BBBCA"
                    )
                ],
                currencyisocode="USD",
                componentdetail="GROUPS"))

        templated_pa_component_parameters_root = TemplatedPAComponentParametersRoot(
            data=templated_pa_component_parameters)

        components = self.templated_pa_components_api.create_templated_pa_components(
            templated_pa_component_parameters_root=
            templated_pa_component_parameters_root)

        component_id = components[0].data['id']

        # delete templated PA component
        response = self.templated_pa_components_api.delete_templated_pa_components(
            id=component_id)

        self.assertEqual(response[1], 204, "Response should be 204 - Success")

        # delete unlinked template
        response = self.unlinked_pa_templates_api.delete_unlinked_pa_templates(
            id=parent_template_id)
コード例 #4
0
def main():
    config = Configuration()
    config.host = host
    config.username = fds_username
    config.password = fds_api_key
    config.discard_unknown_keys = True
    # add proxy and/or disable ssl verification according to your development environment
    # config.proxy = "<proxyUrl>"
    config.verify_ssl = False

    # Setting configuration to retry api calls on http status codes of 429 and 503.
    config.retries = Retry(total=3,
                           status=3,
                           status_forcelist=frozenset([429, 503]),
                           backoff_factor=2,
                           raise_on_status=False)

    api_client = ApiClient(config)

    components_api = ComponentsApi(api_client)

    try:
        pa_document_name = "PA_DOCUMENTS:DEFAULT"
        pa_component_name = "Weights"
        pa_component_category = "Weights / Exposures"
        portfolio = "BENCH:SP50"
        benchmark = "BENCH:R.1000"
        startdate = "20180101"
        enddate = "20181231"
        frequency = "Monthly"
        # uncomment the below code line to setup cache control; max-stale=0 will be a fresh adhoc run and the max-stale value is in seconds.
        # Results are by default cached for 12 hours; Setting max-stale=300 will fetch a cached result which is 5 minutes older.
        # cache_control = "max-stale=0"
        get_components_response = components_api.get_pa_components(
            document=pa_document_name)
        component_id = [
            id for id in list(get_components_response[0].data.keys())
            if get_components_response[0].data[id].name == pa_component_name
            and get_components_response[0].data[id].category ==
            pa_component_category
        ][0]
        print("PA Component Id: " + component_id)
        pa_accounts = [PAIdentifier(id=portfolio)]
        pa_benchmarks = [PAIdentifier(id=benchmark)]
        pa_dates = PADateParameters(startdate=startdate,
                                    enddate=enddate,
                                    frequency=frequency)

        pa_calculation_parameters = {
            "1":
            PACalculationParameters(componentid=component_id,
                                    accounts=pa_accounts,
                                    benchmarks=pa_benchmarks,
                                    dates=pa_dates),
            "2":
            PACalculationParameters(componentid=component_id,
                                    accounts=pa_accounts,
                                    benchmarks=pa_benchmarks,
                                    dates=pa_dates)
        }

        pa_calculation_parameter_root = PACalculationParametersRoot(
            data=pa_calculation_parameters)

        pa_calculations_api = PACalculationsApi(api_client)

        post_and_calculate_response = pa_calculations_api.post_and_calculate(
            pa_calculation_parameters_root=pa_calculation_parameter_root)
        # comment the above line and uncomment the below line to run the request with the cache_control header defined earlier
        # post_and_calculate_response = pa_calculations_api.post_and_calculate(pa_calculation_parameters_root=pa_calculation_parameter_root, cache_control=cache_control)
        if post_and_calculate_response[
                1] == 202 or post_and_calculate_response[1] == 200:
            calculation_id = post_and_calculate_response[0].data.calculationid
            print("Calculation Id: " + calculation_id)

            status_response = pa_calculations_api.get_calculation_status_by_id(
                id=calculation_id)

            while status_response[1] == 202 and (status_response[0].data.status
                                                 in ("Queued", "Executing")):
                max_age = '5'
                age_value = status_response[2].get("cache-control")
                if age_value is not None:
                    max_age = age_value.replace("max-age=", "")
                print('Sleeping: ' + max_age)
                time.sleep(int(max_age))
                status_response = pa_calculations_api.get_calculation_status_by_id(
                    calculation_id)

            for (calculation_unit_id,
                 calculation_unit) in status_response[0].data.units.items():
                if calculation_unit.status == "Success":
                    print("Calculation Unit Id: " + calculation_unit_id +
                          " Succeeded!!!")
                    result_response = pa_calculations_api.get_calculation_unit_result_by_id(
                        id=calculation_id, unit_id=calculation_unit_id)
                    output_calculation_result(result_response[0]['data'])
                else:
                    print("Calculation Unit Id:" + calculation_unit_id +
                          " Failed!!!")
                    print("Error message : " + str(calculation_unit.errors))
        else:
            print("Calculation creation failed")
            print("Error status : " + str(post_and_calculate_response[1]))
            print("Error message : " + str(post_and_calculate_response[0]))

    except ApiException as e:
        print("Api exception Encountered")
        print(e)
        exit()