-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHttpService.cs
More file actions
370 lines (324 loc) · 15.1 KB
/
HttpService.cs
File metadata and controls
370 lines (324 loc) · 15.1 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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
namespace PCPPlugins.BusinessLayer.Http
{
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using PCPPlugins.BusinessLayer.Extensions;
/// <summary>
/// Implementation For Generic Http Service Class
/// </summary>
public class HttpService : IHttpService
{
private readonly ITracingService _tracingService;
/// <summary>
/// Http Client
/// </summary>
private readonly HttpClient _httpClient;
/// <summary>
/// Consturctor
/// </summary>
/// <param name="httpClient"></param>
/// <param name="logger"></param>
public HttpService(HttpClient httpClient, ITracingService tracingService)
{
_httpClient = httpClient;
_tracingService = tracingService;
}
//#region Client Configuration
///// <summary>
///// Extension method to add HttpClient using Startup Class
///// </summary>
///// <param name="services"></param>
//public static void AddHttpClientToServiceCollection(IServiceCollection services)
//{
// services.AddHttpClient<IHttpService, HttpService>();
//}
//#endregion
#region Request
public Task<HttpResult<T>> GetObjectAsync<T>(string url, Dictionary<string, string> headers = null, bool isFormUrlEncodedContent = false)
{
return ProcessRequestAsync<T>(HttpMethod.Get, url, null, headers, isFormUrlEncodedContent);
}
public Task<HttpResult<string>> GetStringAsync(string url, Dictionary<string, string> headers = null, bool isFormUrlEncodedContent = false)
{
return ProcessRequestAsync<string>(HttpMethod.Get, url, null, headers, isFormUrlEncodedContent);
}
public Task<HttpResult<T>> PostJsonAndGetObject<T>(string url, object requestBody, Dictionary<string, string> headers = null, bool isFormUrlEncodedContent = false)
{
return ProcessRequestAsync<T>(HttpMethod.Post, url, requestBody, headers, isFormUrlEncodedContent);
}
public Task<HttpResult<string>> PostJsonAndGetString(string url, object requestBody, Dictionary<string, string> headers = null, bool isFormUrlEncodedContent = false)
{
return ProcessRequestAsync<string>(HttpMethod.Post, url, requestBody, headers, isFormUrlEncodedContent);
}
public Task<HttpResult<T>> PutJsonAndGetObject<T>(string url, object requestBody, Dictionary<string, string> headers = null, bool isFormUrlEncodedContent = false)
{
return ProcessRequestAsync<T>(HttpMethod.Put, url, requestBody, headers, isFormUrlEncodedContent);
}
public Task<HttpResult<string>> PutJsonAndGetString(string url, object requestBody, Dictionary<string, string> headers = null, bool isFormUrlEncodedContent = false)
{
return ProcessRequestAsync<string>(HttpMethod.Put, url, requestBody, headers, isFormUrlEncodedContent);
}
public Task<HttpResult<T>> DeleteJsonAndGetObject<T>(string url, object requestBody, Dictionary<string, string> headers = null, bool isFormUrlEncodedContent = false)
{
return ProcessRequestAsync<T>(HttpMethod.Delete, url, requestBody, headers, isFormUrlEncodedContent);
}
public Task<HttpResult<string>> DeleteJsonAndGetString(string url, object requestBody, Dictionary<string, string> headers = null, bool isFormUrlEncodedContent = false)
{
return ProcessRequestAsync<string>(HttpMethod.Delete, url, requestBody, headers, isFormUrlEncodedContent);
}
public Task<HttpResult<T>> PatchJsonAndGetObject<T>(string url, object requestBody, Dictionary<string, string> headers = null, bool isFormUrlEncodedContent = false)
{
return ProcessRequestAsync<T>(new HttpMethod("PATCH"), url, requestBody, headers, isFormUrlEncodedContent);
}
public Task<HttpResult<string>> PatchJsonAndGetString(string url, object requestBody, Dictionary<string, string> headers = null, bool isFormUrlEncodedContent = false)
{
return ProcessRequestAsync<string>(new HttpMethod("PATCH"), url, requestBody, headers, isFormUrlEncodedContent);
}
#endregion
#region Helper methods
/// <summary>
/// Process Request Asynchronously
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="method"></param>
/// <param name="url"></param>
/// <param name="requestBody"></param>
/// <param name="headers"></param>
/// <returns></returns>
private async Task<HttpResult<T>> ProcessRequestAsync<T>(HttpMethod method, string url, object requestBody, Dictionary<string, string> headers = null, bool isFormUrlEncodedContent = false)
{
try
{
if (isFormUrlEncodedContent)
{
List<KeyValuePair<string, string>> payload = requestBody as List<KeyValuePair<string, string>>;
using (HttpRequestMessage request = CreateFormUrlEncodedContentRequest(method, url, headers, payload))
{
using (HttpResponseMessage response = await _httpClient.SendAsync(request).ConfigureAwait(false))
{
return await GetHttpResponseMessageFromHttpResponse<T>(response).ConfigureAwait(false);
}
}
}
using (HttpRequestMessage request = CreateRequest(method, url, headers, requestBody))
{
using (HttpResponseMessage response = await _httpClient.SendAsync(request).ConfigureAwait(false))
{
return await GetHttpResponseMessageFromHttpResponse<T>(response).ConfigureAwait(false);
}
}
}
catch (HttpRequestException hre)
{
_tracingService.Trace($"Http Request Exception triggered for {url} , {hre.Message}");
_tracingService.Trace($"Stack trace Exception triggered for {url} , {hre.StackTrace}");
if (hre.InnerException != null)
_tracingService.Trace($"Inner Exception Exception triggered for {url} , {hre.InnerException.Message}");
throw GetException(
$"Http Request Exception triggered for {url}",
originalException: hre,
url,
requestBody,
typeof(T).FullName);
}
catch (HttpException ex)
{
_tracingService.Trace($"Http Exception triggered for {url} , {ex.Message}");
throw GetException(
$"Unexpected exeption when calling {url}",
originalException: ex,
url,
requestBody,
typeof(T).FullName);
}
catch (Exception ex)
{
_tracingService.Trace($"Exception triggered for {url}, {ex.Message} ");
throw GetException(
$"Unexpected exeption when calling {url}",
originalException: ex,
url,
requestBody,
typeof(T).FullName);
}
}
/// <summary>
/// Get Http Response Message From Http Response
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="response"></param>
/// <returns></returns>
public static async Task<HttpResult<T>> GetHttpResponseMessageFromHttpResponse<T>(HttpResponseMessage response)
{
if (response is null) return null;
var requestBody = response?.RequestMessage?.Content != null ? await response.RequestMessage.Content.ReadAsStringAsync().ConfigureAwait(false) : string.Empty;
var responseBody = response?.Content != null ? await response.Content.ReadAsStringAsync().ConfigureAwait(false) : string.Empty;
var httpResult = new HttpResult<T>
{
RequestUri = response.RequestMessage.RequestUri.ToString(),
StatusCode = response.StatusCode,
ResponseBody = responseBody,
ReasonPhrase = response.ReasonPhrase,
IsSuccessful = response.IsSuccessStatusCode,
};
try
{
if (response.IsSuccessStatusCode)
{
// Only Serialize if return a specific type
if (typeof(T) != typeof(string))
{
//var serializerSettings = new JsonSerializerOptions
//{
// StringEscapeHandling = StringEscapeHandling.EscapeHtml
//};
//httpResult.Result = JsonSerializer.Deserialize<T>(responseBody, serializerSettings);
httpResult.Result = JsonSerializerExtension.Deserialize<T>(responseBody);
}
else
{
httpResult.Result = (T)Convert.ChangeType(responseBody, typeof(T));
}
}
else
{
httpResult.Message = $"Call to {response.RequestMessage.RequestUri} failed with code {response.StatusCode}. Reason: {response.ReasonPhrase}";
}
return httpResult;
}
catch (Exception ex)
{
throw GetException(
$"[GetHttpResponseMessageFromHttpResponse] Error reading response from {response.RequestMessage.RequestUri} into {typeof(T).FullName}",
originalException: ex,
response.RequestMessage.RequestUri.ToString(),
requestBody,
typeof(T).FullName,
responseBody);
}
}
/// <summary>
/// Get Http Response Message From Exception
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="requestUri"></param>
/// <param name="ex"></param>
/// <param name="message"></param>
/// <param name="additionalContext"></param>
/// <returns></returns>
private static HttpResult<T> GetHttpResponseMessageFromException<T>(string requestUri, Exception ex, string message = "", string additionalContext = "")
{
var httpResult = new HttpResult<T>
{
IsSuccessful = false,
RequestUri = requestUri,
Exception = ex,
Message = message,
AdditionalContext = additionalContext,
Result = default(T),
StatusCode = HttpStatusCode.InternalServerError,
};
return httpResult;
}
/// <summary>
/// Get Exception of different parameters
/// </summary>
/// <param name="message"></param>
/// <param name="originalException"></param>
/// <param name="requestUri"></param>
/// <param name="requestBody"></param>
/// <param name="expectedResponseTypeName"></param>
/// <param name="responseBody"></param>
/// <returns></returns>
private static HttpException GetException(string message, Exception originalException, string requestUri, object requestBody, string expectedResponseTypeName, string responseBody = "")
{
string requestBodyString;
try
{
requestBodyString = JsonSerializerExtension.Serialize(requestBody);
}
catch
{
requestBodyString = "Cannot serialize requestBody";
}
return new HttpException(message, originalException)
{
RequestUri = requestUri,
RequestBody = requestBodyString,
ResponseBody = responseBody,
ExpectedResponseTypeName = expectedResponseTypeName
};
}
/// <summary>
/// Create Http Request
/// </summary>
/// <param name="method"></param>
/// <param name="url"></param>
/// <param name="headers"></param>
/// <param name="body"></param>
/// <returns></returns>
private static HttpRequestMessage CreateRequest(HttpMethod method, string url, Dictionary<string, string> headers, object body)
{
var request = new HttpRequestMessage
{
Method = method,
RequestUri = new Uri(url)
};
if (headers != null)
{
foreach (var kvp in headers)
{
request.Headers.Add(kvp.Key, kvp.Value);
}
}
if (body != null)
{
//var serializerSettings = new JsonSerializerOptions
//{
// //DefaultValueHandling = DefaultValueHandling.Ignore,
// //ContractResolver = new CamelCasePropertyNamesContractResolver()
//};
//var jsonBody = JsonSerializer.Serialize(body, serializerSettings);
var jsonBody = JsonSerializerExtension.Serialize(body);
request.Content = method.Method == "PATCH"
? new StringContent(jsonBody, Encoding.UTF8, "application/merge-patch+json")
: new StringContent(jsonBody, Encoding.UTF8, "application/json");
}
return request;
}
/// <summary>
/// Create Form Url Encoded Content Request
/// </summary>
/// <param name="method"></param>
/// <param name="url"></param>
/// <param name="headers"></param>
/// <param name="body"></param>
/// <returns></returns>
private static HttpRequestMessage CreateFormUrlEncodedContentRequest(HttpMethod method, string url, Dictionary<string, string> headers, List<KeyValuePair<string, string>> body)
{
var request = new HttpRequestMessage
{
Method = method,
RequestUri = new Uri(url)
};
if (headers != null)
{
foreach (var kvp in headers)
{
request.Headers.Add(kvp.Key, kvp.Value);
}
}
if (body != null)
{
request.Content = new FormUrlEncodedContent(body);
}
return request;
}
#endregion
}
}