diff --git a/backend/YPS.Beer/Configuration/PunkServiceConfiguration.cs b/backend/YPS.Beer/Configuration/PunkServiceConfiguration.cs new file mode 100644 index 0000000..7676aa2 --- /dev/null +++ b/backend/YPS.Beer/Configuration/PunkServiceConfiguration.cs @@ -0,0 +1,8 @@ +namespace YPS.Beer.Configuration; + +public class PunkServiceConfiguration +{ + public const string ConfigurationSection = "PunkService"; + + public string BaseUrl { get; set; } = null!; +} \ No newline at end of file diff --git a/backend/YPS.Beer/Program.cs b/backend/YPS.Beer/Program.cs index c14d7fe..774f981 100644 --- a/backend/YPS.Beer/Program.cs +++ b/backend/YPS.Beer/Program.cs @@ -1,15 +1,21 @@ using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; +using YPS.Beer.Configuration; using YPS.Beer.Data; using YPS.Beer.Models; using YPS.Beer.Services; var builder = WebApplication.CreateBuilder(args); +var punkServiceConfiguration = new PunkServiceConfiguration(); +builder.Configuration.GetSection(PunkServiceConfiguration.ConfigurationSection).Bind(punkServiceConfiguration); + + builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddControllers(); -builder.Services.AddHttpClient(); +builder.Services.AddHttpClient() + .ConfigureHttpClient(client => client.BaseAddress = new Uri(punkServiceConfiguration.BaseUrl)); builder.Services.AddScoped(); builder.Services.AddDbContext(options => options.UseInMemoryDatabase("yps-beer")); @@ -41,4 +47,4 @@ app.MapControllers(); app.MapIdentityApi(); -app.Run(); +app.Run(); \ No newline at end of file diff --git a/backend/YPS.Beer/Services/PunkService.cs b/backend/YPS.Beer/Services/PunkService.cs index 0fe4d3e..73494b2 100644 --- a/backend/YPS.Beer/Services/PunkService.cs +++ b/backend/YPS.Beer/Services/PunkService.cs @@ -4,8 +4,6 @@ public class PunkService : IPunkService { private readonly HttpClient _httpClient; - private const string BaseUrl = "https://api.punkapi.com/v2/"; - public PunkService(HttpClient httpClient) { _httpClient = httpClient; @@ -13,7 +11,7 @@ public class PunkService : IPunkService public async Task GetBeer(int id) { - var beer = await _httpClient.GetFromJsonAsync($"{BaseUrl}beers/{id}"); + var beer = await _httpClient.GetFromJsonAsync($"beers/{id}"); return beer?.SingleOrDefault(); } @@ -37,7 +35,7 @@ public class PunkService : IPunkService { search = search.Replace(' ', '_'); - var beers = await _httpClient.GetFromJsonAsync($"{BaseUrl}beers?beer_name={search}"); + var beers = await _httpClient.GetFromJsonAsync($"beers?beer_name={search}"); if (beers is null) throw new Exception(); diff --git a/backend/YPS.Beer/appsettings.Development.json b/backend/YPS.Beer/appsettings.Development.json index 0c208ae..3a5b05d 100644 --- a/backend/YPS.Beer/appsettings.Development.json +++ b/backend/YPS.Beer/appsettings.Development.json @@ -4,5 +4,8 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } + }, + "PunkService": { + "BaseUrl": "https://api.punkapi.com/v2/" } } diff --git a/backend/YPS.Beer/appsettings.json b/backend/YPS.Beer/appsettings.json index 10f68b8..9de6e42 100644 --- a/backend/YPS.Beer/appsettings.json +++ b/backend/YPS.Beer/appsettings.json @@ -5,5 +5,8 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "PunkService": { + "BaseUrl": "https://api.punkapi.com/v2/" + } }