-
Notifications
You must be signed in to change notification settings - Fork 3
/
SveaHttpClient.cs
299 lines (256 loc) · 13 KB
/
SveaHttpClient.cs
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
using Microsoft.Extensions.Logging;
using Svea.WebPay.SDK.Exceptions;
using Svea.WebPay.SDK.Json;
using Svea.WebPay.SDK.PaymentAdminApi.Request;
using Svea.WebPay.SDK.PaymentAdminApi.Response;
using System;
using System.Globalization;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Task = System.Threading.Tasks.Task;
namespace Svea.WebPay.SDK
{
using Svea.WebPay.SDK.PaymentAdminApi;
using System.Diagnostics;
public class SveaHttpClient : ISveaHttpClient
{
private readonly HttpClient _client;
private readonly Credentials _credentials;
private readonly ILogger _logger;
internal SveaHttpClient(HttpClient client, Credentials credentials, ILogger logger)
{
this._client = client;
this._credentials = credentials;
this._logger = logger;
}
/// <summary>
/// Send a HttpGet and Process HttpResponse for a url
/// </summary>
/// <typeparam name="TResponse"></typeparam>
/// <param name="url"></param>
/// <param name="configureAwait"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="HttpRequestException"></exception>
/// <exception cref="HttpResponseException"></exception>
internal async Task<TResponse> HttpGet<TResponse>(Uri url, bool configureAwait) where TResponse : new()
{
var httpRequestMessage = CreateHttpRequestMessage(HttpMethod.Get, url);
return await SendHttpRequestAndProcessHttpResponse<TResponse>(httpRequestMessage, configureAwait).ConfigureAwait(configureAwait);
}
/// <summary>
/// Send a HttpPatch and Process HttpResponse for a url
/// </summary>
/// <typeparam name="TResponse"></typeparam>
/// <param name="url"></param>
/// <param name="payload"></param>
/// <param name="configureAwait"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="HttpRequestException"></exception>
/// <exception cref="HttpResponseException"></exception>
internal async Task<TResponse> HttpPut
<TResponse>(Uri url, object payload, bool configureAwait) where TResponse : new()
{
var httpRequestMessage = CreateHttpRequestMessage(HttpMethod.Put, url, payload);
return await SendHttpRequestAndProcessHttpResponse<TResponse>(httpRequestMessage, configureAwait).ConfigureAwait(configureAwait);
}
/// <summary>
/// Send a HttpPatch and Process HttpResponse for a url
/// </summary>
/// <typeparam name="TResponse"></typeparam>
/// <param name="url"></param>
/// <param name="payload"></param>
/// <param name="configureAwait"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="HttpRequestException"></exception>
/// <exception cref="HttpResponseException"></exception>
internal async Task<TResponse> HttpPatch
<TResponse>(Uri url, object payload, bool configureAwait) where TResponse : new()
{
var httpRequestMessage = CreateHttpRequestMessage(new HttpMethod("PATCH"), url, payload);
return await SendHttpRequestAndProcessHttpResponse<TResponse>(httpRequestMessage, configureAwait).ConfigureAwait(configureAwait);
}
/// <summary>
/// Send a HttpPost and Process HttpResponse for a url
/// </summary>
/// <typeparam name="TResponse"></typeparam>
/// <param name="url"></param>
/// <param name="payload"></param>
/// <param name="configureAwait"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="HttpRequestException"></exception>
/// <exception cref="HttpResponseException"></exception>
internal async Task<TResponse> HttpPost
<TResponse>(Uri url, object payload, bool configureAwait) where TResponse : new()
{
var httpRequestMessage = CreateHttpRequestMessage(HttpMethod.Post, url, payload);
return await SendHttpRequestAndProcessHttpResponse<TResponse>(httpRequestMessage, configureAwait).ConfigureAwait(configureAwait);
}
internal async Task<TResponse> HttpPost<TResponse, TResourceResponse>(Uri url, object payload, PollingTimeout pollingTimeout = null, bool configureAwait = false)
where TResponse : ResourceResponseObject<TResourceResponse>, new()
where TResourceResponse : new()
{
var httpRequestMessage = CreateHttpRequestMessage(HttpMethod.Post, url, payload);
var resourceResponse = await ExecuteResourceRequest<TResponse, TResourceResponse>(httpRequestMessage, pollingTimeout, configureAwait).ConfigureAwait(configureAwait);
if (resourceResponse?.ResourceUri != null)
{
resourceResponse.Resource = await HttpGet<TResourceResponse>(resourceResponse.ResourceUri, configureAwait).ConfigureAwait(configureAwait);
}
return resourceResponse;
}
private async Task<TResponse> ExecuteResourceRequest<TResponse, TResourceResponse>(HttpRequestMessage httpRequestMessage, PollingTimeout timeout = null, bool configureAwait = false)
where TResponse : ResourceResponseObject<TResourceResponse>, new()
{
var polling = true;
if (timeout == null)
{
timeout = new PollingTimeout();
polling = false;
}
using (var cancellationToken = new CancellationTokenSource(timeout.Timeout))
{
try
{
if (cancellationToken.IsCancellationRequested)
{
_logger.LogError("ExecuteResourceRequest timeout");
return default;
}
var response = await SendHttpRequestAndProcessHttpResponse<TResponse>(httpRequestMessage, configureAwait).ConfigureAwait(configureAwait);
response.TaskUri = response.ResourceUri;
try
{
PaymentAdminApi.Models.Task taskResponse = null;
do
{
if (taskResponse is object)
{
await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(configureAwait);
}
taskResponse = await HttpGet<PaymentAdminApi.Models.Task>(response.ResourceUri, configureAwait).ConfigureAwait(configureAwait);
} while (taskResponse.Status == "InProgress" && taskResponse.ResourceUri == null && polling);
response.ResourceUri = taskResponse.ResourceUri;
}
catch (HttpRequestException e)
{
var ex = new HttpRequestException($"Resource object was not returned: {e.Message}");
_logger.LogError(ex, ex.Message);
throw ex;
}
return response;
}
catch (TaskCanceledException e)
{
_logger.LogError("ExecuteResourceRequest timeout", e);
throw;
}
}
}
/// <summary>
/// Send the HttpRequest and Process HttpResponse. Sets ResourceUri header if TResponse is IResourceResponse
/// </summary>
/// <typeparam name="TResponse"></typeparam>
/// <param name="httpRequest"></param>
/// <param name="configureAwait"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="HttpRequestException"></exception>
/// <exception cref="HttpResponseException"></exception>
/// <returns></returns>
internal async Task<TResponse> SendHttpRequestAndProcessHttpResponse<TResponse>(HttpRequestMessage httpRequest, bool configureAwait) where TResponse : new()
{
await SetRequestHeaders(httpRequest, configureAwait);
var requestBody = string.Empty;
if (httpRequest.Content != null)
{
requestBody = await httpRequest.Content.ReadAsStringAsync().ConfigureAwait(configureAwait);
}
var httpResponse = await _client.SendAsync(httpRequest).ConfigureAwait(configureAwait);
string BuildErrorMessage(string httpResponseBody)
{
return
$"{httpRequest.Method}: {httpRequest.RequestUri} failed with error code {httpResponse.StatusCode} using bearer token {httpRequest.Headers.Authorization?.Parameter}. Request body: {requestBody}. Response body: {httpResponseBody}";
}
try
{
var httpResponseBody = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(configureAwait);
if (!httpResponse.IsSuccessStatusCode && httpResponse.StatusCode != HttpStatusCode.SeeOther)
{
throw new HttpResponseException(
httpResponse,
!string.IsNullOrWhiteSpace(httpResponseBody)
? JsonSerializer.Deserialize<ErrorResponse>(httpResponseBody)
: null,
BuildErrorMessage(httpResponseBody));
}
var responsObj = new TResponse();
if (httpResponse.StatusCode != HttpStatusCode.NoContent && !string.IsNullOrWhiteSpace(httpResponseBody))
{
responsObj = JsonSerializer.Deserialize<TResponse>(httpResponseBody, JsonSerialization.Settings);
if (responsObj == null)
{
responsObj = new TResponse();
}
}
SetLocation(responsObj, httpResponse);
return responsObj;
}
catch (HttpResponseException ex)
{
_logger.LogError(ex, ex.Message);
throw;
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
var httpResponseBody = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(configureAwait);
throw new HttpResponseException(
httpResponse,
message: BuildErrorMessage(httpResponseBody),
innerException: ex);
}
}
private void SetLocation<TResponse>(TResponse responseObject, HttpResponseMessage httpResponseMessage) where TResponse : new()
{
if (responseObject is IResourceResponse locationResponse)
{
locationResponse.ResourceUri = httpResponseMessage.Headers.Location;
}
}
private async Task SetRequestHeaders(HttpRequestMessage httpRequest, bool configureAwait)
{
var timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
var token = CreateAuthenticationToken(httpRequest.Content != null ? await httpRequest.Content.ReadAsStringAsync().ConfigureAwait(configureAwait) : string.Empty, timestamp);
httpRequest.Headers.Add("Authorization", "Svea" + " " + token);
httpRequest.Headers.Add("Timestamp", timestamp);
}
private string CreateAuthenticationToken(string requestBody, string timestamp)
{
using (var sha512 = SHA512.Create())
{
var hashBytes = sha512.ComputeHash(Encoding.UTF8.GetBytes(string.Concat(requestBody, _credentials.Secret, timestamp)));
var hashString = BitConverter.ToString(hashBytes).Replace("-", string.Empty).ToLower();
return Convert.ToBase64String(Encoding.UTF8.GetBytes(_credentials.MerchantId + ":" + hashString));
}
}
private HttpRequestMessage CreateHttpRequestMessage(HttpMethod httpMethod, Uri url, object payload = null)
{
var httpRequestMessage = new HttpRequestMessage(httpMethod, url);
if (payload != null)
{
var content = JsonSerializer.Serialize(payload, JsonSerialization.Settings);
httpRequestMessage.Content = new StringContent(content, Encoding.UTF8, "application/json");
}
httpRequestMessage.Headers.Add("Accept", "application/json");
return httpRequestMessage;
}
}
}