Add configurable URL

This commit is contained in:
Stedoss 2023-12-07 00:29:11 +00:00
parent 284a36412d
commit bb5c5b2c9e
5 changed files with 25 additions and 7 deletions

View File

@ -0,0 +1,8 @@
namespace YPS.Beer.Configuration;
public class PunkServiceConfiguration
{
public const string ConfigurationSection = "PunkService";
public string BaseUrl { get; set; } = null!;
}

View File

@ -1,15 +1,21 @@
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using YPS.Beer.Configuration;
using YPS.Beer.Data; using YPS.Beer.Data;
using YPS.Beer.Models; using YPS.Beer.Models;
using YPS.Beer.Services; using YPS.Beer.Services;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
var punkServiceConfiguration = new PunkServiceConfiguration();
builder.Configuration.GetSection(PunkServiceConfiguration.ConfigurationSection).Bind(punkServiceConfiguration);
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.AddControllers(); builder.Services.AddControllers();
builder.Services.AddHttpClient<IPunkService, PunkService>(); builder.Services.AddHttpClient<IPunkService, PunkService>()
.ConfigureHttpClient(client => client.BaseAddress = new Uri(punkServiceConfiguration.BaseUrl));
builder.Services.AddScoped<IBeerService, BeerService>(); builder.Services.AddScoped<IBeerService, BeerService>();
builder.Services.AddDbContext<BeerContext>(options => options.UseInMemoryDatabase("yps-beer")); builder.Services.AddDbContext<BeerContext>(options => options.UseInMemoryDatabase("yps-beer"));

View File

@ -4,8 +4,6 @@ public class PunkService : IPunkService
{ {
private readonly HttpClient _httpClient; private readonly HttpClient _httpClient;
private const string BaseUrl = "https://api.punkapi.com/v2/";
public PunkService(HttpClient httpClient) public PunkService(HttpClient httpClient)
{ {
_httpClient = httpClient; _httpClient = httpClient;
@ -13,7 +11,7 @@ public class PunkService : IPunkService
public async Task<Models.Beer?> GetBeer(int id) public async Task<Models.Beer?> GetBeer(int id)
{ {
var beer = await _httpClient.GetFromJsonAsync<Models.Beer[]>($"{BaseUrl}beers/{id}"); var beer = await _httpClient.GetFromJsonAsync<Models.Beer[]>($"beers/{id}");
return beer?.SingleOrDefault(); return beer?.SingleOrDefault();
} }
@ -37,7 +35,7 @@ public class PunkService : IPunkService
{ {
search = search.Replace(' ', '_'); search = search.Replace(' ', '_');
var beers = await _httpClient.GetFromJsonAsync<Models.Beer[]>($"{BaseUrl}beers?beer_name={search}"); var beers = await _httpClient.GetFromJsonAsync<Models.Beer[]>($"beers?beer_name={search}");
if (beers is null) if (beers is null)
throw new Exception(); throw new Exception();

View File

@ -4,5 +4,8 @@
"Default": "Information", "Default": "Information",
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
} }
},
"PunkService": {
"BaseUrl": "https://api.punkapi.com/v2/"
} }
} }

View File

@ -5,5 +5,8 @@
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
} }
}, },
"AllowedHosts": "*" "AllowedHosts": "*",
"PunkService": {
"BaseUrl": "https://api.punkapi.com/v2/"
}
} }