Skip to content

Commit

Permalink
Simple met.no integration for weather data. Requires attribution in w…
Browse files Browse the repository at this point in the history
…eb app for use
  • Loading branch information
Torkelsen committed Oct 18, 2024
1 parent 8d06abd commit 482eb81
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
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));
}
}
}
8 changes: 8 additions & 0 deletions handlenett-backend/web-api/HandlenettAPI/Models/Weather.cs
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; }
}
}
2 changes: 1 addition & 1 deletion handlenett-backend/web-api/HandlenettAPI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
builder.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod().WithOrigins("http://localhost:3000", "http://localhost:3000");
});
});

builder.Services.AddHttpClient<WeatherService>();

builder.Services.AddHttpClient("SlackClient", client =>
{
Expand Down
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.");
}
}


}
}

0 comments on commit 482eb81

Please sign in to comment.