Ejemplo n.º 1
0
        public async Task <IActionResult> GetTopStocksAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "stocks/{symbol}")]
                                                            HttpRequest request, string symbol)
        {
            var getTopStocksResponse = await _stockQuoteService.GetQuoteAsync(new GetStockQuoteRequest(symbol));

            if (getTopStocksResponse.Status)
            {
                return(new OkObjectResult(getTopStocksResponse.Data));
            }

            _logger.LogError("Error when getting quote for: {symbol}", symbol);
            return(new InternalServerErrorResult());
        }
Ejemplo n.º 2
0
        //public static async Task<IActionResult> Run(
        //public async Task<String> GetQuote(
        public async Task <IActionResult> GetQuote(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            Stock     stockSymbol = null;
            QuoteData response    = null;


            try
            {
                stockSymbol = XmlUtility.GetStockDetails(req);
            } catch (Exception e)
            {
                /*
                 * The 422(Unprocessable Entity) status code means the server understands the content type of the request
                 * entity(hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request
                 * entity is correct(thus a 400(Bad Request) status code is inappropriate) but was unable to process the
                 * contained instructions. For example, this error condition may occur if an XML request body contains
                 * well - formed(i.e., syntactically correct), but semantically erroneous, XML instructions.
                 *
                 * BadRequestObjectResult resp = new BadRequestObjectResult("Invalid Input");
                 * resp.StatusCode = 422;
                 *
                 */
                log.LogInformation($"critical error: {e.Message}");
                return(new BadRequestObjectResult("Invalid Input")
                {
                    StatusCode = 422
                });
            }

            try
            {
                await policy.Execute(async() =>
                {
                    // Call a Webservice
                    response = await _stockQuoteService.GetQuoteAsync(stockSymbol.stockSymbol, stockSymbol.licenseKey).ConfigureAwait(false);

                    //  response = null;

                    // Force a retry
                    if (response == null)
                    {
                        throw new Exception("http request failed");
                    }

                    // Handle result
                    //log.LogInformation($" result: {response.StatusCode}");
                });
            }
            catch (Exception e)
            {
                // Can't recover at this point
                log.LogInformation($"critical error: {e.Message}");
            }

            log.LogInformation($"{response} received.");

            return((ActionResult) new OkObjectResult(XmlUtility.ObjectToXmlString(response, typeof(QuoteData))));

            //return XmlUtility.ObjectToXmlString(response, typeof(QuoteData));
            //return ObjectToXmlString_second(response, typeof(QuoteData));
        }