Skip to content

Commit

Permalink
feat(bpdm): adjust bpdm service calls to new service
Browse files Browse the repository at this point in the history
structure of bpdm service has changed, therefor the models have been aligned

Refs: CPLP-2976
  • Loading branch information
Phil91 committed Jul 25, 2023
1 parent 2da1244 commit d9c725d
Show file tree
Hide file tree
Showing 21 changed files with 568 additions and 968 deletions.
1 change: 1 addition & 0 deletions src/externalsystems/Bpdm.Library/Bpdm.Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\framework\Framework.Async\Framework.Async.csproj" />
<ProjectReference Include="..\..\framework\Framework.HttpClient\Framework.HttpClient.csproj" />
<ProjectReference Include="..\..\framework\Framework.Logging\Framework.Logging.csproj" />
<ProjectReference Include="..\..\framework\Framework.Token\Framework.Token.csproj" />
Expand Down
126 changes: 49 additions & 77 deletions src/externalsystems/Bpdm.Library/BpdmService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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 Org.Eclipse.TractusX.Portal.Backend.Framework.Linq;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Token;
using System.Net.Http.Json;
using System.Text.Json;
Expand All @@ -37,7 +36,10 @@ public class BpdmService : IBpdmService
private static readonly JsonSerializerOptions Options = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters = { new JsonStringEnumConverter(allowIntegerValues: false) }
Converters =
{
new JsonStringEnumConverter(allowIntegerValues: false),
}
};

public BpdmService(ITokenService tokenService, IOptions<BpdmServiceSettings> options)
Expand All @@ -54,75 +56,54 @@ public async Task<bool> PutInputLegalEntity(BpdmTransferData data, CancellationT
var requestData = new BpdmLegalEntityData[]
{
new(
data.ExternalId, // ExternalId
null, // Bpn
data.ExternalId, // Index
Enumerable.Repeat(data.CompanyName, 1), // LegalName
data.ShortName, // LegalShortName
null, // LegalForm
data.Identifiers.Select(x =>
new BpdmIdentifier(
x.Value, // Value
x.BpdmIdentifierId, // Type
null, // IssuingBody
null)), // Status
new BpdmName[] // Names
{
new (
data.CompanyName, // Value
null, // ShortName
"REGISTERED", // Type
"de") // Language
},
null, // LegalForm
null, // Status
Enumerable.Empty<BpdmProfileClassification>(),
Enumerable.Empty<string>(), // Types
Enumerable.Empty<BpdmBankAccount>(),
null)), // IssuingBody
Enumerable.Empty<BpdmStatus>(), // Status
Enumerable.Empty<BpdmProfileClassification>(), // Classifications
Enumerable.Empty<string>(), // Roles
new BpdmLegalAddress(
new BpdmAddressVersion(
"WESTERN_LATIN_STANDARD", //CharacterSet
"de"), // Version
null, // CareOf
Enumerable.Empty<string>(),// Contexts
data.AlphaCode2, // Country
data.Region == null
? Enumerable.Empty<BpdmAdministrativeArea>()
: new BpdmAdministrativeArea[] {
new (
data.Region, // Value
null, // ShortName
null, // Fipscode
"COUNTY" // Type
)
},
data.ZipCode == null
? Enumerable.Empty<BpdmPostcode>()
: new BpdmPostcode[] {
new (
data.ZipCode, // Value
"REGULAR") // Type
},
new BpdmLocality[] {
new (
data.City, // Value
null, // ShortName
"CITY") // Type
},
new BpdmThoroughfare[] {
new (
data.StreetName, // Value
null, // Name
null, // ShortName
data.StreetNumber, // Number
null, // Direction
"STREET") // Type
},
Enumerable.Empty<BpdmPremise>(),
Enumerable.Empty<BpdmPostalDeliveryPoint>(),
null, // GeographicCoordinates
Enumerable.Empty<string>() // Types
)
Enumerable.Empty<string>(), // Name
Enumerable.Empty<BpdmAddressState>(), // States
Enumerable.Empty<BpdmAddressIdentifier>(), // Identifiers
new BpdmAddressPhysicalPostalAddress( // PhysicalPostalAddress
null, // GeographicCoordinates
data.AlphaCode2, // Country
data.ZipCode, // PostalCode
data.City, // City
new BpdmStreet(
null, // NamePrefix
null, // AdditionalNamePrefix
data.StreetName, // Name
null, // NameSuffix
null, // AdditionalNameSuffix
data.StreetNumber, // StreetNumber
null, // Milestone
null // Direction
),
data.Region, // AdministrativeAreaLevel1
null, // AdministrativeAreaLevel2
null, // AdministrativeAreaLevel3
null, // District
null, // CompanyPostalCode
null, // IndustrialZone
null, // Building
null, // Floor
null // Door
),
null, // AlternativePostalAddress
Enumerable.Empty<string>()
)
)
};

await httpClient.PutAsJsonAsync("/api/catena/input/legal-entities", requestData, Options, cancellationToken)
await httpClient.PutAsJsonAsync("/companies/test-company/api/catena/input/legal-entities", requestData, Options, cancellationToken)
.CatchingIntoServiceExceptionFor("bpdm-put-legal-entities", HttpAsyncResponseMessageExtension.RecoverOptions.INFRASTRUCTURE).ConfigureAwait(false);
return true;
}
Expand All @@ -132,28 +113,19 @@ public async Task<BpdmLegalEntityOutputData> FetchInputLegalEntity(string extern
var httpClient = await _tokenService.GetAuthorizedClient<BpdmService>(_settings, cancellationToken).ConfigureAwait(false);

var data = Enumerable.Repeat(externalId, 1);
var result = await httpClient.PostAsJsonAsync("/api/catena/output/legal-entities/search", data, Options, cancellationToken)
var result = await httpClient.PostAsJsonAsync($"/companies/test-company/api/catena/output/legal-entities/search{externalId}", data, Options, cancellationToken)
.CatchingIntoServiceExceptionFor("bpdm-search-legal-entities", HttpAsyncResponseMessageExtension.RecoverOptions.INFRASTRUCTURE).ConfigureAwait(false);
try
{
var paginationResponse = await result.Content
.ReadFromJsonAsync<PageOutputResponseBpdmLegalEntityData>(Options, cancellationToken)
var response = await result.Content
.ReadFromJsonAsync<BpdmLegalEntityOutputData>(Options, cancellationToken)
.ConfigureAwait(false);
if (paginationResponse?.Content == null || paginationResponse.Errors == null)
if (response?.ExternalId == null)
{
throw new ServiceException("Access to external system bpdm did not return a valid legal entity response", true);
}

paginationResponse.Errors.IfAny(errors =>
throw new ServiceException($"The external system bpdm responded with errors {string.Join(";", errors.Select(x => $"ErrorCode: {x.ErrorCode}, ErrorMessage: {x.Message}"))}"));
try
{
return paginationResponse.Content.Single(x => x.ExternalId == externalId);
}
catch (InvalidOperationException)
{
throw new ServiceException("Access to external system bpdm did not return a valid legal entity response", true);
}
return response;
}
catch (JsonException je)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

using Org.Eclipse.TractusX.Portal.Backend.Bpdm.Library.Models;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Async;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
using Org.Eclipse.TractusX.Portal.Backend.Registration.Service.Bpn.Model;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Runtime.CompilerServices;
using System.Text.Json;

namespace Org.Eclipse.TractusX.Portal.Backend.Registration.Service.Bpn;
namespace Org.Eclipse.TractusX.Portal.Backend.Bpdm.Library;

public class BpnAccess : IBpnAccess
{
Expand Down Expand Up @@ -58,9 +59,9 @@ public async IAsyncEnumerable<FetchBusinessPartnerDto> FetchBusinessPartner(stri
public async Task<BpdmLegalEntityDto> FetchLegalEntityByBpn(string businessPartnerNumber, string token, CancellationToken cancellationToken)
{
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var uri = new UriBuilder()
var uri = new UriBuilder
{
Path = $"api/catena/legal-entities/{Uri.EscapeDataString(businessPartnerNumber)}",
Path = $"pool/api/catena/legal-entities/{Uri.EscapeDataString(businessPartnerNumber)}",
Query = "idType=BPN"
}.Uri;
var result = await _httpClient.GetAsync(uri.PathAndQuery, cancellationToken).ConfigureAwait(false);
Expand All @@ -86,43 +87,4 @@ public async Task<BpdmLegalEntityDto> FetchLegalEntityByBpn(string businessPartn
throw new ServiceException($"Access to external system bpdm did not return a valid json response: {je.Message}");
}
}

public async IAsyncEnumerable<BpdmLegalEntityAddressDto> FetchLegalEntityAddressByBpn(string businessPartnerNumber, string token, [EnumeratorCancellation] CancellationToken cancellationToken)
{
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var uri = new UriBuilder()
{
Path = "api/catena/legal-entities/legal-addresses/search"
}.Uri;
var json = new[] { businessPartnerNumber };
var result = await _httpClient.PostAsJsonAsync(uri.PathAndQuery, json, cancellationToken).ConfigureAwait(false);
if (result.IsSuccessStatusCode)
{
await using var responseStream = await result.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);

await foreach (var address in JsonSerializer
.DeserializeAsyncEnumerable<BpdmLegalEntityAddressDto>(
responseStream,
new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase },
cancellationToken: cancellationToken)
.CatchingAsync(
ex =>
{
throw new ServiceException($"Access to external system bpdm did not return a valid json response: {ex.Message}");
},
cancellationToken)
.ConfigureAwait(false))
{
if (address?.LegalAddress == null || address.LegalEntity == null)
{
throw new ServiceException("Access to external system bpdm did not return a valid legal entity address response");
}
yield return address;
}
}
else
{
throw new ServiceException($"Access to external system bpdm failed with Status Code {result.StatusCode}", result.StatusCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
using Org.Eclipse.TractusX.Portal.Backend.Framework.HttpClientExtensions;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Logging;

namespace Org.Eclipse.TractusX.Portal.Backend.Bpdm.Library;
namespace Org.Eclipse.TractusX.Portal.Backend.Bpdm.Library.DependencyInjection;

public static class BpdmServiceCollectionExtension
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

using Microsoft.Extensions.DependencyInjection;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Logging;

namespace Org.Eclipse.TractusX.Portal.Backend.Registration.Service.Bpn;
namespace Org.Eclipse.TractusX.Portal.Backend.Bpdm.Library.DependencyInjection;

public static class BpnAccessCollectionExtension
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

using Org.Eclipse.TractusX.Portal.Backend.Registration.Service.Bpn.Model;
using Org.Eclipse.TractusX.Portal.Backend.Bpdm.Library.Models;

namespace Org.Eclipse.TractusX.Portal.Backend.Registration.Service.Bpn;
namespace Org.Eclipse.TractusX.Portal.Backend.Bpdm.Library;

public interface IBpnAccess
{
IAsyncEnumerable<FetchBusinessPartnerDto> FetchBusinessPartner(string bpn, string token, CancellationToken cancellationToken);
Task<BpdmLegalEntityDto> FetchLegalEntityByBpn(string businessPartnerNumber, string token, CancellationToken cancellationToken);
IAsyncEnumerable<BpdmLegalEntityAddressDto> FetchLegalEntityAddressByBpn(string businessPartnerNumber, string token, CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

namespace Org.Eclipse.TractusX.Portal.Backend.Registration.Service.Bpn.Model;
namespace Org.Eclipse.TractusX.Portal.Backend.Bpdm.Library.Models;

public record BpdmLegalEntityAddressDto(
string LegalEntity,
Expand All @@ -37,7 +37,7 @@ public record BpdmLegalAddressDto(
IEnumerable<BpdmPremiseDto> Premises,
IEnumerable<BpdmPostalDeliveryPointDto> PostalDeliveryPoints,
BpdmGeographicCoordinatesDto GeographicCoordinates,
IEnumerable<BpdmUrlDataDto> Types
IEnumerable<BpdmTechnicalKey> Types
);

public record BpdmAddressVersionDto(
Expand All @@ -49,19 +49,19 @@ public record BpdmAdministrativeAreaDto(
string Value,
string ShortName,
string FipsCode,
BpdmUrlDataDto Type,
BpdmTechnicalKey Type,
BpdmDataDto Language
);

public record BpdmPostCodeDto(
string Value,
BpdmUrlDataDto Type
BpdmTechnicalKey Type
);

public record BpdmLocalityDto(
string Value,
string ShortName,
BpdmUrlDataDto Type,
BpdmTechnicalKey Type,
BpdmDataDto Language
);

Expand All @@ -71,23 +71,23 @@ public record BpdmThoroughfareDto(
string ShortName,
string Number,
string Direction,
BpdmUrlDataDto Type,
BpdmTechnicalKey Type,
BpdmDataDto Language
);

public record BpdmPremiseDto(
string Value,
string ShortName,
string Number,
BpdmUrlDataDto Type,
BpdmTechnicalKey Type,
BpdmDataDto Language
);

public record BpdmPostalDeliveryPointDto(
string Value,
string ShortName,
string Number,
BpdmUrlDataDto Type,
BpdmTechnicalKey Type,
BpdmDataDto Language
);

Expand Down
Loading

0 comments on commit d9c725d

Please sign in to comment.