Skip to content

Instantly share code, notes, and snippets.

@TimSmith714
Created July 28, 2021 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TimSmith714/940e135103eda7c34cae160df790cfed to your computer and use it in GitHub Desktop.
Save TimSmith714/940e135103eda7c34cae160df790cfed to your computer and use it in GitHub Desktop.
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
// Get Weather Data
string weatherQueryURL = "http://api.openweathermap.org/data/2.5/weather?appid=[[KEYHERE]]&units=metric&q=[[LOCATION HERE]]";
var weatherClient = new HttpClient();
weatherClient.BaseAddress = new Uri(weatherQueryURL);
var weatherRequest = new HttpRequestMessage(HttpMethod.Get, "");
var response = await weatherClient.SendAsync(weatherRequest);
string responseBody = await response.Content.ReadAsStringAsync();
JObject weatherResponse = JObject.Parse(responseBody);
var weatherData = new WeatherObject()
{
weatherId = (int)weatherResponse["weather"][0]["id"],
temperature = (int)weatherResponse["main"]["temp"]
};
// Get DateTime Data
// Sorted to local with https://stackoverflow.com/questions/52524972/why-is-my-azure-function-app-timestamp-an-hour-less
TimeZoneInfo ukTimeZone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
DateTime currentTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, ukTimeZone);
var dateTimeData = new TimeObject()
{
year = currentTime.Year,
month = currentTime.Month,
day = currentTime.Day,
hour = currentTime.Hour,
minute = currentTime.Minute
};
var responseData = new ResponseObject()
{
dateTime = dateTimeData,
weather = weatherData
};
return new OkObjectResult(responseData);
}
public class TimeObject
{
public int year {get; set;}
public int month {get; set;}
public int day {get; set;}
public int hour {get; set;}
public int minute {get; set;}
}
public class WeatherObject
{
public int weatherId {get; set;}
public int temperature {get; set;}
}
public class ResponseObject
{
public TimeObject dateTime {get; set;}
public WeatherObject weather {get; set;}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment