Skip to content

Commit

Permalink
feat(bpdm): adjust bpdm service calls to new service (#164)
Browse files Browse the repository at this point in the history
* structure of bpdm service has changed, therefor the models have been aligned
* feat(bpdm): Remove FetchBusinessPartner call
* feat(bpdm): fix search legal-entity
Refs: CPLP-2976
---------
Co-authored-by: Norbert Truchsess <[email protected]>
Reviewed-By: Norbert Truchsess <[email protected]>
  • Loading branch information
Phil91 authored Jul 31, 2023
1 parent 45a131b commit 9dc4d5a
Show file tree
Hide file tree
Showing 23 changed files with 738 additions and 1,503 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
128 changes: 50 additions & 78 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>(),
x.Value, // Value
x.BpdmIdentifierId, // Type
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", data, Options, cancellationToken)
.CatchingIntoServiceExceptionFor("bpdm-search-legal-entities", HttpAsyncResponseMessageExtension.RecoverOptions.INFRASTRUCTURE).ConfigureAwait(false);
try
{
var paginationResponse = await result.Content
var response = await result.Content
.ReadFromJsonAsync<PageOutputResponseBpdmLegalEntityData>(Options, cancellationToken)
.ConfigureAwait(false);
if (paginationResponse?.Content == null || paginationResponse.Errors == null)
if (response?.Content?.Count() != 1)
{
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.Content.Single();
}
catch (JsonException je)
{
Expand Down
65 changes: 65 additions & 0 deletions src/externalsystems/Bpdm.Library/BpnAccess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/********************************************************************************
* Copyright (c) 2021, 2023 Microsoft and BMW Group AG
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

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)
{
_httpClient = httpFactory.CreateClient(nameof(BpnAccess));
}

public async Task<BpdmLegalEntityDto> FetchLegalEntityByBpn(string businessPartnerNumber, string token, CancellationToken cancellationToken)
{
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var uri = new UriBuilder
{
Path = $"pool/api/catena/legal-entities/{Uri.EscapeDataString(businessPartnerNumber)}",
Query = "idType=BPN"
}.Uri;
var result = await _httpClient.GetAsync(uri.PathAndQuery, cancellationToken)
.CatchingIntoServiceExceptionFor("bpn-fetch-legal-entity")
.ConfigureAwait(false);
try
{
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");
}
return legalEntityResponse;
}
catch (JsonException je)
{
throw new ServiceException($"Access to external system bpdm did not return a valid json response: {je.Message}");
}
}
}
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,11 @@
* 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,28 +71,28 @@ 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
);

public record BpdmGeographicCoordinatesDto(
int Longitude,
int Latitude,
int Altitude
double Longitude,
double Latitude,
double Altitude
);
Loading

0 comments on commit 9dc4d5a

Please sign in to comment.