From 080d43d005aae77a0c093f38e4a73111129778d2 Mon Sep 17 00:00:00 2001 From: Richard Treier Date: Mon, 25 Sep 2023 09:53:07 +0200 Subject: [PATCH] feat: migrate data address properties (#490) --- ...ry-builder.ts => asset-request-builder.ts} | 2 +- .../core/services/data-address-properties.ts | 19 ++++++++ .../services/http-params-mapper.service.ts | 46 ++++++++++--------- src/app/core/utils/record-utils.ts | 16 +++++++ .../asset-create-dialog.component.ts | 6 +-- ...act-agreement-transfer-dialog.component.ts | 3 +- ...ract-definition-editor-dialog.component.ts | 3 +- .../new-policy-dialog.component.ts | 3 +- 8 files changed, 67 insertions(+), 31 deletions(-) rename src/app/core/services/{asset-entry-builder.ts => asset-request-builder.ts} (98%) create mode 100644 src/app/core/services/data-address-properties.ts diff --git a/src/app/core/services/asset-entry-builder.ts b/src/app/core/services/asset-request-builder.ts similarity index 98% rename from src/app/core/services/asset-entry-builder.ts rename to src/app/core/services/asset-request-builder.ts index 8fa491d37..993c8821f 100644 --- a/src/app/core/services/asset-entry-builder.ts +++ b/src/app/core/services/asset-request-builder.ts @@ -5,7 +5,7 @@ import {APP_CONFIG, AppConfig} from '../config/app-config'; import {DataAddressMapper} from './data-address-mapper'; @Injectable() -export class AssetEntryBuilder { +export class AssetRequestBuilder { constructor( @Inject(APP_CONFIG) private config: AppConfig, private dataAddressMapper: DataAddressMapper, diff --git a/src/app/core/services/data-address-properties.ts b/src/app/core/services/data-address-properties.ts new file mode 100644 index 000000000..f02cc36d7 --- /dev/null +++ b/src/app/core/services/data-address-properties.ts @@ -0,0 +1,19 @@ +const EDC = 'https://w3id.org/edc/v0.0.1/ns/'; + +export const DataAddressProperty = { + authCode: `${EDC}authCode`, + authKey: `${EDC}authKey`, + baseUrl: `${EDC}baseUrl`, + body: `${EDC}body`, + header: `${EDC}header`, + mediaType: `${EDC}mediaType`, + method: `${EDC}method`, + pathSegments: `${EDC}pathSegments`, + proxyBody: `${EDC}proxyBody`, + proxyMethod: `${EDC}proxyMethod`, + proxyPath: `${EDC}proxyPath`, + proxyQueryParams: `${EDC}proxyQueryParams`, + queryParams: `${EDC}queryParams`, + secretName: `${EDC}secretName`, + type: `${EDC}type`, +}; diff --git a/src/app/core/services/http-params-mapper.service.ts b/src/app/core/services/http-params-mapper.service.ts index 52a71b523..e3a0a1c9f 100644 --- a/src/app/core/services/http-params-mapper.service.ts +++ b/src/app/core/services/http-params-mapper.service.ts @@ -3,8 +3,9 @@ import {AssetDatasourceFormValue} from '../../routes/connector-ui/asset-page/ass import {HttpDatasourceHeaderFormValue} from '../../routes/connector-ui/asset-page/asset-create-dialog/model/http-datasource-header-form-model'; import {HttpDatasourceQueryParamFormValue} from '../../routes/connector-ui/asset-page/asset-create-dialog/model/http-datasource-query-param-form-model'; import {ContractAgreementTransferDialogFormValue} from '../../routes/connector-ui/contract-agreement-page/contract-agreement-transfer-dialog/contract-agreement-transfer-dialog-form-model'; -import {removeNullValues} from '../utils/record-utils'; +import {mapKeys, removeNullValues} from '../utils/record-utils'; import {everythingAfter, everythingBefore} from '../utils/string-utils'; +import {DataAddressProperty} from './data-address-properties'; import {Asset} from './models/asset'; import {HttpRequestParams} from './models/http-request-params'; @@ -46,33 +47,36 @@ export class HttpRequestParamsMapper { asset.httpDatasourceHintsProxyBody; return removeNullValues({ - method: proxyMethod ? method : null, - pathSegments: proxyPath ? pathSegments : null, - queryParams: proxyQueryParams ? queryParams : null, - body: proxyBody ? body : null, - mediaType: proxyBody ? contentType : null, + [DataAddressProperty.method]: proxyMethod ? method : null, + [DataAddressProperty.pathSegments]: proxyPath ? pathSegments : null, + [DataAddressProperty.queryParams]: proxyQueryParams ? queryParams : null, + [DataAddressProperty.body]: proxyBody ? body : null, + [DataAddressProperty.mediaType]: proxyBody ? contentType : null, }); } encodeHttpRequestParams( httpRequestParams: HttpRequestParams, ): Record { + const bool = (b?: boolean | null) => (b ? 'true' : null); + const props: Record = { - type: 'HttpData', - baseUrl: httpRequestParams.baseUrl, - method: httpRequestParams.method, - authKey: httpRequestParams.authHeaderName, - authCode: httpRequestParams.authHeaderValue, - secretName: httpRequestParams.authHeaderSecretName, - proxyMethod: httpRequestParams.proxyMethod ? 'true' : null, - proxyPath: httpRequestParams.proxyPath ? 'true' : null, - proxyQueryParams: httpRequestParams.proxyQueryParams ? 'true' : null, - proxyBody: httpRequestParams.proxyBody ? 'true' : null, - queryParams: httpRequestParams.queryParams, - ...Object.fromEntries( - Object.entries(httpRequestParams.headers).map( - ([headerName, headerValue]) => [`header:${headerName}`, headerValue], - ), + [DataAddressProperty.type]: 'HttpData', + [DataAddressProperty.baseUrl]: httpRequestParams.baseUrl, + [DataAddressProperty.method]: httpRequestParams.method, + [DataAddressProperty.authKey]: httpRequestParams.authHeaderName, + [DataAddressProperty.authCode]: httpRequestParams.authHeaderValue, + [DataAddressProperty.secretName]: httpRequestParams.authHeaderSecretName, + [DataAddressProperty.proxyMethod]: bool(httpRequestParams.proxyMethod), + [DataAddressProperty.proxyPath]: bool(httpRequestParams.proxyPath), + [DataAddressProperty.proxyQueryParams]: bool( + httpRequestParams.proxyQueryParams, + ), + [DataAddressProperty.proxyBody]: bool(httpRequestParams.proxyBody), + [DataAddressProperty.queryParams]: httpRequestParams.queryParams, + ...mapKeys( + httpRequestParams.headers, + (k) => `${DataAddressProperty.header}:${k}`, ), }; return removeNullValues(props); diff --git a/src/app/core/utils/record-utils.ts b/src/app/core/utils/record-utils.ts index ecdac1d45..3854067b3 100644 --- a/src/app/core/utils/record-utils.ts +++ b/src/app/core/utils/record-utils.ts @@ -9,3 +9,19 @@ export function removeNullValues( Object.entries(obj).filter(([_, v]) => v != null) as [string, string][], ); } + +/** + * Maps keys of a given object + * @param obj object + * @param mapFn key mapper + * @return new object with keys mapped + */ +export function mapKeys< + K extends string | number | symbol, + L extends string | number | symbol, + V, +>(obj: Record, mapFn: (key: K) => L): Record { + return Object.fromEntries( + Object.entries(obj).map(([k, v]) => [mapFn(k as K), v]), + ) as Record; +} diff --git a/src/app/routes/connector-ui/asset-page/asset-create-dialog/asset-create-dialog.component.ts b/src/app/routes/connector-ui/asset-page/asset-create-dialog/asset-create-dialog.component.ts index b3542a0fa..fc9b1ab6a 100644 --- a/src/app/routes/connector-ui/asset-page/asset-create-dialog/asset-create-dialog.component.ts +++ b/src/app/routes/connector-ui/asset-page/asset-create-dialog/asset-create-dialog.component.ts @@ -3,7 +3,7 @@ import {MatDialogRef} from '@angular/material/dialog'; import {Subject} from 'rxjs'; import {finalize, takeUntil} from 'rxjs/operators'; import {EdcApiService} from '../../../../core/services/api/edc-api.service'; -import {AssetEntryBuilder} from '../../../../core/services/asset-entry-builder'; +import {AssetRequestBuilder} from '../../../../core/services/asset-request-builder'; import {NotificationService} from '../../../../core/services/notification.service'; import {ValidationMessages} from '../../../../core/validators/validation-messages'; import {AssetCreateDialogForm} from './asset-create-dialog-form'; @@ -19,7 +19,7 @@ import {AssetMetadataFormBuilder} from './model/asset-metadata-form-builder'; AssetAdvancedFormBuilder, AssetDatasourceFormBuilder, AssetCreateDialogForm, - AssetEntryBuilder, + AssetRequestBuilder, AssetMetadataFormBuilder, ], }) @@ -32,7 +32,7 @@ export class AssetCreateDialogComponent implements OnDestroy { private edcApiService: EdcApiService, public form: AssetCreateDialogForm, public validationMessages: ValidationMessages, - private assetEntryBuilder: AssetEntryBuilder, + private assetEntryBuilder: AssetRequestBuilder, private notificationService: NotificationService, private dialogRef: MatDialogRef, ) {} diff --git a/src/app/routes/connector-ui/contract-agreement-page/contract-agreement-transfer-dialog/contract-agreement-transfer-dialog.component.ts b/src/app/routes/connector-ui/contract-agreement-page/contract-agreement-transfer-dialog/contract-agreement-transfer-dialog.component.ts index f1f533580..5f5e0a530 100644 --- a/src/app/routes/connector-ui/contract-agreement-page/contract-agreement-transfer-dialog/contract-agreement-transfer-dialog.component.ts +++ b/src/app/routes/connector-ui/contract-agreement-page/contract-agreement-transfer-dialog/contract-agreement-transfer-dialog.component.ts @@ -4,7 +4,6 @@ import {Subject} from 'rxjs'; import {finalize} from 'rxjs/operators'; import {ContractAgreementTransferRequest} from '@sovity.de/edc-client'; import {EdcApiService} from '../../../../core/services/api/edc-api.service'; -import {AssetEntryBuilder} from '../../../../core/services/asset-entry-builder'; import {DataAddressMapper} from '../../../../core/services/data-address-mapper'; import {HttpRequestParamsMapper} from '../../../../core/services/http-params-mapper.service'; import {NotificationService} from '../../../../core/services/notification.service'; @@ -17,7 +16,7 @@ import {ContractAgreementTransferDialogResult} from './contract-agreement-transf @Component({ selector: 'contract-agreement-transfer-dialog', templateUrl: './contract-agreement-transfer-dialog.component.html', - providers: [ContractAgreementTransferDialogForm, AssetEntryBuilder], + providers: [ContractAgreementTransferDialogForm], }) export class ContractAgreementTransferDialogComponent implements OnDestroy { loading = false; diff --git a/src/app/routes/connector-ui/contract-definition-page/contract-definition-editor-dialog/contract-definition-editor-dialog.component.ts b/src/app/routes/connector-ui/contract-definition-page/contract-definition-editor-dialog/contract-definition-editor-dialog.component.ts index 847984ac8..a77938604 100644 --- a/src/app/routes/connector-ui/contract-definition-page/contract-definition-editor-dialog/contract-definition-editor-dialog.component.ts +++ b/src/app/routes/connector-ui/contract-definition-page/contract-definition-editor-dialog/contract-definition-editor-dialog.component.ts @@ -4,7 +4,6 @@ import {Subject} from 'rxjs'; import {finalize, takeUntil} from 'rxjs/operators'; import {PolicyDefinitionDto} from '@sovity.de/edc-client'; import {EdcApiService} from '../../../../core/services/api/edc-api.service'; -import {AssetEntryBuilder} from '../../../../core/services/asset-entry-builder'; import {AssetServiceMapped} from '../../../../core/services/asset-service-mapped'; import {ContractDefinitionBuilder} from '../../../../core/services/contract-definition-builder'; import {Asset} from '../../../../core/services/models/asset'; @@ -16,7 +15,7 @@ import {ContractDefinitionEditorDialogResult} from './contract-definition-editor @Component({ selector: 'contract-definition-editor-dialog', templateUrl: './contract-definition-editor-dialog.component.html', - providers: [ContractDefinitionEditorDialogForm, AssetEntryBuilder], + providers: [ContractDefinitionEditorDialogForm], }) export class ContractDefinitionEditorDialog implements OnInit, OnDestroy { policies: PolicyDefinitionDto[] = []; diff --git a/src/app/routes/connector-ui/policy-definition-page/new-policy-dialog/new-policy-dialog.component.ts b/src/app/routes/connector-ui/policy-definition-page/new-policy-dialog/new-policy-dialog.component.ts index 147903389..9d3bc94a2 100644 --- a/src/app/routes/connector-ui/policy-definition-page/new-policy-dialog/new-policy-dialog.component.ts +++ b/src/app/routes/connector-ui/policy-definition-page/new-policy-dialog/new-policy-dialog.component.ts @@ -3,7 +3,6 @@ import {MatDialogRef} from '@angular/material/dialog'; import {Subject} from 'rxjs'; import {finalize, takeUntil} from 'rxjs/operators'; import {EdcApiService} from '../../../../core/services/api/edc-api.service'; -import {AssetEntryBuilder} from '../../../../core/services/asset-entry-builder'; import {NotificationService} from '../../../../core/services/notification.service'; import {PolicyDefinitionBuilder} from '../../../../core/services/policy-definition-builder'; import {ValidationMessages} from '../../../../core/validators/validation-messages'; @@ -13,7 +12,7 @@ import {NewPolicyDialogResult} from './new-policy-dialog-result'; @Component({ selector: 'new-policy-dialog', templateUrl: './new-policy-dialog.component.html', - providers: [NewPolicyDialogForm, AssetEntryBuilder], + providers: [NewPolicyDialogForm], }) export class NewPolicyDialogComponent implements OnDestroy { loading = false;