-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathShipEngineClient.cs
350 lines (304 loc) · 14.1 KB
/
ShipEngineClient.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
using ShipEngineSDK.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using System.Threading;
using System.Threading.Tasks;
namespace ShipEngineSDK
{
/// <summary>
/// ShipEngine Client is used for handling generic calls and settings that
/// are needed for all ShipEngine API calls.
/// </summary>
public class ShipEngineClient
{
/// <summary>
/// Default constructor
/// </summary>
public ShipEngineClient() { }
/// <summary>
/// Constructor that takes a collection of request modifiers to apply to the request before it is sent
/// </summary>
/// <param name="requestModifiers">Collection of modifiers to be used for each request</param>
protected ShipEngineClient(IEnumerable<Action<HttpRequestMessage>> requestModifiers)
{
this.requestModifiers = requestModifiers;
}
/// <summary>
/// Options for serializing the method call params to JSON.
/// A separate inline setting is used for deserializing the response
/// </summary>
internal static readonly JsonSerializerOptions JsonSerializerOptions = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
IgnoreReadOnlyProperties = true,
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
PropertyNameCaseInsensitive = true,
WriteIndented = true,
Converters = { new JsonStringEnumMemberConverter() },
TypeInfoResolver = new DefaultJsonTypeInfoResolver
{
Modifiers =
{
static typeInfo =>
{
if (typeInfo.Kind != JsonTypeInfoKind.Object)
return;
foreach (JsonPropertyInfo propertyInfo in typeInfo.Properties)
{
// Don't require any properties when deserializing since we should not break consumers
// if the API changes slightly without consumers updating
propertyInfo.IsRequired = false;
// If a property is marked as writeOnly, we should not try deserializing it
var writeOnly = propertyInfo.AttributeProvider?
.GetCustomAttributes(typeof(JsonWriteOnlyAttribute), false).Any() ?? false;
if (writeOnly)
{
propertyInfo.Set = (o, o1) => { };
}
}
}
}
}
};
private static readonly string? OsPlatform = Environment.OSVersion.Platform.ToString();
private static readonly string? OsVersion = Environment.OSVersion.Version.ToString();
private static readonly string? ClrVersion = Environment.Version.ToString();
private static readonly Version? SdkVersionObject = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
private static readonly string SdkVersion = $"{SdkVersionObject.Major}.{SdkVersionObject.Minor}.{SdkVersionObject.Build}";
private const string JsonMediaType = "application/json";
/// <summary>
/// Token to cancel the request
/// </summary>
public CancellationToken CancellationToken { get; set; }
/// <summary>
/// Collections of request modifiers to apply to the request before it is sent
/// </summary>
/// <remarks>
/// This is a collection instead of a single action so that modifiers can be added at multiple levels.
/// For example, a consumer could add a modifier at the client level, and then add another at the method level.
/// </remarks>
protected IEnumerable<Action<HttpRequestMessage>> requestModifiers = [];
/// <summary>
/// Sets the HttpClient User agent, the json media type, and the API key to be used
/// for all ShipEngine API calls unless overrwritten at the method level.
/// </summary>
/// <param name="config">Config object used to configure the HttpClient</param>
/// <param name="client">The HttpClient to be configured</param>
/// <returns></returns>
public static HttpClient ConfigureHttpClient(Config config, HttpClient client)
{
client.DefaultRequestHeaders.Accept.Clear();
var userAgentString = $"shipengine-dotnet/{SdkVersion} {OsPlatform}/{OsVersion} clr/{ClrVersion}";
client.DefaultRequestHeaders.Add("User-Agent", userAgentString);
client.DefaultRequestHeaders.Add("Api-Key", config.ApiKey);
client.DefaultRequestHeaders.Add("Accept", JsonMediaType);
if (client.BaseAddress == null)
{
client.BaseAddress = new Uri("https://api.shipengine.com");
}
client.Timeout = config.Timeout;
return client;
}
/// <summary>
/// Sets the HttpClient User agent, the json media type, and the API key to be used
/// for all ShipEngine API calls unless overwritten at the method level.
/// </summary>
/// <param name="client">The HttpClient to be configured</param>
/// <param name="apiKey">The API key to be used for all ShipEngine API calls</param>
/// <param name="baseUri">The base URI for the ShipEngine API</param>
/// <param name="timeout">The timeout for the ShipEngine API Calls</param>
/// <returns></returns>
public static HttpClient ConfigureHttpClient(HttpClient client, string apiKey, Uri? baseUri, TimeSpan? timeout = null)
{
var config = new Config(apiKey, timeout);
client.BaseAddress = baseUri ?? new Uri("https://api.shipengine.com");
return ConfigureHttpClient(config, client);
}
private async Task<T> DeserializedResultOrThrow<T>(HttpResponseMessage response)
{
var contentString = await response.Content.ReadAsStringAsync();
string? requestId = null;
if (response.Headers.TryGetValues("x-shipengine-requestid", out var requestIdValues))
{
requestId = requestIdValues.FirstOrDefault();
}
if (!response.IsSuccessStatusCode)
{
ShipEngineAPIError? deserializedError = null;
try
{
deserializedError =
JsonSerializer.Deserialize<ShipEngineAPIError>(contentString, JsonSerializerOptions);
}
catch (JsonException)
{
}
if (deserializedError == null)
{
// in this case, the response body was not parseable JSON
throw new ShipEngineException("Unexpected HTTP status", requestID: requestId, responseMessage: response);
}
var error = deserializedError.Errors?.FirstOrDefault(e => e.Message != null);
// if error is null, it means we got back a JSON response, but it wasn't the format we expected
throw new ShipEngineException(
error?.Message ?? response.ReasonPhrase,
error?.ErrorSource ?? ErrorSource.Shipengine,
error?.ErrorType ?? ErrorType.System,
error?.ErrorCode ?? ErrorCode.Unspecified,
deserializedError.RequestId ?? requestId,
response
);
}
T? result;
// If the caller asked for a string, return the response as-is. This can be useful for no content responses.
if (typeof(T).IsAssignableFrom(typeof(string)))
{
return (T)(object)(contentString ?? "");
}
try
{
result = JsonSerializer.Deserialize<T>(contentString, JsonSerializerOptions);
}
catch (JsonException)
{
throw new ShipEngineException("Unable to parse response", requestID: requestId, responseMessage: response);
}
if (result != null)
{
return result;
}
throw new ShipEngineException(message: "Unexpected null response", requestID: requestId, responseMessage: response);
}
/// <summary>
/// Builds and sends an HTTP Request to the ShipEngine Client, has special logic for handling
/// 429 rate limit exceeded errors and subsequent retry logic.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="method"></param>
/// <param name="requestOptions">Options for the request</param>
/// <param name="client"></param>
/// <param name="config"></param>
/// <param name="cancellationToken">Token that can be used to cancel the request</param>
/// <returns></returns>
public virtual Task<T> SendHttpRequestAsync<T>(HttpMethod method, RequestOptions requestOptions,
HttpClient client, Config config, CancellationToken cancellationToken) =>
SendHttpRequestAsync<T>(method, requestOptions.FullPath(), requestOptions.Data, client, config, cancellationToken);
/// <summary>
/// Builds and sends an HTTP Request to the ShipEngine Client, has special logic for handling
/// 429 rate limit exceeded errors and subsequent retry logic.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="method"></param>
/// <param name="path"></param>
/// <param name="jsonContent"></param>
/// <param name="client"></param>
/// <param name="config"></param>
/// <returns></returns>
public virtual Task<T> SendHttpRequestAsync<T>(HttpMethod method, string path, string? jsonContent, HttpClient client, Config config) =>
SendHttpRequestAsync<T>(method, path, jsonContent, client, config, CancellationToken);
/// <summary>
/// Builds and sends an HTTP Request to the ShipEngine Client, has special logic for handling
/// 429 rate limit exceeded errors and subsequent retry logic.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="method"></param>
/// <param name="path"></param>
/// <param name="jsonContent"></param>
/// <param name="client"></param>
/// <param name="config"></param>
/// <param name="cancellationToken">Token that can be used to cancel the request</param>
/// <returns></returns>
public virtual async Task<T> SendHttpRequestAsync<T>(HttpMethod method, string path, string? jsonContent, HttpClient client, Config config, CancellationToken cancellationToken)
{
int retry = 0;
HttpResponseMessage? response = null;
ShipEngineException requestException;
while (true)
{
try
{
var request = BuildRequest(method, path, jsonContent);
foreach (var modifier in requestModifiers ?? [])
{
modifier?.Invoke(request);
}
response = await client.SendAsync(request, cancellationToken);
var deserializedResult = await DeserializedResultOrThrow<T>(response);
return deserializedResult;
}
catch (ShipEngineException e)
{
if (e.ErrorCode != ErrorCode.RateLimitExceeded)
{
throw;
}
requestException = e;
}
if (!ShouldRetry(retry, response?.StatusCode, response?.Headers, config))
{
break;
}
retry += 1;
await WaitAndRetry(response, config, requestException);
}
throw requestException;
}
private async Task WaitAndRetry(HttpResponseMessage? response, Config config, ShipEngineException ex)
{
int? retryAfter;
try
{
retryAfter = Int32.Parse(response?.Headers.GetValues("RetryAfter").First());
}
catch
{
retryAfter = 5;
}
if (config.Timeout.Seconds < retryAfter)
{
throw new ShipEngineException(
$"The request took longer than the {config.Timeout.Milliseconds} milliseconds allowed",
ErrorSource.Shipengine,
ErrorType.System,
ErrorCode.Timeout,
ex.RequestId,
ex.ResponseMessage
);
}
await Task.Delay((int)retryAfter * 1000, CancellationToken).ConfigureAwait(false);
}
private static HttpRequestMessage BuildRequest(HttpMethod method, string path, string? jsonContent)
{
var request = new HttpRequestMessage(method, path);
if (jsonContent != null)
{
request.Content = new StringContent(jsonContent, System.Text.Encoding.UTF8, JsonMediaType);
}
return request;
}
private static bool ShouldRetry(
int numRetries,
HttpStatusCode? statusCode,
HttpHeaders? headers,
Config config)
{
// Do not retry if we are out of retries.
if (numRetries >= config.Retries)
{
return false;
}
if (statusCode == (HttpStatusCode)429 || headers != null && headers.Contains("RetryAfter"))
{
return true;
}
return false;
}
}
}