From 8131e16395ab1fedd801491ca3e65f84c2c1f175 Mon Sep 17 00:00:00 2001 From: Rob Relyea Date: Tue, 20 Jun 2023 05:13:35 -0700 Subject: [PATCH] add SearchTicker azure function --- api/GetQuotes.cs | 2 +- api/SearchTicker.cs | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 api/SearchTicker.cs diff --git a/api/GetQuotes.cs b/api/GetQuotes.cs index 54747e7..40f7238 100644 --- a/api/GetQuotes.cs +++ b/api/GetQuotes.cs @@ -18,7 +18,7 @@ public static async Task Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { - log.LogInformation("C# HTTP trigger function processed a request."); + log.LogInformation("GetQuotes processed a request."); string ticker = req.Query["ticker"]; string apikey = req.Query["apikey"]; diff --git a/api/SearchTicker.cs b/api/SearchTicker.cs new file mode 100644 index 0000000..74e4925 --- /dev/null +++ b/api/SearchTicker.cs @@ -0,0 +1,42 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Azure.WebJobs; +using Microsoft.Azure.WebJobs.Extensions.Http; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using System.Net.Http; + +namespace api +{ + public static class SearchTicker + { + [FunctionName("SearchTicker")] + public static async Task Run( + [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, + ILogger log) + { + log.LogInformation("SearchTicker processed a request."); + + string ticker = req.Query["ticker"]; + string apikey = req.Query["apikey"]; + + string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); + dynamic data = JsonConvert.DeserializeObject(requestBody); + ticker = ticker ?? data?.ticker; + apikey = apikey ?? data?.apikey; + + string url = $"https://eodhistoricaldata.com/api/search/{ticker}?api_token={apikey}"; + var httpClient = new HttpClient(); + try { + var quoteDataJson = await httpClient.GetStringAsync(url); + return new OkObjectResult(quoteDataJson); + } + catch (Exception ex) { + return new BadRequestObjectResult(ex.GetType().Name + ": " + ex.Message); + } + } + } +}