This repository was archived by the owner on Dec 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIotDashboardTrigger.cs
69 lines (57 loc) · 2.07 KB
/
IotDashboardTrigger.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace ca.jeffrey
{
public static class IotDashboardTrigger
{
private static FinanceHttpClient financeHttpClient = new FinanceHttpClient();
private static WeatherHttpClient weatherHttpClient = new WeatherHttpClient();
[FunctionName("IotDashboardTrigger")]
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
// Parse query parameters
string currencyPair = req.Query["currencypair"];
string ticker = req.Query["stock"];
string location = req.Query["location"];
if (currencyPair == null || ticker == null || location == null)
{
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}
// Get information
float exchangeRate = await financeHttpClient.GetClosingPrice(currencyPair);
float stockPrice = await financeHttpClient.GetClosingPrice(ticker);
WeatherModel weatherModel = await weatherHttpClient.GetWeather(location);
string condition = weatherModel.Condition;
float temp = weatherModel.Temp;
float tempMin = weatherModel.TempMin;
float tempMax = weatherModel.TempMax;
// Construct response object
var responseObj = new
{
exchange_rate = exchangeRate,
stock_price = stockPrice,
condition = condition,
temp = temp,
temp_min = tempMin,
temp_max = tempMax
};
var responseJson = JsonConvert.SerializeObject(responseObj);
// Return response message
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(responseJson, Encoding.UTF8, "application/json")
};
}
}
}