Skip to content

Commit

Permalink
fix: switch to new API request for loading country-meta data
Browse files Browse the repository at this point in the history
  • Loading branch information
alsami committed Sep 25, 2021
1 parent 9cddc1e commit a5d6f05
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 14 deletions.
2 changes: 0 additions & 2 deletions src/Covid19Api.Constants/Urls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ public static class Urls
// ReSharper disable once S1075
#pragma warning disable S1075
public const string Covid19WorldometerUrl = "https://worldometers.info/coronavirus";

public const string RestCountriesApiUrl = "https://restcountries.eu/rest/v2/all";
#pragma warning restore
}
}
13 changes: 13 additions & 0 deletions src/Covid19Api.Services/Configuration/CountryLayerConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
namespace Covid19Api.Services.Configuration
{
public class CountryLayerConfiguration
{
public string ApiUrl { get; set; } = null!;

public string ApiKey { get; set; } = null!;

public string GetRequestUrl() => $"{this.ApiUrl}?access_key={this.ApiKey}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class CountryMetaDataLoaderDecorator : ICountryMetaDataLoader, IDisposabl
{
private const string CacheKey = "CountryMetaData";

private readonly SemaphoreSlim mutex = new SemaphoreSlim(1);
private readonly SemaphoreSlim mutex = new(1);

private readonly IDistributedCache distributedCache;
private readonly ICountryMetaDataLoader countryMetaDataLoader;
Expand Down Expand Up @@ -72,7 +72,7 @@ private async ValueTask CacheAsync(CountryMetaData[] fetchedCountryMetaData)

await this.distributedCache.SetAsync(CacheKey, compressed, new DistributedCacheEntryOptions
{
AbsoluteExpiration = DateTime.UtcNow.AddDays(10)
AbsoluteExpiration = DateTime.UtcNow.AddDays(1)
});
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/Covid19Api.Services/Loader/CountryMetaDataLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Covid19Api.Constants;
using Covid19Api.Services.Abstractions.Loader;
using Covid19Api.Services.Abstractions.Models;
using Covid19Api.Services.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Covid19Api.Services.Loader
{
Expand All @@ -18,18 +19,20 @@ public class CountryMetaDataLoader : ICountryMetaDataLoader

private readonly ILogger<CountryMetaDataLoader> logger;
private readonly IHttpClientFactory httpClientFactory;
private readonly IOptions<CountryLayerConfiguration> countryLayerConfiguration;

public CountryMetaDataLoader(ILogger<CountryMetaDataLoader> logger, IHttpClientFactory httpClientFactory)
public CountryMetaDataLoader(ILogger<CountryMetaDataLoader> logger, IHttpClientFactory httpClientFactory, IOptions<CountryLayerConfiguration> countryLayerConfiguration)
{
this.logger = logger;
this.httpClientFactory = httpClientFactory;
this.countryLayerConfiguration = countryLayerConfiguration;
}

public async Task<CountryMetaData[]> LoadCountryMetaDataAsync()
{
var client = this.httpClientFactory.CreateClient();

var response = await client.GetAsync(Urls.RestCountriesApiUrl);
var url = this.countryLayerConfiguration.Value.GetRequestUrl();
var response = await client.GetAsync(url);

if (response.IsSuccessStatusCode)
{
Expand All @@ -40,8 +43,7 @@ public async Task<CountryMetaData[]> LoadCountryMetaDataAsync()
}

var error = await response.Content.ReadAsStringAsync();
this.logger.LogError("Failed load country meta-data from {url}! Status-Code: {statusCode} Error:\n{error}",
Urls.RestCountriesApiUrl, response.StatusCode, error);
this.logger.LogError("Failed load country meta-data from {Url}! Status-Code: {StatusCode} Error:\n{Error}", url, response.StatusCode, error);

return Array.Empty<CountryMetaData>();
}
Expand Down
6 changes: 3 additions & 3 deletions src/Covid19Api/Covid19Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<PackageReference Include="Serilog.Settings.Configuration" Version="3.2.0" />
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.2.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.2.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUi" Version="6.2.1" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.2.2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.2.2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUi" Version="6.2.2" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions src/Covid19Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Covid19Api.ExceptionFilter;
using Covid19Api.IoC.Extensions;
using Covid19Api.Middleware;
using Covid19Api.Services.Configuration;
using Covid19Api.UseCases.Behaviors;
using Covid19Api.UseCases.Queries.GlobalStatistics;
using MediatR.Extensions.Autofac.DependencyInjection;
Expand Down Expand Up @@ -61,6 +62,9 @@ public void ConfigureServices(IServiceCollection services)
Version = ApiVersion
}));

services.AddOptions<CountryLayerConfiguration>()
.BindConfiguration(nameof(CountryLayerConfiguration));

services.AddHttpClient();

services.AddCors(options => options.AddPolicy(CorsPolicyName, builder => builder
Expand Down
6 changes: 5 additions & 1 deletion src/Covid19Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,9 @@
}
},
"EnableResponseCompression": false,
"DisableWorker": false
"DisableWorker": false,
"CountryLayerConfiguration": {
"ApiUrl": "http://api.countrylayer.com/v2/all",
"ApiKey": ""
}
}

0 comments on commit a5d6f05

Please sign in to comment.