Add weather result

This commit is contained in:
Thomas Fransolet 2024-02-24 22:42:41 +01:00
parent 78f402a840
commit 8cd69c7d8b
4 changed files with 74 additions and 3 deletions

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Manager.Interfaces.Models
{
public class CityData
{
//[JsonProperty("lat")]
public double Lat { get; set; }
//[JsonProperty("lon")]
public double Lon { get; set; }
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Manager.Helpers;
@ -9,6 +10,7 @@ using Manager.Interfaces.Models;
using Manager.Services;
using ManagerService.Service.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
@ -116,7 +118,7 @@ namespace ManagerService.Controllers
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)]
[HttpGet("{id}")]
public ObjectResult GetDetail(string id)
public async Task<ObjectResult> GetDetailAsync(string id)
{
try
{
@ -127,6 +129,56 @@ namespace ManagerService.Controllers
List<string> sectionIds = _sectionService.GetAllIdsFromConfiguration(id);
if (configuration.WeatherCity != null && configuration.WeatherCity.Length >= 2 &&
(configuration.WeatherUpdatedDate == null || configuration.WeatherUpdatedDate.Value.AddHours(3) < DateTimeOffset.Now)) // Update all 4 hours
{
// Call Openweather api with token from appSettings and update result with json
var apiKey = _configuration.GetSection("OpenWeatherApiKey").Get<string>();
if (apiKey != null && apiKey.Length > 0)
{
string url = $"http://api.openweathermap.org/geo/1.0/direct?q={configuration.WeatherCity}&limit=1&appid={apiKey}";
using (HttpClient client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
List<CityData> cities = JsonConvert.DeserializeObject<List<CityData>>(responseBody);
if (cities.Count > 0)
{
double lat = cities[0].Lat;
double lon = cities[0].Lon;
//string onecallUrl = $"https://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&appid={apiKey}";
string callUrl = $"https://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&units=metric&appid={apiKey}";
HttpResponseMessage callResponse = await client.GetAsync(callUrl);
callResponse.EnsureSuccessStatusCode();
string callResponseBody = await callResponse.Content.ReadAsStringAsync();
configuration.WeatherUpdatedDate = DateTimeOffset.Now;
configuration.WeatherResult = callResponseBody;
_configurationService.Update(configuration.Id, configuration);
}
else
{
Console.WriteLine("Aucune ville trouvée.");
}
}
catch (HttpRequestException e)
{
Console.WriteLine($"Une erreur s'est produite lors de la requête HTTP : {e.Message}");
}
}
}
}
return new OkObjectResult(configuration.ToDTO(sectionIds));
}
catch (KeyNotFoundException ex)

View File

@ -252,7 +252,8 @@ namespace ManagerService.Controllers
}
stringResult = Convert.ToBase64String(fileBytes);
}
} else
}
else
{
throw new FileLoadException(message: "Fichier inexistant ou trop volumineux (max 4Mb)");
}

View File

@ -36,6 +36,7 @@
"UserName": "user1", //admin
"Password": "MyInfoMate2023!" //mdlf2021!
},
"SupportedLanguages": [ "FR", "NL", "EN", "DE", "IT", "ES", "PL", "CN", "AR", "UK" ]
"SupportedLanguages": [ "FR", "NL", "EN", "DE", "IT", "ES", "PL", "CN", "AR", "UK" ],
"OpenWeatherApiKey": "d489973b4c09ddc5fb56bd7b9270bbef"
//"Urls": "http://[::]:80"
}