-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetQuotes.cs
44 lines (38 loc) · 1.42 KB
/
GetQuotes.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace api.multifol.io
{
public class GetQuotes
{
private readonly ILogger<GetQuotes> _logger;
public GetQuotes(ILogger<GetQuotes> logger)
{
_logger = logger;
}
[Function("GetQuotes")]
public async Task<IActionResult> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req)
{
_logger.LogInformation("GetQuotes 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://eodhd.com/api/real-time/{ticker}?fmt=json&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);
}
}
}
}