Esempio n. 1
0
        public void CalculationsWorkflow_Should_Succeed_When_GivenValidInputs()
        {
            var calculations = new List <string>();
            var securities   = new List <FISecurity>();

            var jobSettings = new FIJobSettings(asOfDate: "20201201");

            var fiCalculationParameters     = new FICalculationParameters(securities, calculations, jobSettings);
            var fiCalculationParametersRoot = new FICalculationParametersRoot(data: fiCalculationParameters);

            Console.WriteLine(JsonConvert.SerializeObject(fiCalculationParametersRoot));

            try
            {
                var postAndOptimizeHttpResponse =
                    fiCalculationsApi.PostAndCalculateWithHttpInfo(fICalculationParametersRoot: fiCalculationParametersRoot);

                postAndOptimizeHttpResponse.StatusCode
                .Should()
                .Match <HttpStatusCode>(c => c == HttpStatusCode.Created || c == HttpStatusCode.Accepted || c == HttpStatusCode.OK);

                switch (postAndOptimizeHttpResponse.StatusCode)
                {
                case HttpStatusCode.Accepted:
                    var optimizationId =
                        postAndOptimizeHttpResponse.Headers["X-Factset-Api-Calculation-Id"].First();
                    bool shouldPoll;
                    do
                    {
                        var pollingResponse = fiCalculationsApi.GetCalculationStatusByIdWithHttpInfo(optimizationId);
                        pollingResponse.StatusCode
                        .Should()
                        .Match <HttpStatusCode>(c =>
                                                c == HttpStatusCode.Created || c == HttpStatusCode.Accepted);

                        if (pollingResponse.StatusCode == HttpStatusCode.Created)
                        {
                            break;
                        }

                        shouldPoll = pollingResponse.StatusCode == HttpStatusCode.Accepted;

                        if (pollingResponse.Headers.ContainsKey("Cache-Control"))
                        {
                            var maxAge = pollingResponse.Headers["Cache-Control"][0];
                            if (string.IsNullOrWhiteSpace(maxAge))
                            {
                                Console.WriteLine("Sleeping for 2 seconds");
                                // Sleep for at least 2 seconds.
                                Thread.Sleep(2000);
                            }
                            else
                            {
                                var age = int.Parse(maxAge.Replace("max-age=", ""));
                                Console.WriteLine($"Sleeping for {age} seconds");
                                Thread.Sleep(age * 1000);
                            }
                        }
                    } while (shouldPoll);

                    var getResultResponse = fiCalculationsApi.GetCalculationResultWithHttpInfo(optimizationId);
                    getResultResponse.StatusCode.Should().Be(HttpStatusCode.OK);
                    getResultResponse.Data.Should().NotBeNull();
                    break;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        public static void Main(string[] args)
        {
            try
            {
                var fiSecurities = new List <FISecurity>
                {
                    new FISecurity(
                        calcFromMethod: "Price",
                        calcFromValue: 10000.0,
                        face: 10000.0,
                        symbol: "912828ZG8",
                        settlement: "20201202",
                        discountCurve: "UST"
                        ),
                    new FISecurity(
                        calcFromMethod: "Price",
                        calcFromValue: 101.138,
                        face: 200000.0,
                        symbol: "US037833AR12",
                        settlement: "20201203",
                        discountCurve: "UST"
                        )
                };

                var fiCalculations = new List <string>
                {
                    "Security Type",
                    "Security Name",
                    "Run Status",
                    "Elapse Time (seconds)",
                    "Calc From Method",
                    "Option Pricing Model",
                    "Yield Curve Date",
                    "Settlement Date",
                    "Discount Curve",
                    "Price",
                    "Yield to No Call",
                    "OAS",
                    "Effective Duration",
                    "Effective Convexity"
                };

                var fiJobSettings = new FIJobSettings("20201201");

                var fiCalculationParameters      = new FICalculationParameters(fiSecurities, fiCalculations, fiJobSettings);
                var fiCalculationParameterseRoot = new FICalculationParametersRoot(data: fiCalculationParameters);


                var fiCalculationsApi = new FICalculationsApi(GetApiConfiguration());

                var calculationResponse = fiCalculationsApi.PostAndCalculateWithHttpInfo(null, null, fiCalculationParameterseRoot);
                // Comment the above line and uncomment the below lines to add cache control configuration. Results are by default cached for 12 hours; Setting max-stale=300 will fetch a cached result which is at max 5 minutes older.
                //var cacheControl = "max-stale=300";
                //var calculationResponse = fiCalculationsApi.PostAndCalculateWithHttpInfo(null, cacheControl, fiCalculationParameterseRoot);

                switch (calculationResponse.StatusCode)
                {
                case HttpStatusCode.Created:
                    var result = (ObjectRoot)calculationResponse.Data;
                    PrintResult(result);
                    return;

                case HttpStatusCode.Accepted:
                    var calculationStatus = (CalculationInfoRoot)calculationResponse.Data;
                    var calculationId     = calculationStatus.Data.CalculationId;
                    Console.WriteLine("Calculation Id: " + calculationId);
                    var optimizationStatusResponse =
                        fiCalculationsApi.GetCalculationStatusByIdWithHttpInfo(calculationId);
                    var continuePolling = optimizationStatusResponse.StatusCode == HttpStatusCode.Accepted;
                    while (continuePolling)
                    {
                        if (optimizationStatusResponse.StatusCode == HttpStatusCode.Accepted)
                        {
                            if (optimizationStatusResponse.Headers.ContainsKey("Cache-Control"))
                            {
                                var maxAge = optimizationStatusResponse.Headers["Cache-Control"][0];
                                if (string.IsNullOrWhiteSpace(maxAge))
                                {
                                    Console.WriteLine("Sleeping for 2 seconds");
                                    // Sleep for at least 2 seconds.
                                    Thread.Sleep(2000);
                                }
                                else
                                {
                                    var age = int.Parse(maxAge.Replace("max-age=", ""));
                                    Console.WriteLine($"Sleeping for {age} seconds");
                                    Thread.Sleep(age * 1000);
                                }
                            }

                            optimizationStatusResponse =
                                fiCalculationsApi.GetCalculationStatusByIdWithHttpInfo(calculationId);
                        }
                        else
                        {
                            continuePolling = false;
                        }
                    }

                    if (optimizationStatusResponse.StatusCode == HttpStatusCode.Created)
                    {
                        Console.WriteLine("Calculation Id: " + calculationId + " succeeded!");
                        PrintResult((ObjectRoot)optimizationStatusResponse.Data);
                    }
                    else
                    {
                        Console.WriteLine("Calculation Id: " + calculationId + " failed!");
                        Console.WriteLine("Error message: " + optimizationStatusResponse.ErrorText);
                    }

                    break;
                }
            }
            catch (ApiException e)
            {
                Console.WriteLine($"Status Code: {e.ErrorCode}");
                Console.WriteLine($"Reason : {e.Message}");
                Console.WriteLine(e.StackTrace);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }