From d4549c92d9b209db0ae6a059425dd5e25a651587 Mon Sep 17 00:00:00 2001 From: Phil Schneider Date: Sat, 29 Jul 2023 17:09:37 +0200 Subject: [PATCH] chore: use CatchingIntoServiceExceptionFor Refs: CPLP-2976 --- src/externalsystems/Bpdm.Library/BpnAccess.cs | 17 +++++++---------- .../Bpdm.Library/BPNAccessTest.cs | 2 +- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/externalsystems/Bpdm.Library/BpnAccess.cs b/src/externalsystems/Bpdm.Library/BpnAccess.cs index 42e4985a23..d894c46c9a 100644 --- a/src/externalsystems/Bpdm.Library/BpnAccess.cs +++ b/src/externalsystems/Bpdm.Library/BpnAccess.cs @@ -20,7 +20,9 @@ using Org.Eclipse.TractusX.Portal.Backend.Bpdm.Library.Models; using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling; +using Org.Eclipse.TractusX.Portal.Backend.Framework.HttpClientExtensions; using System.Net.Http.Headers; +using System.Net.Http.Json; using System.Text.Json; namespace Org.Eclipse.TractusX.Portal.Backend.Bpdm.Library; @@ -28,6 +30,7 @@ namespace Org.Eclipse.TractusX.Portal.Backend.Bpdm.Library; public class BpnAccess : IBpnAccess { private readonly HttpClient _httpClient; + private static readonly JsonSerializerOptions Options = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; public BpnAccess(IHttpClientFactory httpFactory) { @@ -42,18 +45,12 @@ public async Task FetchLegalEntityByBpn(string businessPartn Path = $"pool/api/catena/legal-entities/{Uri.EscapeDataString(businessPartnerNumber)}", Query = "idType=BPN" }.Uri; - var result = await _httpClient.GetAsync(uri.PathAndQuery, cancellationToken).ConfigureAwait(false); - if (!result.IsSuccessStatusCode) - { - throw new ServiceException($"Access to external system bpdm failed with Status Code {result.StatusCode}", result.StatusCode); - } - await using var responseStream = await result.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false); + var result = await _httpClient.GetAsync(uri.PathAndQuery, cancellationToken) + .CatchingIntoServiceExceptionFor("bpn-fetch-legal-entity") + .ConfigureAwait(false); try { - var legalEntityResponse = await JsonSerializer.DeserializeAsync( - responseStream, - new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }, - cancellationToken: cancellationToken).ConfigureAwait(false); + var legalEntityResponse = await result.Content.ReadFromJsonAsync(Options, cancellationToken).ConfigureAwait(false); if (legalEntityResponse?.Bpn == null) { throw new ServiceException("Access to external system bpdm did not return a valid legal entity response"); diff --git a/tests/externalsystems/Bpdm.Library/BPNAccessTest.cs b/tests/externalsystems/Bpdm.Library/BPNAccessTest.cs index 2f3102a3ff..071fc1387c 100644 --- a/tests/externalsystems/Bpdm.Library/BPNAccessTest.cs +++ b/tests/externalsystems/Bpdm.Library/BPNAccessTest.cs @@ -329,7 +329,7 @@ public async Task FetchLegalEntityByBpn_UnsuccessfulCall_Throws() var result = await Assert.ThrowsAsync(Act).ConfigureAwait(false); //Assert - result.Message.Should().Be($"Access to external system bpdm failed with Status Code {HttpStatusCode.BadRequest}"); + result.Message.Should().Be($"call to external system bpn-fetch-legal-entity failed with statuscode {(int)HttpStatusCode.BadRequest}"); } #endregion