Skip to content

Commit

Permalink
chore: use CatchingIntoServiceExceptionFor
Browse files Browse the repository at this point in the history
Refs: CPLP-2976
  • Loading branch information
Phil91 committed Jul 29, 2023
1 parent fa998ce commit d4549c9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
17 changes: 7 additions & 10 deletions src/externalsystems/Bpdm.Library/BpnAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@

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;

public class BpnAccess : IBpnAccess
{
private readonly HttpClient _httpClient;
private static readonly JsonSerializerOptions Options = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };

public BpnAccess(IHttpClientFactory httpFactory)
{
Expand All @@ -42,18 +45,12 @@ public async Task<BpdmLegalEntityDto> 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<BpdmLegalEntityDto>(
responseStream,
new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase },
cancellationToken: cancellationToken).ConfigureAwait(false);
var legalEntityResponse = await result.Content.ReadFromJsonAsync<BpdmLegalEntityDto>(Options, cancellationToken).ConfigureAwait(false);
if (legalEntityResponse?.Bpn == null)
{
throw new ServiceException("Access to external system bpdm did not return a valid legal entity response");
Expand Down
2 changes: 1 addition & 1 deletion tests/externalsystems/Bpdm.Library/BPNAccessTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public async Task FetchLegalEntityByBpn_UnsuccessfulCall_Throws()
var result = await Assert.ThrowsAsync<ServiceException>(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
Expand Down

0 comments on commit d4549c9

Please sign in to comment.