-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessingClient.cs
More file actions
164 lines (137 loc) · 5.54 KB
/
Copy pathProcessingClient.cs
File metadata and controls
164 lines (137 loc) · 5.54 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using RestSharp;
using RestSharp.Serializers;
using RestSharp.Serializers.NewtonsoftJson;
using Acme.DemoServer.Processing.Api.Common;
using Acme.DemoServer.Processing.Api.Common.Dtos.DemoObject;
using Acme.DemoServer.Processing.Api.Common.Dtos.DemoObject.Update;
using Acme.Wattle.Common.Exceptions;
using Acme.Wattle.Common.Interfaces;
using Acme.Wattle.Json.Extensions;
using Acme.Wattle.Primitives;
using Acme.Wattle.Utils;
using System.Globalization;
using System.Net;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace Acme.DemoServer.Processing.Api.Clients;
public sealed class ProcessingClient : IProcessingClient
{
private IRestClient m_restClient;
private readonly bool m_disposeRestClient;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
// ReSharper disable once ConvertToPrimaryConstructor
// ReSharper disable once MemberCanBePrivate.Global
public ProcessingClient(
IRestClient restClient,
bool disposeRestClient = false)
{
m_restClient = restClient;
m_disposeRestClient = disposeRestClient;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ProcessingClient(
HttpClient httpClient,
bool disposeHttpClient = false)
: this(
new RestClient(
httpClient,
disposeHttpClient: disposeHttpClient,
configureSerialization: s => UpdateSerializerConfig(s)),
true)
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public async ValueTask<MetaServerDescription> GetDescriptionAsync(
CancellationToken cancellationToken = default)
{
var request = new RestRequest(ServerControllerConstants.MethodDescription.UrlSuffix);
var response = await m_restClient.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
var result = ReadResponse<MetaServerDescription>(response);
return result;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public async ValueTask<DemoObjectInfo> DemoObjectReadAsync(
long id,
CancellationToken cancellationToken = default)
{
var request =
new RestRequest(DemoObjectControllerConstants.MethodRead.UrlSuffix)
.AddQueryParameter(DemoObjectControllerConstants.MethodRead.Arguments.Id, id.ToString(CultureInfo.InvariantCulture));
var response = await m_restClient.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
var result = ReadResponse<DemoObjectInfo>(response);
return result;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public async ValueTask<DemoObjectInfo> DemoObjectCreateAsync(
DemoObjectCreate parameters,
CancellationToken cancellationToken = default)
{
var request =
new RestRequest(DemoObjectControllerConstants.MethodCreate.UrlSuffix, Method.Post)
.AddJsonBody(parameters);
var response = await m_restClient.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
var result = ReadResponse<DemoObjectInfo>(response);
return result;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public async ValueTask<DemoObjectInfo> DemoObjectUpdateAsync(
DemoObjectUpdate parameters,
CancellationToken cancellationToken = default)
{
var request =
new RestRequest(DemoObjectControllerConstants.MethodUpdate.UrlSuffix, Method.Post)
.AddJsonBody(parameters);
var response = await m_restClient.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
var result = ReadResponse<DemoObjectInfo>(response);
return result;
}
#region Helpers
[MethodImpl(MethodImplOptions.AggressiveInlining)]
// ReSharper disable once MemberCanBePrivate.Global
// ReSharper disable once UnusedMethodReturnValue.Global
public static SerializerConfig UpdateSerializerConfig(SerializerConfig config)
{
config.UseNewtonsoftJson(JsonExtensions.CreateSettings());
return config;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private T ReadResponse<T>(RestResponse response)
{
if (response.StatusCode == HttpStatusCode.OK)
{
var result = m_restClient.Serializers.DeserializeContent<T>(response);
if (result == null)
{
ThrowsHelper.ThrowInvalidOperationException("Не удалось прочитать ответ.");
}
return result!;
}
if (response.StatusCode == HttpStatusCode.Conflict)
{
var workflowExceptionData = m_restClient.Serializers.DeserializeContent<WorkflowExceptionData>(response);
if (workflowExceptionData == null)
{
ThrowsHelper.ThrowInvalidOperationException("Не удалось прочитать ошибку в ответе.");
}
workflowExceptionData!.Throw();
}
if (response.StatusCode == HttpStatusCode.BadRequest)
{
ThrowsHelper.ThrowInvalidOperationException($@"Ошибка '{response.StatusCode}'
{response.Content}");
}
response.ThrowIfError();
ThrowsHelper.ThrowInvalidOperationException($"Ошибка '{response.StatusCode}'.");
return default!;
}
#endregion
public void Dispose()
{
if (m_disposeRestClient)
{
CommonWattleExtensions.SilentDisposeAndFree(ref m_restClient!);
}
}
}