From 8cd69c7d8b17a7e5895a4ead2c5e575f23d149a8 Mon Sep 17 00:00:00 2001 From: Thomas Fransolet Date: Sat, 24 Feb 2024 22:42:41 +0100 Subject: [PATCH] Add weather result --- Manager.Interfaces/Models/CityData.cs | 17 ++++++ .../Controllers/ConfigurationController.cs | 54 ++++++++++++++++++- .../Controllers/ResourceController.cs | 3 +- ManagerService/appsettings.json | 3 +- 4 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 Manager.Interfaces/Models/CityData.cs diff --git a/Manager.Interfaces/Models/CityData.cs b/Manager.Interfaces/Models/CityData.cs new file mode 100644 index 0000000..ba22016 --- /dev/null +++ b/Manager.Interfaces/Models/CityData.cs @@ -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; } + } +} diff --git a/ManagerService/Controllers/ConfigurationController.cs b/ManagerService/Controllers/ConfigurationController.cs index 706adf7..c06f81c 100644 --- a/ManagerService/Controllers/ConfigurationController.cs +++ b/ManagerService/Controllers/ConfigurationController.cs @@ -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 GetDetailAsync(string id) { try { @@ -127,6 +129,56 @@ namespace ManagerService.Controllers List 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(); + + 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 cities = JsonConvert.DeserializeObject>(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) diff --git a/ManagerService/Controllers/ResourceController.cs b/ManagerService/Controllers/ResourceController.cs index 9131a13..f2794a9 100644 --- a/ManagerService/Controllers/ResourceController.cs +++ b/ManagerService/Controllers/ResourceController.cs @@ -252,7 +252,8 @@ namespace ManagerService.Controllers } stringResult = Convert.ToBase64String(fileBytes); } - } else + } + else { throw new FileLoadException(message: "Fichier inexistant ou trop volumineux (max 4Mb)"); } diff --git a/ManagerService/appsettings.json b/ManagerService/appsettings.json index 8fb5161..b5dbff2 100644 --- a/ManagerService/appsettings.json +++ b/ManagerService/appsettings.json @@ -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" }