-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simple met.no integration for weather data. Requires attribution in w…
…eb app for use
- Loading branch information
Showing
4 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
handlenett-backend/web-api/HandlenettAPI/Controllers/WeatherController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using HandlenettAPI.Services; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace HandlenettAPI.Controllers | ||
{ | ||
[Authorize] | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class WeatherController : ControllerBase | ||
{ | ||
private Uri yrLogo = new Uri("https://www.yr.no/assets/images/logo-yr--circle.svg"); | ||
|
||
private readonly WeatherService _weatherService; | ||
|
||
public WeatherController(WeatherService weatherService) | ||
{ | ||
_weatherService = weatherService; | ||
} | ||
|
||
[HttpGet] //Default lat/long for Miles Haugesund | ||
public async Task<IActionResult> GetWeather(double latitude = 59.408175834503076, double longitude = 5.273991771778363) | ||
{ | ||
return Ok(await _weatherService.GetWeatherByCoordinatesAsync(latitude, longitude)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace HandlenettAPI.Models | ||
{ | ||
public class Weather | ||
{ | ||
public string Temperature { get; set; } | ||
public Uri ImageUri { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
handlenett-backend/web-api/HandlenettAPI/Services/WeatherService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using HandlenettAPI.Models; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace HandlenettAPI.Services | ||
{ | ||
public class WeatherService | ||
{ | ||
private readonly HttpClient _httpClient; | ||
|
||
public WeatherService(HttpClient httpClient) | ||
{ | ||
_httpClient = httpClient; | ||
_httpClient.DefaultRequestHeaders.Add("User-Agent", "Miles Haugesund Handlenett/1.0 ([email protected])"); | ||
} | ||
|
||
public async Task<Weather> GetWeatherByCoordinatesAsync(double latitude, double longitude) | ||
{ | ||
var returnModel = new Weather(); | ||
var url = $"https://api.met.no/weatherapi/locationforecast/2.0/compact?lat={latitude}&lon={longitude}"; | ||
|
||
var response = await _httpClient.GetAsync(url); | ||
|
||
if (response.IsSuccessStatusCode) | ||
{ | ||
var content = await response.Content.ReadAsStringAsync(); | ||
var json = JObject.Parse(content); | ||
|
||
returnModel.Temperature = json["properties"]["timeseries"][0]["data"]["instant"]["details"]["air_temperature"].ToString(); | ||
var symbol = json["properties"]["timeseries"][0]["data"]["next_1_hours"]["summary"]["symbol_code"].ToString(); | ||
//returnModel.ImageUri = new Uri($"https://api.met.no/weatherapi/weathericon/2.0/?symbol={symbol}&content_type=image/png"); | ||
//https://github.com/metno/weathericons | ||
return returnModel; | ||
} | ||
else | ||
{ | ||
throw new Exception ("Unable to retrieve weather data from MET Norway."); | ||
} | ||
} | ||
|
||
|
||
} | ||
} |