-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdiscoveryServiceApi.ts
148 lines (126 loc) · 4.79 KB
/
discoveryServiceApi.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { encodeBase64 } from 'lib/util/Base64Util';
import { DiscoveryEntry, IDiscoveryServiceApi } from 'lib/api/discovery-service-api/discoveryServiceApiInterface';
import { DiscoveryServiceApiInMemory } from 'lib/api/discovery-service-api/discoveryServiceApiInMemory';
import { ApiResponseWrapper, wrapErrorCode, wrapSuccess } from 'lib/util/apiResponseWrapper/apiResponseWrapper';
import { SpecificAssetId } from '@aas-core-works/aas-core3.0-typescript/types';
import * as path from 'node:path';
import ServiceReachable from 'test-utils/TestUtils';
type DiscoveryEntryResponse = {
paging_metadata: object;
result: string[];
};
export class DiscoveryServiceApi implements IDiscoveryServiceApi {
private constructor(
protected baseUrl: string,
protected http: {
fetch<T>(url: RequestInfo, init?: RequestInit): Promise<ApiResponseWrapper<T>>;
},
) {}
static create(
baseUrl: string,
http: {
fetch<T>(url: RequestInfo, init?: RequestInit): Promise<ApiResponseWrapper<T>>;
},
): DiscoveryServiceApi {
return new DiscoveryServiceApi(baseUrl, http);
}
static createNull(
baseUrl: string,
discoveryEntries: { assetId: string; aasId: string }[],
reachable: ServiceReachable = ServiceReachable.Yes,
): DiscoveryServiceApiInMemory {
return new DiscoveryServiceApiInMemory(baseUrl, discoveryEntries, reachable);
}
getBaseUrl(): string {
return this.baseUrl;
}
async linkAasIdAndAssetId(
aasId: string,
assetId: string,
apikey?: string,
): Promise<ApiResponseWrapper<DiscoveryEntry>> {
const assetLink = {
name: 'globalAssetId',
value: assetId,
} as SpecificAssetId;
const options = apikey ? { ApiKey: apikey } : undefined;
const response = await this.postAllAssetLinksById(aasId, assetLink, options);
if (!response.isSuccess) return wrapErrorCode(response.errorCode, response.message);
return wrapSuccess({
aasId: aasId,
asset: response.result,
});
}
async getAasIdsByAssetId(assetId: string) {
return this.getAllAssetAdministrationShellIdsByAssetLink([
{
name: 'globalAssetId',
value: assetId,
} as SpecificAssetId,
]);
}
async getAllAssetAdministrationShellIdsByAssetLink(
assetIds: SpecificAssetId[],
options?: object,
): Promise<ApiResponseWrapper<string[]>> {
const headers = {
Accept: 'application/json',
'Content-Type': 'application/json',
...options,
};
const url = new URL(path.posix.join(this.baseUrl, 'lookup/shells'));
assetIds.forEach((obj) => {
url.searchParams.append('assetIds', encodeBase64(JSON.stringify(obj)));
});
const response = await this.http.fetch<DiscoveryEntryResponse>(url.toString(), {
method: 'GET',
headers,
});
if (!response.isSuccess) return wrapErrorCode(response.errorCode, response.message);
return wrapSuccess(response.result.result);
}
async getAllAssetLinksById(aasId: string, options?: object): Promise<ApiResponseWrapper<SpecificAssetId[]>> {
const b64_aasId = encodeBase64(aasId);
const headers = {
Accept: 'application/json',
'Content-Type': 'application/json',
...options,
};
const url = path.posix.join(this.baseUrl, 'lookup/shells', b64_aasId);
return this.http.fetch<SpecificAssetId[]>(url, {
method: 'GET',
headers,
});
}
async postAllAssetLinksById(
aasId: string,
assetLinks: SpecificAssetId, // this is NOT a list in the specification
options?: object,
): Promise<ApiResponseWrapper<SpecificAssetId>> {
const b64_aasId = encodeBase64(aasId);
const headers = {
Accept: 'application/json',
'Content-Type': 'application/json',
...options,
};
const url = path.posix.join(this.baseUrl, 'lookup/shells', b64_aasId);
return this.http.fetch<SpecificAssetId>(url, {
method: 'POST',
headers,
body: JSON.stringify(assetLinks),
});
}
async deleteAllAssetLinksById(aasId: string, options?: object): Promise<ApiResponseWrapper<void>> {
const b64_aasId = encodeBase64(aasId);
const headers = {
Accept: 'application/json',
'Content-Type': 'application/json',
...options,
};
const url = path.posix.join(this.baseUrl, 'lookup/shells', b64_aasId);
return this.http.fetch(url, {
method: 'DELETE',
headers,
});
}
}