-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCDSWebApiService.cs
840 lines (754 loc) · 30.6 KB
/
CDSWebApiService.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace WebAPISamplePrototype
{
public class CDSWebApiService : IDisposable
{
private readonly HttpClient httpClient;
private readonly CDSWebApiServiceConfig config;
/// <summary>
/// The BaseAddresss property of the HttpClient.
/// </summary>
public Uri BaseAddress { get { return httpClient.BaseAddress; } }
public CDSWebApiService(CDSWebApiServiceConfig config)
{
this.config = config;
HttpMessageHandler messageHandler = new OAuthMessageHandler(config,
new HttpClientHandler());
httpClient = new HttpClient(messageHandler)
{
BaseAddress = new Uri(config.Url + $"/api/data/v{config.Version}/")
};
httpClient.Timeout = TimeSpan.FromSeconds(config.TimeoutInSeconds);
httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
httpClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
if (config.CallerObjectId != Guid.Empty)
{
httpClient.DefaultRequestHeaders.Add("CallerObjectId", config.CallerObjectId.ToString());
}
}
/// <summary>
/// Creates an entity record.
/// </summary>
/// <param name="entitySetName">The entityset name for the entity.</param>
/// <param name="body">The JObject that contains the data to set for the entity.</param>
/// <returns>The Uri of the created record</returns>
public Uri PostCreate(string entitySetName, JObject body)
{
try
{
using (var message = new HttpRequestMessage(HttpMethod.Post, entitySetName))
{
message.Content = new StringContent(body.ToString());
message.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
using (HttpResponseMessage response = Send(message)) {
return new Uri(response.Headers.GetValues("OData-EntityId").FirstOrDefault());
}
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Posts data to a Uri
/// </summary>
/// <param name="uri">The Uri to post the data to</param>
/// <param name="body">The JObject containing the data to post.</param>
public JObject Post(string path, JObject body, Dictionary<string, List<string>> headers = null)
{
try
{
using (var message = new HttpRequestMessage(HttpMethod.Post, path))
{
message.Content = new StringContent(body.ToString());
message.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
using (HttpResponseMessage response = Send(message)) {
string content = response.Content.ReadAsStringAsync().Result;
if (string.IsNullOrEmpty(content))
{
return null;
}
return JObject.Parse(content);
}
}
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Retrieves data
/// </summary>
/// <param name="path">The location of the resources to return</param>
/// <param name="headers">Headers to set for special behaviors</param>
/// <returns></returns>
public JToken Get(string path, Dictionary<string, List<string>> headers = null)
{
try
{
using (var message = new HttpRequestMessage(HttpMethod.Get, path))
{
if (headers != null)
{
foreach (KeyValuePair<string, List<string>> header in headers)
{
message.Headers.Add(header.Key, header.Value);
}
}
using (HttpResponseMessage response = Send(message, HttpCompletionOption.ResponseContentRead)) {
if (response.StatusCode != HttpStatusCode.NotModified)
{
return JToken.Parse(response.Content.ReadAsStringAsync().Result);
}
return null;
}
}
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Sends a PATCH request
/// </summary>
/// <param name="uri">The Uri of the entity record to update</param>
/// <param name="body">The JObject containing the data to post.</param>
public void Patch(Uri uri, JObject body, Dictionary<string, List<string>> headers = null)
{
using (var message = new HttpRequestMessage(new HttpMethod("PATCH"), uri))
{
message.Content = new StringContent(body.ToString());
message.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
if (headers != null)
{
foreach (KeyValuePair<string, List<string>> header in headers)
{
message.Headers.Add(header.Key, header.Value);
}
}
Send(message).Dispose();
}
}
/// <summary>
/// Sends a Delete operation
/// </summary>
/// <param name="uri">The URI of the resource to delete</param>
public void Delete(Uri uri, Dictionary<string, List<string>> headers = null)
{
try
{
using (var message = new HttpRequestMessage(HttpMethod.Delete, uri))
{
if (headers != null)
{
foreach (KeyValuePair<string, List<string>> header in headers)
{
message.Headers.Add(header.Key, header.Value);
}
}
Send(message, HttpCompletionOption.ResponseHeadersRead).Dispose();
}
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Sends a PUT request to set data
/// </summary>
/// <param name="uri">The URI of the resource to update</param>
/// <param name="property">The name of the resource property to update.</param>
/// <param name="value">The value of the data to update</param>
public void Put(Uri uri, string property, string value)
{
try
{
using (var message = new HttpRequestMessage(HttpMethod.Put, $"{uri}/{property}"))
{
var body = new JObject
{
["value"] = value
};
message.Content = new StringContent(body.ToString());
message.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
Send(message, HttpCompletionOption.ResponseHeadersRead).Dispose();
}
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Sends a set of requests in a batch
/// </summary>
/// <param name="items">The items for each request.</param>
/// <returns>Responses for each request</returns>
public List<HttpResponseMessage> PostBatch(List<BatchItem> items)
{
//A generated guid for the unique identifier for the batch
var batchId = Guid.NewGuid();
//StringBuilder to create StringContent
var sb = new StringBuilder();
sb.AppendLine();
//Loop through the BatchItems
items.ForEach(x =>
{
//BatchChangeSet are treated differently from BatchGetRequests
switch (x)
{
case BatchChangeSet c:
sb.AppendLine($"--batch_{batchId}");
sb.AppendLine($"Content-Type: multipart/mixed;boundary=changeset_{c.Id}");
sb.AppendLine();
var contentid = 0;
//Add each of the individual requests in the changeset
c.Requests.ForEach(y =>
{
sb.AppendLine($"--changeset_{c.Id}");
sb.AppendLine("Content-Type: application/http");
sb.AppendLine("Content-Transfer-Encoding:binary");
sb.AppendLine($"Content-ID: {++contentid}");
sb.AppendLine();
sb.AppendLine($"{y.Method.ToString()} {y.RequestUri} HTTP/1.1");
sb.AppendLine("Content-Type: application/json;type=entry");
sb.AppendLine();
sb.AppendLine(y.Content.ReadAsStringAsync().Result);
});
//End the changeset
sb.AppendLine($"--changeset_{c.Id}--");
sb.AppendLine();
break;
case BatchGetRequest g:
sb.AppendLine($"--batch_{batchId}");
sb.AppendLine("Content-Type: application/http");
sb.AppendLine("Content-Transfer-Encoding:binary");
sb.AppendLine();
sb.AppendLine($"GET {g.Path} HTTP/1.1");
sb.AppendLine("Accept:application/json");
foreach (var header in g.Headers)
{
sb.AppendLine($"{header.Key}:{header.Value}");
}
break;
}
});
sb.AppendLine();
sb.AppendLine($"--batch_{batchId}--");
try
{
//A list of the responses that will be parsed from the Batch response
var responses = new List<HttpResponseMessage>();
using (var message = new HttpRequestMessage(HttpMethod.Post, "$batch"))
{
//Add the generated string content to the messge content
message.Content = new StringContent(sb.ToString());
//Set the message header
message.Content.Headers.ContentType = MediaTypeHeaderValue.Parse($"multipart/mixed;boundary=batch_{batchId}");
//Send the request
using (HttpResponseMessage response = Send(message)) {
//Get the content of the response
string body = response.Content.ReadAsStringAsync().Result;
//Get the batch responseid from the first line
string batchResponseId = new StringReader(body).ReadLine();
//Split the content using the batchResponseId
var batchResponses = body.Split(new string[] { batchResponseId }, StringSplitOptions.RemoveEmptyEntries).ToList();
if (batchResponses.Any())
{
batchResponses.RemoveAt(batchResponses.Count - 1); //remove last row with "--";
}
batchResponses.ForEach(x =>
{
var changeSetBoundary = string.Empty;
//Determine the changeset boundary
foreach (var line in x.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
{
if (line.StartsWith("Content-Type: multipart/mixed; boundary=changesetresponse_"))
{
changeSetBoundary = line.Split('=')[1];
break;
}
}
if (changeSetBoundary != string.Empty)
{
//Split the content using the changeset boundary
var changeSetText = x.Split(new string[] { $"--{changeSetBoundary}" }, StringSplitOptions.RemoveEmptyEntries).ToList();
if (changeSetText.Any())
{
changeSetText.RemoveAt(changeSetText.Count - 1); //remove last row with "--";
changeSetText.RemoveAt(0); //remove first row
}
changeSetText.ForEach(y =>
{
//Parse the responses for each changeset
responses.Add(ParseResponse(y));
});
}
else
{
//Parse the responses for each GET response
responses.Add(ParseResponse(x));
}
});
return responses;
}
}
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Generates a Response from the text returned by a batch item.
/// </summary>
/// <param name="responseText"></param>
/// <returns></returns>
private HttpResponseMessage ParseResponse(string responseText)
{
var response = new HttpResponseMessage();
//Parse each line in the text and extract useful information
//Not all elements are parsed. Some new ones may need to be added for different use cases.
foreach (var line in responseText.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
{
if (!string.IsNullOrEmpty(line))
{
//Get the StatusCode value
if (line.StartsWith("HTTP/1.1 "))
{
int start = "HTTP/1.1 ".Length;
var statusCode = int.Parse(line.Substring(start, 3));
response.StatusCode = (HttpStatusCode)statusCode;
}
//Get the URL of any records created
if (line.StartsWith("OData-EntityId: "))
{
int start = "OData-EntityId: ".Length;
response.Headers.Add("OData-EntityId", line.Substring(start));
}
//Get the ETag value
if (line.StartsWith("ETag: "))
{
int start = "ETag: ".Length;
response.Headers.Add("ETag", line.Substring(start));
}
//Get the JSON content
if (line.StartsWith("{")) {
response.Content = new StringContent(line.Trim());
}
}
}
return response;
}
/// <summary>
/// Sends the Http Request
/// </summary>
/// <param name="request">The request to send</param>
/// <param name="httpCompletionOption">The completion option</param>
/// <param name="retryCount">The current retry count</param>
/// <returns></returns>
private HttpResponseMessage Send(
HttpRequestMessage request,
HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseHeadersRead,
int retryCount = 0)
{
//Sending a copy of the request because if it fails the Content will be disposed and can't be sent again.
HttpResponseMessage response;
using (var requestCopy = request.Clone())
{
try
{
response = httpClient.SendAsync(requestCopy, httpCompletionOption).Result;
}
catch (Exception ex)
{
throw ex;
}
}
if (!response.IsSuccessStatusCode)
{
//NotModified is not considered an error for conditional operations
if (response.StatusCode == HttpStatusCode.NotModified)
{
return response;
}
if ((int)response.StatusCode != 429)
{
//Not a service protection limit error
throw ParseError(response);
}
else
{
// Give up re-trying if exceeding the maxRetries
if (++retryCount >= config.MaxRetries)
{
throw ParseError(response);
}
int seconds;
//Try to use the Retry-After header value if it is returned.
if (response.Headers.Contains("Retry-After"))
{
seconds = int.Parse(response.Headers.GetValues("Retry-After").FirstOrDefault());
Console.WriteLine($"Waiting for: {seconds} seconds based on Retry-After value.");
}
else
{
//Otherwise, use an exponential backoff strategy
seconds = (int)Math.Pow(2, retryCount);
Console.WriteLine($"Waiting for: {seconds} seconds based on exponential backoff.");
}
Thread.Sleep(TimeSpan.FromSeconds(seconds));
return Send(request, httpCompletionOption, retryCount);
}
}
else
{
return response;
}
}
private Exception ParseError(HttpResponseMessage response)
{
try
{
var errorObject = JObject.Parse(response.Content.ReadAsStringAsync().Result);
string message = errorObject["error"]["message"].Value<string>();
int code = Convert.ToInt32(errorObject["error"]["code"].Value<string>(), 16);
int statusCode = (int)response.StatusCode;
string reasonPhrase = response.ReasonPhrase;
return new CDSWebApiException(code, statusCode, reasonPhrase, message);
}
catch (Exception)
{
throw;
}
}
~CDSWebApiService()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing == true)
{
ReleaseClient();
}
else
{
}
ReleaseClient();
}
private void ReleaseClient()
{
if (httpClient != null)
{
httpClient.Dispose();
}
}
/// <summary>
///Custom HTTP message handler that uses OAuth authentication thru ADAL.
/// </summary>
private class OAuthMessageHandler : DelegatingHandler
{
private readonly CDSWebApiServiceConfig config;
private readonly UserPasswordCredential _credential = null;
private readonly AuthenticationContext _authContext;
public OAuthMessageHandler(CDSWebApiServiceConfig configParam,
HttpMessageHandler innerHandler)
: base(innerHandler)
{
config = configParam;
if (config.UserPrincipalName != null && config.Password != null)
{
_credential = new UserPasswordCredential(config.UserPrincipalName, config.Password);
}
_authContext = new AuthenticationContext(config.Authority, false);
}
private AuthenticationHeaderValue GetAuthHeader()
{
AuthenticationResult authResult;
if (_credential == null)
{
authResult = _authContext.AcquireTokenAsync(config.Url, config.ClientId, new Uri(config.RedirectUrl), new PlatformParameters(PromptBehavior.Auto)).Result;
}
else
{
authResult = _authContext.AcquireTokenAsync(config.Url, config.ClientId, _credential).Result;
}
return new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
}
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
try
{
request.Headers.Authorization = GetAuthHeader();
}
catch (Exception ex)
{
throw ex;
}
return base.SendAsync(request, cancellationToken);
}
}
}
public class CDSWebApiException : Exception
{
public int ErrorCode { get; private set; }
public int StatusCode { get; private set; }
public string ReasonPhrase { get; private set; }
public CDSWebApiException(int errorcode, int statuscode, string reasonphrase, string message) : base(message)
{
ErrorCode = errorcode;
StatusCode = statuscode;
ReasonPhrase = reasonphrase;
}
}
public class CDSWebApiServiceConfig
{
private static string connectionString;
private string authority = "https://login.microsoftonline.com/common";
private string url = null;
private string clientId = null;
private string redirectUrl = null;
public CDSWebApiServiceConfig(string connectionStringParam)
{
connectionString = connectionStringParam;
string authorityValue = GetParameterValue("Authority");
if (!string.IsNullOrEmpty(authorityValue))
{
Authority = authorityValue;
}
Url = GetParameterValue("Url");
ClientId = GetParameterValue("ClientId");
RedirectUrl = GetParameterValue("RedirectUrl");
string userPrincipalNameValue = GetParameterValue("UserPrincipalName");
if (!string.IsNullOrEmpty(userPrincipalNameValue))
{
UserPrincipalName = userPrincipalNameValue;
}
if (Guid.TryParse(GetParameterValue("CallerObjectId"), out Guid callerObjectId))
{
CallerObjectId = callerObjectId;
}
string versionValue = GetParameterValue("Version");
if (!string.IsNullOrEmpty(versionValue))
{
Version = versionValue;
}
if (byte.TryParse(GetParameterValue("MaxRetries"), out byte maxRetries))
{
MaxRetries = maxRetries;
}
if (ushort.TryParse(GetParameterValue("TimeoutInSeconds"), out ushort timeoutInSeconds))
{
TimeoutInSeconds = timeoutInSeconds;
}
string pwd = GetParameterValue("Password");
if (!string.IsNullOrEmpty(pwd))
{
var ss = new SecureString();
pwd.ToCharArray().ToList().ForEach(ss.AppendChar);
ss.MakeReadOnly();
Password = ss;
}
}
/// <summary>
/// The authority to use to authorize user.
/// Default is 'https://login.microsoftonline.com/common'
/// </summary>
public string Authority
{
get => authority; set
{
if (!string.IsNullOrEmpty(value))
{
authority = value;
}
else
{
throw new Exception("CDSWebApiServiceConfig.Authority value cannot be null.");
}
}
}
/// <summary>
/// The Url to the CDS environment, i.e "https://yourorg.api.crm.dynamics.com"
/// </summary>
public string Url
{
get => url; set
{
if (!string.IsNullOrEmpty(value))
{
url = value;
}
else
{
throw new Exception("CDSWebApiServiceConfig.Url value cannot be null.");
}
}
}
/// <summary>
/// The id of the application registered with Azure AD
/// </summary>
public string ClientId
{
get => clientId; set
{
if (!string.IsNullOrEmpty(value))
{
clientId = value;
}
else
{
throw new Exception("CDSWebApiServiceConfig.ClientId value cannot be null.");
}
}
}
/// <summary>
/// The Redirect Url of the application registered with Azure AD
/// </summary>
public string RedirectUrl
{
get => redirectUrl; set
{
if (!string.IsNullOrEmpty(value))
{
redirectUrl = value;
}
else
{
throw new Exception("CDSWebApiServiceConfig.RedirectUrl value cannot be null.");
}
}
}
/// <summary>
/// The user principal name of the user. i.e. [email protected]
/// </summary>
public string UserPrincipalName { get; set; } = null;
/// <summary>
/// The password for the user principal
/// </summary>
public SecureString Password { get; set; } = null;
/// <summary>
/// The Azure AD ObjectId for the user to impersonate other users.
/// </summary>
public Guid CallerObjectId { get; set; }
/// <summary>
/// The version of the Web API to use
/// Default is '9.1'
/// </summary>
public string Version { get; set; } = "9.1";
/// <summary>
/// The maximum number of attempts to retry a request blocked by service protection limits.
/// Default is 3.
/// </summary>
public byte MaxRetries { get; set; } = 3;
/// <summary>
/// The amount of time to try completing a request before it will be cancelled.
/// Default is 120 (2 minutes)
/// </summary>
public ushort TimeoutInSeconds { get; set; } = 120;
/// <summary>
/// Extracts a parameter value from a connection string
/// </summary>
/// <param name="parameter">The name of the parameter value</param>
/// <returns></returns>
private static string GetParameterValue(string parameter)
{
try
{
string value = connectionString
.Split(';')
.Where(s => s.Trim()
.StartsWith(parameter))
.FirstOrDefault()
.Split('=')[1];
if (value.ToLower() == "null")
{
return string.Empty;
}
return value;
}
catch (Exception)
{
return string.Empty;
}
}
}
/// <summary>
/// Contains extension methods to clone HttpRequestMessage and HttpContent types.
/// </summary>
public static class Extensions
{
/// <summary>
/// Clones a HttpRequestMessage instance
/// </summary>
/// <param name="request">The HttpRequestMessage to clone.</param>
/// <returns>A copy of the HttpRequestMessage</returns>
public static HttpRequestMessage Clone(this HttpRequestMessage request)
{
var clone = new HttpRequestMessage(request.Method, request.RequestUri)
{
Content = request.Content.Clone(),
Version = request.Version
};
foreach (KeyValuePair<string, object> prop in request.Properties)
{
clone.Properties.Add(prop);
}
foreach (KeyValuePair<string, IEnumerable<string>> header in request.Headers)
{
clone.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
return clone;
}
/// <summary>
/// Clones a HttpContent instance
/// </summary>
/// <param name="content">The HttpContent to clone</param>
/// <returns>A copy of the HttpContent</returns>
public static HttpContent Clone(this HttpContent content)
{
if (content == null) return null;
HttpContent clone;
switch (content)
{
case StringContent sc:
clone = new StringContent(sc.ReadAsStringAsync().Result);
break;
//TODO: Add support for other content types as needed.
default:
throw new Exception($"{content.GetType().ToString()} Content type not implemented for HttpContent.Clone extension method.");
}
clone.Headers.Clear();
foreach (KeyValuePair<string, IEnumerable<string>> header in content.Headers)
{
clone.Headers.Add(header.Key, header.Value);
}
return clone;
}
}
}