Exemple #1
0
        public async static Task <HttpResponseMessage> GetStockData([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req, ILogger log)
        {
            //Get symbol
            log.LogInformation("New request received");
            string symbol = req.Query["symbol"];

            if (symbol == null || symbol == "")
            {
                log.LogInformation("Parameter 'symbol' not present as part of request. Ending.");
                HttpResponseMessage hrm = new HttpResponseMessage(HttpStatusCode.BadRequest);
                hrm.Content = new StringContent("Fatal failure. Parameter 'symbol' not provided. You must provide a stock symbol to return data for.");
                return(hrm);
            }
            symbol = symbol.Trim().ToUpper();
            log.LogInformation("Symbol requested: '" + symbol + "'");


            //Get Data
            bool   SummaryData            = false;
            bool   StatisticalData        = false;
            int    TryCount               = 1;
            string SummaryData_String     = req.Query["summary"];
            string StatisticalData_String = req.Query["statistics"];
            string TryCount_String        = req.Query["trycount"];

            if (SummaryData_String != null)
            {
                if (SummaryData_String.ToLower() == "true")
                {
                    SummaryData = true;
                }
            }
            if (StatisticalData_String != null)
            {
                if (StatisticalData_String.ToLower() == "true")
                {
                    StatisticalData = true;
                }
            }
            if (TryCount_String != null)
            {
                if (TryCount_String != "")
                {
                    try
                    {
                        TryCount = Convert.ToInt32(TryCount_String);
                    }
                    catch
                    {
                        string errormsg = "Unable to convert TryCount '" + TryCount_String + "' to integer.";
                        log.LogError(errormsg);
                        HttpResponseMessage hrm = new HttpResponseMessage(HttpStatusCode.BadRequest);
                        hrm.Content = new StringContent(errormsg);
                        return(hrm);
                    }
                }
            }

            //Log data
            log.LogInformation("Summary request: " + SummaryData.ToString());
            log.LogInformation("Statistics request: " + StatisticalData.ToString());
            log.LogInformation("Try count: " + TryCount.ToString());

            Equity e = Equity.Create(symbol);

            //Try to download summary data (if wanted)
            if (SummaryData)
            {
                int SummaryDataTimesTried = 0;
                do
                {
                    try
                    {
                        log.LogInformation("Downloading summary data...");
                        await e.DownloadSummaryAsync();

                        log.LogInformation("Successfully downloaded summary data.");
                    }
                    catch
                    {
                        SummaryDataTimesTried = SummaryDataTimesTried + 1;
                        log.LogInformation("Summary data download attempt " + SummaryDataTimesTried.ToString() + " failed.");
                    }
                } while (e.Summary == null && SummaryDataTimesTried < TryCount);

                if (e.Summary == null)
                {
                    string error_message = "Fatal failure while downloading equity summary data.";
                    log.LogError(error_message);
                    HttpResponseMessage hrm = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                    hrm.Content = new StringContent(error_message);
                    return(hrm);
                }
            }

            //Try to download statistical data (if wanted)
            if (StatisticalData)
            {
                int StatisticalDataTimesTried = 0;

                do
                {
                    try
                    {
                        log.LogInformation("Downloading statistical data...");
                        await e.DownloadStatisticsAsync();

                        log.LogInformation("Successfully downloaded statistics data.");
                    }
                    catch
                    {
                        StatisticalDataTimesTried = StatisticalDataTimesTried + 1;
                        log.LogInformation("Statistical data download attempt " + StatisticalDataTimesTried.ToString() + " failed.");
                    }
                } while (e.Statistics == null && StatisticalDataTimesTried < TryCount);

                if (e.Statistics == null)
                {
                    string error_message = "Fatal failure while downloading equity statistical data.";
                    log.LogError(error_message);
                    HttpResponseMessage hrm = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                    hrm.Content = new StringContent(error_message);
                    return(hrm);
                }
            }

            log.LogInformation("Converting to JSON...");
            string ToReturnJson          = JsonConvert.SerializeObject(e);
            HttpResponseMessage ToReturn = new HttpResponseMessage(HttpStatusCode.OK);

            ToReturn.Content = new StringContent(ToReturnJson, Encoding.UTF8, "application/json");
            log.LogInformation("Returning");
            return(ToReturn);
        }