diff --git a/ODPC.Server/Apis/Odrc/OdrcClientFactory.cs b/ODPC.Server/Apis/Odrc/OdrcClientFactory.cs new file mode 100644 index 0000000..2131369 --- /dev/null +++ b/ODPC.Server/Apis/Odrc/OdrcClientFactory.cs @@ -0,0 +1,30 @@ +using System.Net.Http.Headers; +using Microsoft.Extensions.Options; +using ODPC.Authentication; + +namespace ODPC.Apis.Odrc +{ + public interface IOdrcClientFactory + { + HttpClient Create(OdpUser user, string handeling); + } + + public class OdrcClientFactory(IHttpClientFactory httpClientFactory, IOptions options) : IOdrcClientFactory + { + private readonly IHttpClientFactory _httpClientFactory = httpClientFactory; + private readonly IOptions _options = options; + + public HttpClient Create(OdpUser user, string handeling) + { + var config = _options.Value; + var client = _httpClientFactory.CreateClient(); + client.BaseAddress = new(config.BaseUrl); + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", config.ApiKey); //dit doet nog niets. nog niet geimplementeerd aan odrc kant. nb nog niet duidelijk of dit de juiste manier zal zijn voor het meesturen van het token + client.DefaultRequestHeaders.Add("Audit-User-Id", user.Id); //dit doet nog niets. nog niet geimplementeerd aan odrc kant + client.DefaultRequestHeaders.Add("Audit-User-Display-Name", user.FullName); //dit doet nog niets. nog niet geimplementeerd aan odrc kant + client.DefaultRequestHeaders.Add("Audit-Toelichting", handeling); //dit doet nog niets. nog niet geimplementeerd aan odrc kant + return client; + } + + } +} diff --git a/ODPC.Server/Apis/Odrc/OdrcConfig.cs b/ODPC.Server/Apis/Odrc/OdrcConfig.cs new file mode 100644 index 0000000..1e82d8f --- /dev/null +++ b/ODPC.Server/Apis/Odrc/OdrcConfig.cs @@ -0,0 +1,11 @@ +using System.Globalization; + +namespace ODPC.Apis.Odrc +{ + public class OdrcConfig + { + public required string BaseUrl { get; set; } + public required string ApiKey { get; set; } + + } +} diff --git a/ODPC.Server/Config/JsonSerialization.cs b/ODPC.Server/Config/JsonSerialization.cs new file mode 100644 index 0000000..47205a0 --- /dev/null +++ b/ODPC.Server/Config/JsonSerialization.cs @@ -0,0 +1,12 @@ +using System.Text.Json; + +namespace ODPC.Config +{ + public static class JsonSerialization + { + public static readonly JsonSerializerOptions Options = new() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }; + } +} diff --git a/ODPC.Server/Features/Publicaties/PublicatieRegistreren/PublicatieRegistrerenController.cs b/ODPC.Server/Features/Publicaties/PublicatieRegistreren/PublicatieRegistrerenController.cs index 0b1a5c4..5f81a98 100644 --- a/ODPC.Server/Features/Publicaties/PublicatieRegistreren/PublicatieRegistrerenController.cs +++ b/ODPC.Server/Features/Publicaties/PublicatieRegistreren/PublicatieRegistrerenController.cs @@ -1,17 +1,37 @@ -using Microsoft.AspNetCore.Mvc; +using System.Text.Json; +using Microsoft.AspNetCore.Mvc; +using ODPC.Apis.Odrc; +using ODPC.Authentication; +using ODPC.Config; namespace ODPC.Features.Publicaties.PublicatieRegistreren { [ApiController] - public class PublicatieRegistrerenController : ControllerBase + public class PublicatieRegistrerenController(OdpUser user, IOdrcClientFactory clientFactory) : ControllerBase { + private readonly IOdrcClientFactory _clientFactory = clientFactory; + [HttpPost("api/v1/publicaties")] - public IActionResult Post(Publicatie publicatie) + public async Task Post(Publicatie publicatie) { publicatie.Uuid = Guid.NewGuid(); publicatie.Registratiedatum = DateTime.Now; + + //de mock laten we er nog even in totdat ook het ophalen van gegevens via het register verloopt PublicatiesMock.Publicaties[publicatie.Uuid] = publicatie; - return Ok(publicatie); + + var client = _clientFactory.Create(user, "Publicatie geregistreerd"); + + var response = await client.PostAsJsonAsync("/api/v1/publicaties/", publicatie, new CancellationToken()); + + response.EnsureSuccessStatusCode(); + + var responseBody = await response.Content.ReadAsStringAsync(); + + var viewModel = JsonSerializer.Deserialize(responseBody, JsonSerialization.Options); + + return Ok(viewModel); } + } } diff --git a/ODPC.Server/Program.cs b/ODPC.Server/Program.cs index 0921427..79deb6d 100644 --- a/ODPC.Server/Program.cs +++ b/ODPC.Server/Program.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore; +using ODPC.Apis.Odrc; using ODPC.Authentication; using ODPC.Data; using Serilog; @@ -39,6 +40,10 @@ var connStr = $"Username={builder.Configuration["POSTGRES_USER"]};Password={builder.Configuration["POSTGRES_PASSWORD"]};Host={builder.Configuration["POSTGRES_HOST"]};Database={builder.Configuration["POSTGRES_DB"]};Port={builder.Configuration["POSTGRES_PORT"]}"; builder.Services.AddDbContext(opt => opt.UseNpgsql(connStr)); + builder.Services.Configure(builder.Configuration.GetSection("Odrc")); + builder.Services.AddScoped(); + + var app = builder.Build(); diff --git a/ODPC.Server/appsettings.json b/ODPC.Server/appsettings.json index a7a1015..83558f8 100644 --- a/ODPC.Server/appsettings.json +++ b/ODPC.Server/appsettings.json @@ -16,5 +16,10 @@ "OIDC_NAME_CLAIM_TYPE": "", "OIDC_ROLE_CLAIM_TYPE": "", "OIDC_ID_CLAIM_TYPE": "", - "AllowedHosts": "*" + "AllowedHosts": "*", + + "Odrc": { + "BaseUrl": "https://woo-publicaties.test.opengem.nl/", + "ApiKey": "..." + } } \ No newline at end of file