Refactor and expose more information
This commit was merged in pull request #1.
This commit is contained in:
17
HomeApi/Integration/AuroraService.cs
Normal file
17
HomeApi/Integration/AuroraService.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using HomeApi.Integration.Client;
|
||||
using HomeApi.Models.Response;
|
||||
|
||||
namespace HomeApi.Integration;
|
||||
|
||||
public interface IAuroraService
|
||||
{
|
||||
Task<AuroraForecastApiResponse> GetAuroraForecastAsync(string lat, string lon);
|
||||
}
|
||||
|
||||
public class AuroraService(IAuroraClient auroraApi) : IAuroraService
|
||||
{
|
||||
public Task<AuroraForecastApiResponse> GetAuroraForecastAsync(string lat, string lon)
|
||||
{
|
||||
return auroraApi.GetForecastAsync(latitude: lat, longitude: lon);
|
||||
}
|
||||
}
|
||||
15
HomeApi/Integration/Client/AuroraClient.cs
Normal file
15
HomeApi/Integration/Client/AuroraClient.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using HomeApi.Models.Response;
|
||||
using Refit;
|
||||
|
||||
namespace HomeApi.Integration.Client;
|
||||
|
||||
public interface IAuroraClient
|
||||
{
|
||||
[Get("/v1/")]
|
||||
Task<AuroraForecastApiResponse> GetForecastAsync(
|
||||
[AliasAs("type")] string type = "all",
|
||||
[AliasAs("lat")] string latitude = "0",
|
||||
[AliasAs("long")] string longitude = "0",
|
||||
[AliasAs("forecast")] string forecast = "false",
|
||||
[AliasAs("threeday")] string threeDay = "false");
|
||||
}
|
||||
13
HomeApi/Integration/Client/NominatimClient.cs
Normal file
13
HomeApi/Integration/Client/NominatimClient.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using HomeApi.Models.Response;
|
||||
using Refit;
|
||||
|
||||
namespace HomeApi.Integration.Client;
|
||||
|
||||
public interface INominatimClient
|
||||
{
|
||||
[Get("/search")]
|
||||
Task<List<NomatimApiResponse>> SearchAsync(
|
||||
[AliasAs("q")] string query,
|
||||
[AliasAs("format")] string format = "json",
|
||||
[AliasAs("limit")] int limit = 1);
|
||||
}
|
||||
17
HomeApi/Integration/Client/WeatherClient.cs
Normal file
17
HomeApi/Integration/Client/WeatherClient.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using HomeApi.Models.Response;
|
||||
|
||||
namespace HomeApi.Integration.Client;
|
||||
|
||||
using Refit;
|
||||
|
||||
public interface IWeatherClient
|
||||
{
|
||||
[Get("/forecast.json")]
|
||||
Task<WeatherData> GetForecastAsync(
|
||||
[AliasAs("key")] string apiKey,
|
||||
[AliasAs("q")] string coordinates,
|
||||
[AliasAs("days")] int days = 7,
|
||||
[AliasAs("lang")] string language = "sv",
|
||||
[AliasAs("aqi")] string aqi = "yes",
|
||||
[AliasAs("alerts")] string alerts = "yes");
|
||||
}
|
||||
19
HomeApi/Integration/GeocodingService.cs
Normal file
19
HomeApi/Integration/GeocodingService.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using HomeApi.Integration.Client;
|
||||
using HomeApi.Models.Response;
|
||||
|
||||
namespace HomeApi.Integration;
|
||||
|
||||
public interface IGeocodingService
|
||||
{
|
||||
Task<NomatimApiResponse?> GetCoordinatesAsync(string address);
|
||||
}
|
||||
|
||||
public class GeocodingService(INominatimClient nominatimApi) : IGeocodingService
|
||||
{
|
||||
public async Task<NomatimApiResponse?> GetCoordinatesAsync(string address)
|
||||
{
|
||||
var results = await nominatimApi.SearchAsync(address);
|
||||
|
||||
return results.FirstOrDefault();
|
||||
}
|
||||
}
|
||||
22
HomeApi/Integration/WeatherService.cs
Normal file
22
HomeApi/Integration/WeatherService.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using HomeApi.Integration.Client;
|
||||
using HomeApi.Models.Configuration;
|
||||
using HomeApi.Models.Response;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace HomeApi.Integration;
|
||||
|
||||
public interface IWeatherService
|
||||
{
|
||||
Task<WeatherData> GetWeatherAsync(string lat, string lon);
|
||||
}
|
||||
|
||||
public class WeatherService(IWeatherClient weatherApi, IOptions<ApiConfiguration> options) : IWeatherService
|
||||
{
|
||||
private readonly ApiConfiguration _apiConfig = options.Value;
|
||||
|
||||
public Task<WeatherData> GetWeatherAsync(string lat, string lon)
|
||||
{
|
||||
var location = $"{lat},{lon}";
|
||||
return weatherApi.GetForecastAsync(_apiConfig.Keys.Weather, location);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user