-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDimClient.cs
More file actions
149 lines (139 loc) · 7.47 KB
/
DimClient.cs
File metadata and controls
149 lines (139 loc) · 7.47 KB
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
149
/********************************************************************************
* Copyright (c) 2024 BMW Group AG
* Copyright 2024 SAP SE or an SAP affiliate company and ssi-dim-middle-layer contributors.
*
* 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 Dim.Clients.Api.Dim.Models;
using Dim.Clients.Extensions;
using Dim.Clients.Token;
using Dim.DbAccess.Models;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
using Org.Eclipse.TractusX.Portal.Backend.Framework.HttpClientExtensions;
using System.Net.Http.Json;
using System.Text.Json;
using System.Web;
namespace Dim.Clients.Api.Dim;
public class DimClient(IBasicAuthTokenService basicAuthTokenService)
: IDimClient
{
public async Task<CompanyData> GetCompanyData(BasicAuthSettings dimAuth, string dimBaseUrl, string tenantName, string application, CancellationToken cancellationToken)
{
var client = await basicAuthTokenService.GetBasicAuthorizedClient<DimClient>(dimAuth, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
var filterString = $"name eq {tenantName} and application eq {application}";
var result = await client.GetAsync($"{dimBaseUrl}/api/v2.0.0/companyIdentities?$filter={HttpUtility.UrlEncode(filterString)}", cancellationToken)
.CatchingIntoServiceExceptionFor("get-company-data", HttpAsyncResponseMessageExtension.RecoverOptions.INFRASTRUCTURE,
async m =>
{
var message = await m.Content.ReadAsStringAsync().ConfigureAwait(ConfigureAwaitOptions.None);
return (false, message);
}).ConfigureAwait(false);
try
{
var response = await result.Content
.ReadFromJsonAsync<CompanyIdentitiesResponse>(JsonSerializerExtensions.Options, cancellationToken)
.ConfigureAwait(ConfigureAwaitOptions.None);
if (response?.Data == null || response.Data.Count() != 1)
{
throw new ConflictException("There is no matching company");
}
return response.Data.Single();
}
catch (JsonException je)
{
throw new ServiceException(je.Message);
}
}
public async Task<string> UpdateCompanyStatus(BasicAuthSettings dimAuth, string dimBaseUrl, Guid companyId, CancellationToken cancellationToken)
{
var client = await basicAuthTokenService.GetBasicAuthorizedClient<DimClient>(dimAuth, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
var result = await client.PatchAsync($"{dimBaseUrl}/api/v2.0.0/companyIdentities/{companyId}/status", null, cancellationToken)
.CatchingIntoServiceExceptionFor("update-company-status", HttpAsyncResponseMessageExtension.RecoverOptions.INFRASTRUCTURE,
async m =>
{
var message = await m.Content.ReadAsStringAsync().ConfigureAwait(ConfigureAwaitOptions.None);
return (false, message);
}).ConfigureAwait(false);
try
{
var response = await result.Content
.ReadFromJsonAsync<CompanyStatusResponse>(JsonSerializerExtensions.Options, cancellationToken)
.ConfigureAwait(ConfigureAwaitOptions.None);
if (response?.Status == null || response.Status != "successful")
{
throw new ServiceException("Company status update failed", true);
}
return response.Status;
}
catch (JsonException je)
{
throw new ServiceException(je.Message);
}
}
public async Task<string> GetStatusList(BasicAuthSettings dimAuth, string dimBaseUrl, Guid companyId, StatusListType statusListType, CancellationToken cancellationToken)
{
var client = await basicAuthTokenService.GetBasicAuthorizedClient<DimClient>(dimAuth, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
var result = await client.GetAsync($"{dimBaseUrl}/api/v2.0.0/companyIdentities/{companyId}/revocationLists", cancellationToken)
.CatchingIntoServiceExceptionFor("get-status-list", HttpAsyncResponseMessageExtension.RecoverOptions.INFRASTRUCTURE,
async m =>
{
var message = await m.Content.ReadAsStringAsync().ConfigureAwait(ConfigureAwaitOptions.None);
return (false, message);
}).ConfigureAwait(false);
try
{
var response = await result.Content
.ReadFromJsonAsync<StatusListListResponse>(JsonSerializerExtensions.Options, cancellationToken)
.ConfigureAwait(ConfigureAwaitOptions.None);
if (response?.Data == null || !response.Data.Where(x => x.Type == statusListType.ToString()).Any(x => x.RemainingSpace > 0))
{
throw new ConflictException("There is no status list with remaining space, please create a new one.");
}
return response.Data.First(x => x.RemainingSpace > 0 && x.Type == statusListType.ToString()).StatusListCredential;
}
catch (JsonException je)
{
throw new ServiceException(je.Message);
}
}
public async Task<string> CreateStatusList(BasicAuthSettings dimAuth, string dimBaseUrl, Guid companyId, StatusListType statusListType, CancellationToken cancellationToken)
{
var client = await basicAuthTokenService.GetBasicAuthorizedClient<DimClient>(dimAuth, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
var data = new CreateStatusListRequest(new CreateStatusListPaypload(new CreateStatusList(statusListType.ToString(), DateTimeOffset.UtcNow.ToString("yyyyMMdd"), "New revocation list", 2097152)));
var result = await client.PostAsJsonAsync($"{dimBaseUrl}/api/v2.0.0/companyIdentities/{companyId}/revocationLists", data, JsonSerializerExtensions.Options, cancellationToken)
.CatchingIntoServiceExceptionFor("assign-application", HttpAsyncResponseMessageExtension.RecoverOptions.INFRASTRUCTURE,
async m =>
{
var message = await m.Content.ReadAsStringAsync().ConfigureAwait(ConfigureAwaitOptions.None);
return (false, message);
}).ConfigureAwait(false);
try
{
var response = await result.Content
.ReadFromJsonAsync<CreateStatusListResponse>(JsonSerializerExtensions.Options, cancellationToken)
.ConfigureAwait(ConfigureAwaitOptions.None);
if (response == null)
{
throw new ServiceException("Response must not be null");
}
return response.RevocationVc.Id;
}
catch (JsonException je)
{
throw new ServiceException(je.Message);
}
}
}