-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathForceClient.cs
More file actions
953 lines (797 loc) · 46.2 KB
/
ForceClient.cs
File metadata and controls
953 lines (797 loc) · 46.2 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
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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
using NetCoreForce.Client.Enumerations;
using NetCoreForce.Client.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace NetCoreForce.Client
{
public class ForceClient
{
public string ApiVersion { get; private set; }
public string InstanceUrl { get; private set; }
public string AccessToken { get; private set; }
public string ClientName { get; set; }
/// <summary>
/// The Access Token Response data received after a successful login
/// May not be available if the client was initialized with a pre-existing access token
/// </summary>
public AccessTokenResponse AccessInfo { get; set; }
private static readonly HttpClient _SharedHttpClient;
private HttpClient _httpClient;
private HttpClient SharedHttpClient
{
get
{
//use the instance client when extant, otherwise use the default shared instance.
return _httpClient ?? _SharedHttpClient;
}
}
static ForceClient()
{
_SharedHttpClient = HttpClientFactory.CreateHttpClient();
}
/// <summary>
/// Login to Salesforce using the username-password authentication flow, and initialize the client
/// </summary>
/// <param name="authInfo"></param>
public ForceClient(AuthInfo authInfo)
: this(authInfo.ClientId, authInfo.ClientSecret, authInfo.Username, authInfo.Password, authInfo.TokenRequestEndpoint, authInfo.ApiVersion)
{ }
/// <summary>
/// Login to Salesforce using the username-password authentication flow, and initialize the client
/// </summary>
/// <param name="clientId">Client ID, a.k.a. Consumer Key</param>
/// <param name="clientSecret">Client Secret, a.k.a. Consumer Secret</param>
/// <param name="username">Salesforce username</param>
/// <param name="password">Salesforce password</param>
/// <param name="tokenRequestEndpoint">Token request endpoint <para>e.g. https://login.salesforce.com/services/oauth2/token</para></param>
/// <param name="apiVersion">Salesforce API version</param>
/// <param name="httpClient">Optional HttpClient object. Defaults to a shared static instance for best performance, but a custom HttpClient can be specified when custom properties are needed e.g. proxy settings.</param>
public ForceClient(string clientId, string clientSecret, string username, string password, string tokenRequestEndpoint, string apiVersion = null, HttpClient httpClient = null)
{
try
{
Login(clientId, clientSecret, username, password, tokenRequestEndpoint, apiVersion, httpClient).Wait();
}
catch (AggregateException ax)
{
throw ax.InnerException;
}
}
/// <summary>
/// Initialize the client using previously obtained access token and instance url, if using the AuthenticationClient separately.
/// </summary>
/// <param name="instanceUrl">Identifies the Salesforce instance to which API calls should be sent.</param>
/// <param name="apiVersion">Salesforce API version</param>
/// <param name="accessToken">Access token</param>
/// <param name="httpClient">Optional HttpClient object. Defaults to a shared static instance for best performance, but a custom HttpClient can be specified when custom properties are needed e.g. proxy settings.</param>
/// <param name="accessInfo">AccessTokenResponse object, to store all of the OAuth details received via the AuthenticationClient</param>
public ForceClient(string instanceUrl, string apiVersion, string accessToken, HttpClient httpClient = null, AccessTokenResponse accessInfo = null)
{
Initialize(instanceUrl, apiVersion, accessToken, httpClient, accessInfo);
}
private async Task Login(string clientId, string clientSecret, string username, string password, string tokenRequestEndpoint, string apiVersion = null, HttpClient httpClient = null)
{
AuthenticationClient authClient = new AuthenticationClient(apiVersion, httpClient);
await authClient.UsernamePasswordAsync(clientId, clientSecret, username, password, tokenRequestEndpoint).ConfigureAwait(false);
Initialize(authClient.AccessInfo.InstanceUrl, authClient.ApiVersion, authClient.AccessInfo.AccessToken, httpClient, authClient.AccessInfo);
}
private void Initialize(string instanceUrl, string apiVersion, string accessToken, HttpClient httpClient = null, AccessTokenResponse accessInfo = null)
{
this.ApiVersion = apiVersion;
this.InstanceUrl = instanceUrl;
this.AccessToken = accessToken;
this.AccessInfo = accessInfo;
if (httpClient != null)
{
_httpClient = httpClient;
}
}
/// <summary>
/// Does a basic test of the client's connection to the current Salesforce instance, and that the API is responding to requests.
/// <para>This does not validate authentication.</para>
/// <para>Makes a call to the Versions resource, since it requires no authentication or permissions.</para>
/// </summary>
/// <param name="currentInstanceUrl">Instance URL. Defaults to the client's current instance, this would typically only need to be specified if it is needed to test the connection to a different SFDC instance.</param>
/// <returns>True or false. Does not throw exceptions, only false in case of any errors.</returns>
public bool TestConnection(string currentInstanceUrl = null)
{
try
{
//Makes a call to the Versions resource, since it requires no authentication or permissions.
//Should serve as an adequate test that the instance is physically reachable and that the API is responding
GetAvailableRestApiVersions(currentInstanceUrl, deserializeResponse: false).Wait();
return true;
}
catch (Exception ex)
{
Debug.WriteLine("TestConnection() failed with exception: " + ex.Message);
return false;
}
}
/// <summary>
/// Retrieve records using a SOQL query.
/// <para>Will automatically retrieve the complete result set if split into batches. If you want to limit results, use the LIMIT operator in your query.</para>
/// </summary>
/// <param name="queryString">SOQL query string, without any URL escaping/encoding</param>
/// <param name="queryAll">True if deleted records are to be included</param>
/// <returns>List{T} of results</returns>
public async Task<List<T>> Query<T>(string queryString, bool queryAll = false)
{
#if DEBUG
Stopwatch sw = new Stopwatch();
sw.Start();
#endif
try
{
Dictionary<string, string> headers = HeaderFormatter.SforceCallOptions(ClientName);
var queryUri = UriFormatter.Query(InstanceUrl, ApiVersion, queryString, queryAll);
JsonClient client = new JsonClient(AccessToken, SharedHttpClient);
List<T> results = new List<T>();
bool done = false;
string nextRecordsUrl = string.Empty;
//larger result sets will be split into batches (sized according to system and account settings)
//if additional batches are indicated retrieve the rest and append to the result set.
do
{
if (!string.IsNullOrEmpty(nextRecordsUrl))
{
queryUri = new Uri(new Uri(InstanceUrl), nextRecordsUrl);
}
QueryResult<T> qr = await client.HttpGetAsync<QueryResult<T>>(queryUri, headers).ConfigureAwait(false);
#if DEBUG
Debug.WriteLine(string.Format("Got query resuts, {0} totalSize, {1} in this batch, final batch: {2}",
qr.TotalSize, qr.Records.Count.ToString(), qr.Done.ToString()));
#endif
results.AddRange(qr.Records);
done = qr.Done;
nextRecordsUrl = qr.NextRecordsUrl;
if (!done && string.IsNullOrEmpty(nextRecordsUrl))
{
//Normally if query has remaining batches, NextRecordsUrl will have a value, and Done will be false.
//In case of some unforseen error, flag the result as done if we're missing the NextRecordsUrl
//In this situation we'll just get the previous set again and be stuck in a loop.
done = true;
}
} while (!done);
#if DEBUG
sw.Stop();
Debug.WriteLine(string.Format("Query results retrieved in {0}ms", sw.ElapsedMilliseconds.ToString()));
#endif
return results;
}
catch (Exception ex)
{
Debug.WriteLine("Error querying: " + ex.Message);
throw;
}
}
/// <summary>
/// Retrieve a single record using a SOQL query.
/// <para>Will throw an exception if multiple rows are retrieved by the query - if you are note sure of a single result, use Query{T} instead.</para>
/// </summary>
/// <param name="queryString">SOQL query string, without any URL escaping/encoding</param>
/// <param name="queryAll">True if deleted records are to be included</param>
/// <returns>result object</returns>
public async Task<T> QuerySingle<T>(string queryString, bool queryAll = false)
{
List<T> results = await Query<T>(queryString, queryAll).ConfigureAwait(false);
if (results != null && results.Count > 1)
{
throw new Exception("Multiple records were returned by query passed into QuerySingle - query must retrieve zero or one record.");
}
if (results != null && results.Count == 1)
{
return results[0];
}
return default(T);
}
/// <summary>
/// Retrieve a <see cref="IAsyncEnumerable{T}"/> using a SOQL query. Batches will be retrieved asynchronously.
/// <para>When using the iterator, the initial result batch will be returned as soon as it is received. The additional result batches will be retrieved only as needed.</para>
/// </summary>
/// <param name="queryString">SOQL query string, without any URL escaping/encoding</param>
/// <param name="queryAll">Optional. True if deleted records are to be included.await Defaults to false.</param>
/// <param name="batchSize">Optional. Size of result batches between 200 and 2000</param>
/// <param name="cancellationToken">Optional. Cancellation token</param>
/// <returns><see cref="IAsyncEnumerable{T}"/> of results</returns>
public async IAsyncEnumerable<T> QueryAsync<T>(string queryString, bool queryAll = false, int? batchSize = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
Dictionary<string, string> headers = new Dictionary<string, string>();
// Add call options
Dictionary<string, string> callOptions = HeaderFormatter.SforceCallOptions(ClientName);
headers.AddRange(callOptions);
// Add query options headers if batch size specified
if (batchSize.HasValue)
{
Dictionary<string, string> queryOptions = HeaderFormatter.SforceQueryOptions(batchSize.Value);
headers.AddRange(queryOptions);
}
var jsonClient = new JsonClient(AccessToken, SharedHttpClient);
var nextRecordsUri = UriFormatter.Query(InstanceUrl, ApiVersion, queryString, queryAll);
bool hasMoreRecords = true;
while (hasMoreRecords)
{
var qr = await jsonClient.HttpGetAsync<QueryResult<T>>(nextRecordsUri, headers).ConfigureAwait(false);
#if DEBUG
Debug.WriteLine($"Got query resuts, {qr.TotalSize} totalSize, {qr.Records.Count} in this batch, final batch: {qr.Done}");
#endif
if (!string.IsNullOrEmpty(qr.NextRecordsUrl))
{
nextRecordsUri = new Uri(new Uri(InstanceUrl), qr.NextRecordsUrl);
hasMoreRecords = true;
}
else
{
// Normally if a query has remaining batches, NextRecordsUrl will have a value, and Done will be false.
// In case of some unforseen error, consider the results done if we're missing the NextRecordsURL
hasMoreRecords = false;
}
foreach (T record in qr.Records)
{
yield return record;
}
}
}
/// <summary>
/// Get a basic SOQL COUNT() query result
/// <para>The query must start with SELECT COUNT() FROM, with no named field in the count clause. COUNT() must be the only element in the SELECT list.</para>
/// </summary>
/// <param name="queryString">SOQL query string starting with SELECT COUNT() FROM</param>
/// <param name="queryAll">True if deleted records are to be included</param>
/// <returns>The <see cref="Task{Int}"/> returning the count</returns>
public async Task<int> CountQuery(string queryString, bool queryAll = false)
{
// https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_count.htm
// COUNT() must be the only element in the SELECT list.
Dictionary<string, string> headers = HeaderFormatter.SforceCallOptions(ClientName);
if (!queryString.Replace(" ", "").ToLower().StartsWith("selectcount()from"))
{
throw new ForceApiException("CountQueryAsync may only be used with a query starting with SELECT COUNT() FROM");
}
var jsonClient = new JsonClient(AccessToken, SharedHttpClient);
var uri = UriFormatter.Query(InstanceUrl, ApiVersion, queryString);
var qr = await jsonClient.HttpGetAsync<QueryResult<object>>(uri, headers).ConfigureAwait(false);
return qr.TotalSize;
}
/// <summary>
/// Executes a SOSL search, returning a type T, e.g. when using "RETURNING Account" in the SOSL query.
/// <para>Not properly matching the return type T and the RETURNING clause of the SOSL query may return unexpected results</para>
/// </summary>
/// <param name="searchString"></param>
/// <returns>SearchResult{T}</returns>
public async Task<SearchResult<T>> Search<T>(string searchString)
{
try
{
Dictionary<string, string> headers = HeaderFormatter.SforceCallOptions(ClientName);
var uri = UriFormatter.Search(InstanceUrl, ApiVersion, searchString);
JsonClient client = new JsonClient(AccessToken, SharedHttpClient);
SearchResult<T> searchResult = await client.HttpGetAsync<SearchResult<T>>(uri, headers).ConfigureAwait(false);
return searchResult;
}
catch (Exception ex)
{
Debug.WriteLine("Error searching: " + ex.Message);
throw;
}
}
/// <summary>
/// Executes a SOSL search, returning a simple generic object in the results collection that primarly results in a list of object IDs
/// </summary>
/// <param name="searchString"></param>
/// <returns>SearchResult{SObjectGeneric}</returns>
public async Task<SearchResult<SObjectGeneric>> Search(string searchString)
{
try
{
Dictionary<string, string> headers = HeaderFormatter.SforceCallOptions(ClientName);
var uri = UriFormatter.Search(InstanceUrl, ApiVersion, searchString);
JsonClient client = new JsonClient(AccessToken, SharedHttpClient);
SearchResult<SObjectGeneric> searchResult = await client.HttpGetAsync<SearchResult<SObjectGeneric>>(uri, headers).ConfigureAwait(false);
return searchResult;
}
catch (Exception ex)
{
Debug.WriteLine("Error searching: " + ex.Message);
throw;
}
}
/// <summary>
/// Get SObject by ID
/// </summary>
/// <param name="sObjectTypeName">SObject name, e.g. "Account"</param>
/// <param name="objectId">SObject ID</param>
/// <param name="fields">(optional) List of fields to retrieve, if not supplied, all fields are retrieved.</param>
public async Task<T> GetObjectById<T>(string sObjectTypeName, string objectId, List<string> fields = null)
{
Dictionary<string, string> headers = HeaderFormatter.SforceCallOptions(ClientName);
var uri = UriFormatter.SObjectRows(InstanceUrl, ApiVersion, sObjectTypeName, objectId, fields);
JsonClient client = new JsonClient(AccessToken, SharedHttpClient);
return await client.HttpGetAsync<T>(uri, headers).ConfigureAwait(false);
}
/// <summary>
/// Create a new record
/// </summary>
/// <param name="sObjectTypeName">SObject name, e.g. "Account"</param>
/// <param name="sObject">Object to create</param>
/// <param name="customHeaders">Custom headers to include in request (Optional). await The HeaderFormatter helper class can be used to generate the custom header as needed.</param>
/// <param name="fieldsToNull">A list of properties that should be set to null, but inclusing the null values in the serialized output</param>
/// <param name="ignoreNulls">Use with caution. By default null values are not serialized, this will serialize all explicitly nulled or missing properties as null</param>
/// <returns>CreateResponse object, includes new object's ID</returns>
/// <exception cref="ForceApiException">Thrown when creation fails</exception>
public async Task<CreateResponse> CreateRecord<T>(
string sObjectTypeName,
T sObject,
Dictionary<string, string> customHeaders = null,
List<string> fieldsToNull = null,
bool ignoreNulls = true)
{
Dictionary<string, string> headers = new Dictionary<string, string>();
//Add call options
Dictionary<string, string> callOptions = HeaderFormatter.SforceCallOptions(ClientName);
headers.AddRange(callOptions);
//Add custom headers if specified
if (customHeaders != null)
{
headers.AddRange(customHeaders);
}
var uri = UriFormatter.SObjectBasicInformation(InstanceUrl, ApiVersion, sObjectTypeName);
JsonClient client = new JsonClient(AccessToken, SharedHttpClient);
return await client.HttpPostAsync<CreateResponse>(sObject, uri, headers, fieldsToNull: fieldsToNull, ignoreNulls: ignoreNulls).ConfigureAwait(false);
}
/// <summary>
/// Create mlutiple records
/// </summary>
/// <param name="sObjectTypeName">SObject name, e.g. "Account"</param>
/// <param name="sObjects">Objects to create. Each sObject must have the entity type and reference id in the attributes property object.</param>
/// <param name="customHeaders">Custom headers to include in request (Optional). await The HeaderFormatter helper class can be used to generate the custom header as needed.</param>
/// <param name="autoFillAttributes">Automatically create attribute object property, reference Id will be the zero-based index of the array</param>
/// <param name="fieldsToNull">A list of properties that should be set to null, but inclusing the null values in the serialized output</param>
/// <param name="ignoreNulls">Use with caution. By default null values are not serialized, this will serialize all explicitly nulled or missing properties as null</param>
/// <returns>SObjectTreeResponse object, includes new object IDs, and errors if any</returns>
/// <exception cref="ForceApiException">Thrown when creation fails</exception>
public async Task<SObjectTreeResponse> CreateMultiple(
string sObjectTypeName,
List<SObject> sObjects,
bool autoFillAttributes = true,
Dictionary<string, string> customHeaders = null,
List<string> fieldsToNull = null,
bool ignoreNulls = true)
{
if (sObjects == null)
{
throw new ArgumentNullException("sObjects");
}
if (sObjects.Count > 200)
{
throw new ForceApiException($"A maximum of 200 records can be created in a single request - request included {sObjects.Count} records.");
}
if (autoFillAttributes)
{
for (int i = 0; i < sObjects.Count; i++)
{
sObjects[i].Attributes = new SObjectAttributes()
{
ReferenceId = i.ToString(),
Type = sObjectTypeName
};
}
}
else
{
foreach (SObject obj in sObjects)
{
if (obj.Attributes == null || string.IsNullOrEmpty(obj.Attributes.ReferenceId) || string.IsNullOrEmpty(obj.Attributes.Type))
{
throw new ArgumentException("All objects in request must include a reference id and sObject type name in the attributes");
}
}
}
Dictionary<string, string> headers = new Dictionary<string, string>();
//Add call options
Dictionary<string, string> callOptions = HeaderFormatter.SforceCallOptions(ClientName);
headers.AddRange(callOptions);
//Add custom headers if specified
if (customHeaders != null)
{
headers.AddRange(customHeaders);
}
var uri = UriFormatter.SObjectTree(InstanceUrl, ApiVersion, sObjectTypeName);
JsonClient client = new JsonClient(AccessToken, SharedHttpClient);
SObjectTreeRequest treeRequest = new SObjectTreeRequest(sObjects);
return await client.HttpPostAsync<SObjectTreeResponse>(treeRequest, uri, headers, fieldsToNull: fieldsToNull, ignoreNulls: ignoreNulls).ConfigureAwait(false);
}
/// <summary>
/// Update single record
/// </summary>
/// <param name="sObjectTypeName">SObject name, e.g. "Account"</param>
/// <param name="objectId">Id of Object to update</param>
/// <param name="sObject">Object to update</param>
/// <param name="customHeaders">Custom headers to include in request (Optional). await The HeaderFormatter helper class can be used to generate the custom header as needed.</param>
/// <param name="fieldsToNull">A list of properties that should be set to null, but inclusing the null values in the serialized output</param>
/// <param name="ignoreNulls">Use with caution. By default null values are not serialized, this will serialize all explicitly nulled or missing properties as null</param>
/// <typeparam name="T"></typeparam>
/// <returns>void, API returns 204/NoContent</returns>
/// <exception cref="ForceApiException">Thrown when update fails</exception>
public async Task UpdateRecord<T>(
string sObjectTypeName,
string objectId,
T sObject,
Dictionary<string, string> customHeaders = null,
List<string> fieldsToNull = null,
bool ignoreNulls = true)
{
Dictionary<string, string> headers = new Dictionary<string, string>();
//Add call options
Dictionary<string, string> callOptions = HeaderFormatter.SforceCallOptions(ClientName);
headers.AddRange(callOptions);
//Add custom headers if specified
if (customHeaders != null)
{
headers.AddRange(customHeaders);
}
var uri = UriFormatter.SObjectRows(InstanceUrl, ApiVersion, sObjectTypeName, objectId);
JsonClient client = new JsonClient(AccessToken, SharedHttpClient);
await client.HttpPatchAsync<object>(sObject, uri, headers, ignoreNulls: ignoreNulls, fieldsToNull: fieldsToNull).ConfigureAwait(false);
return;
}
/// <summary>
/// Update multiple records.
/// The list can contain up to 200 objects.
/// The list can contain objects of different types, including custom objects.
/// Each object must contain an attributes map. The map must contain a value for type.
/// Each object must contain an id field with a valid ID value.
/// </summary>
/// <param name="sObjects">Objects to update</param>
/// <param name="allOrNone">Optional. Indicates whether to roll back the entire request when the update of any object fails (true) or to continue with the independent update of other objects in the request. The default is false.</param>
/// <param name="customHeaders">Custom headers to include in request (Optional). await The HeaderFormatter helper class can be used to generate the custom header as needed.</param>
/// <param name="fieldsToNull">A list of properties that should be set to null, but inclusing the null values in the serialized output</param>
/// <param name="ignoreNulls">Use with caution. By default null values are not serialized, this will serialize all explicitly nulled or missing properties as null</param>
/// <returns>List of UpsertResponse objects, includes response for each object (id, success, errors)</returns>
/// <exception cref="ArgumentException">Thrown when missing required information</exception>
/// <exception cref="ForceApiException">Thrown when update fails</exception>
public async Task<List<UpsertResponse>> UpdateRecords(
List<SObject> sObjects,
bool allOrNone = false,
Dictionary<string, string> customHeaders = null,
List<string> fieldsToNull = null,
bool ignoreNulls = true)
{
if (sObjects == null)
{
throw new ArgumentNullException("sObjects");
}
foreach (SObject sObject in sObjects)
{
if (sObject.Attributes == null || string.IsNullOrEmpty(sObject.Attributes.Type))
{
throw new ForceApiException("Objects are missing Type property in Attributes map");
}
}
Dictionary<string, string> headers = new Dictionary<string, string>();
//Add call options
Dictionary<string, string> callOptions = HeaderFormatter.SforceCallOptions(ClientName);
headers.AddRange(callOptions);
//Add custom headers if specified
if (customHeaders != null)
{
headers.AddRange(customHeaders);
}
var uri = UriFormatter.SObjectsComposite(InstanceUrl, ApiVersion);
JsonClient client = new JsonClient(AccessToken, SharedHttpClient);
UpsertRequest upsertRequest = new UpsertRequest(sObjects, allOrNone);
return await client.HttpPatchAsync<List<UpsertResponse>>(upsertRequest, uri, headers, includeSObjectId: true, fieldsToNull: fieldsToNull, ignoreNulls: ignoreNulls).ConfigureAwait(false);
}
/// <summary>
/// Inserts or Updates a records based on external id
/// </summary>
/// <param name="sObjectTypeName">SObject name, e.g. "Account"</param>
/// <param name="fieldName">External ID field name</param>
/// <param name="fieldValue">External ID field value</param>
/// <param name="sObject">Object to update</param>
/// <param name="customHeaders">Custom headers to include in request (Optional). await The HeaderFormatter helper class can be used to generate the custom header as needed.</param>
/// <param name="fieldsToNull">A list of properties that should be set to null, but inclusing the null values in the serialized output</param>
/// <param name="ignoreNulls">Use with caution. By default null values are not serialized, this will serialize all explicitly nulled or missing properties as null</param>
/// <returns>UpsertResponse object, includes new object's ID if record was created and no value if object was updated</returns>
/// <exception cref="ForceApiException">Thrown when request fails</exception>
public async Task<UpsertResponse> InsertOrUpdateRecord<T>(
string sObjectTypeName,
string fieldName,
string fieldValue,
T sObject,
Dictionary<string, string> customHeaders = null,
List<string> fieldsToNull = null,
bool ignoreNulls = true)
{
Dictionary<string, string> headers = new Dictionary<string, string>();
//Add call options
Dictionary<string, string> callOptions = HeaderFormatter.SforceCallOptions(ClientName);
headers.AddRange(callOptions);
//Add custom headers if specified
if (customHeaders != null)
{
headers.AddRange(customHeaders);
}
var uri = UriFormatter.SObjectRowsByExternalId(InstanceUrl, ApiVersion, sObjectTypeName, fieldName, fieldValue);
JsonClient client = new JsonClient(AccessToken, SharedHttpClient);
return await client.HttpPatchAsync<UpsertResponse>(sObject, uri, headers, fieldsToNull: fieldsToNull, ignoreNulls: ignoreNulls).ConfigureAwait(false);
}
/// <summary>
/// Delete record
/// </summary>
/// <param name="sObjectTypeName">SObject name, e.g. "Account"</param>
/// <param name="objectId">Id of Object to update</param>
/// <returns>void, API returns 204/NoContent</returns>
/// <exception cref="ForceApiException">Thrown when update fails</exception>
public async Task DeleteRecord(string sObjectTypeName, string objectId)
{
Dictionary<string, string> headers = HeaderFormatter.SforceCallOptions(ClientName);
var uri = UriFormatter.SObjectRows(InstanceUrl, ApiVersion, sObjectTypeName, objectId);
JsonClient client = new JsonClient(AccessToken, SharedHttpClient);
await client.HttpDeleteAsync<object>(uri, headers).ConfigureAwait(false);
return;
}
/// <summary>
/// Execute multiple composite records.
/// The list can contain up to 200 objects.
/// The list can contain objects of different types, including custom objects.
/// Each object must contain an attributes map. The map must contain a value for type.
/// </summary>
/// <param name="sObjects">Objects to update</param>
/// <param name="allOrNone">Optional. Indicates whether to roll back the entire request when the update of any object fails (true) or to continue with the independent update of other objects in the request. The default is false.</param>
/// <param name="collateSubrequests">Optional. Controls whether the API collates unrelated subrequests to bulkify them (true) or not (false). When subrequests are collated, the processing speed is faster, but the order of execution is not guaranteed (unless there is an explicit dependency between the subrequests).If collation is disabled, then the subrequests are executed in the order in which they are received. The default is true.</param>
/// <param name="customHeaders">Custom headers to include in request (Optional). await The HeaderFormatter helper class can be used to generate the custom header as needed.</param>
/// <returns>List of UpdateMultipleResponse objects, includes response for each object (id, success, errors)</returns>
/// <exception cref="ArgumentException">Thrown when missing required information</exception>
/// <exception cref="ForceApiException">Thrown when update fails</exception>
public async Task<CompositeRequestResponse> ExecuteCompositeRecords(
List<CompositeSObject> sObjects,
bool allOrNone = false,
bool collateSubrequests = true,
Dictionary<string, string> customHeaders = null)
{
if (sObjects == null)
{
throw new ArgumentNullException("sObjects");
}
foreach (CompositeSObject s in sObjects)
{
if (s == null || (string.IsNullOrEmpty(s.Type) && s.CompositeType == CompositeType.SObject))
{
throw new ForceApiException("Objects are missing Type property in Attributes map");
}
}
Dictionary<string, string> headers = new Dictionary<string, string>();
//Add call options
Dictionary<string, string> callOptions = HeaderFormatter.SforceCallOptions(ClientName);
headers.AddRange(callOptions);
//Add custom headers if specified
if (customHeaders != null)
{
headers.AddRange(customHeaders);
}
var uri = UriFormatter.CompositeRequest(InstanceUrl, ApiVersion);
JsonClient client = new JsonClient(AccessToken, _httpClient);
List<CompositeSubRequest> subRequests = sObjects.Select(s =>
{
return new CompositeSubRequest(
s.SObject,
s.Method == CompositeMethod.Write ? string.IsNullOrWhiteSpace(s.Id) ? "POST" : "PATCH" : s.Method == CompositeMethod.Delete ? "DELETE" : "GET",
s.ReferenceId,
s.CompositeType == CompositeType.SObject ? UriFormatter.CompositeSubRequest(ApiVersion, s.Type, s.Id) : UriFormatter.CompositeSObjectCollectionsSubRequest(ApiVersion)
);
}).ToList();
CompositeRequest createMultipleRequest = new CompositeRequest(subRequests, allOrNone, collateSubrequests);
return await client.HttpPostAsync<CompositeRequestResponse>(createMultipleRequest, uri, headers);
}
/// <summary>
/// Execute request against ApexRest custom endpoints.
/// </summary>
/// <param name="apexResourceUrl">The URL of the apex resource. Ex: /services/apexrest/DuplicateCheck should provide "DuplicateCheck"</param>
/// <param name="request">The custom object to include in the request</param>
/// <param name="customHeaders">Custom headers to include in request (Optional). await The HeaderFormatter helper class can be used to generate the custom header as needed.</param>
/// <returns>List of UpdateMultipleResponse objects, includes response for each object (id, success, errors)</returns>
/// <exception cref="ArgumentException">Thrown when missing required information</exception>
/// <exception cref="ForceApiException">Thrown when update fails</exception>
public async Task<T> ExecuteApexPost<Request, T>(string apexResourceUrl, Request request, Dictionary<string, string> customHeaders = null)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (string.IsNullOrWhiteSpace(apexResourceUrl))
{
throw new ArgumentNullException(nameof(apexResourceUrl));
}
Dictionary<string, string> headers = new Dictionary<string, string>();
//Add call options
Dictionary<string, string> callOptions = HeaderFormatter.SforceCallOptions(ClientName);
headers.AddRange(callOptions);
//Add custom headers if specified
if (customHeaders != null)
{
headers.AddRange(customHeaders);
}
var uri = UriFormatter.ApexUri(InstanceUrl, apexResourceUrl);
JsonClient client = new JsonClient(AccessToken, _httpClient);
return await client.HttpPostAsync<T>(request, uri, headers);
}
const string blobUrlRegexString = @"^.+sobjects\/(\w+)\/(\w+)\/(\w+)$";
/// <summary>
/// Retrieve blob data at the specified URL.
/// Assumes a relative URL - If a full URL is used, the instance portion will be ignored.
/// </summary>
/// <param name="blobUrl">relative blob URL</param>
/// <returns>binary content stream</returns>
public async Task<Stream> BlobRetrieveStream(string blobUrl)
{
Regex regex = new Regex(blobUrlRegexString);
Match match = regex.Match(blobUrl);
if (!match.Success)
{
throw new ForceApiException("Unable to parse blob URL");
}
string sObjectTypeName = match.Groups[1].Value;
string objectId = match.Groups[2].Value;
string blobField = match.Groups[3].Value;
return await BlobRetrieveStream(sObjectTypeName, objectId, blobField).ConfigureAwait(false);
}
public async Task<Stream> BlobRetrieveStream(string sObjectTypeName, string objectId, string blobField)
{
HttpResponseMessage response = await BlobRetrieveResponse(sObjectTypeName, objectId, blobField).ConfigureAwait(false);
return await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
}
// public async Task<byte[]> BlobRetrieveBytes(string blobUrl)
// {
// Regex regex = new Regex(blobUrlRegexString);
// Match match = regex.Match(blobUrl);
// if (!match.Success)
// {
// throw new ForceApiException("Unable to parse blob URL");
// }
// string sObjectTypeName = match.Groups[1].Value;
// string objectId = match.Groups[2].Value;
// string blobField = match.Groups[3].Value;
// return await BlobRetrieveBytes(sObjectTypeName, objectId, blobField);
// }
// public async Task<byte[]> BlobRetrieveBytes(string sObjectTypeName, string objectId, string blobField)
// {
// HttpResponseMessage response = await BlobRetrieveResponse(sObjectTypeName, objectId, blobField);
// return await response.Content.ReadAsByteArrayAsync();
// }
private async Task<HttpResponseMessage> BlobRetrieveResponse(string sObjectTypeName, string objectId, string blobField)
{
HttpResponseMessage responseMessage = null;
try
{
var uri = UriFormatter.SObjectBlobRetrieve(InstanceUrl, ApiVersion, sObjectTypeName, objectId, blobField);
var authHeaderValue = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", AccessToken);
HttpRequestMessage request = new HttpRequestMessage();
request.Headers.Authorization = authHeaderValue;
request.RequestUri = uri;
request.Method = HttpMethod.Get;
//ResponseHeadersRead = do not buffer binary content immediately to memory
HttpCompletionOption completionOption = HttpCompletionOption.ResponseHeadersRead;
responseMessage = await SharedHttpClient.SendAsync(request, completionOption).ConfigureAwait(false);
}
catch (Exception ex)
{
string errMsg = "Error retrieving blob data:" + ex.Message;
if (ex.InnerException != null && !string.IsNullOrEmpty(ex.InnerException.Message))
{
errMsg += " " + ex.InnerException.Message;
}
throw new ForceApiException(errMsg);
}
if (responseMessage.IsSuccessStatusCode)
return responseMessage;
throw new ForceApiException($"Failed to download blob data, request returned {responseMessage.StatusCode} {responseMessage.ReasonPhrase}");
}
#region metadata
/// <summary>
/// Lists information about limits in your org.
/// <para>This resource is available in REST API version 29.0 and later for API users with the View Setup and Configuration permission.</para>
/// </summary>
public async Task<OrganizationLimits> GetOrganizationLimits()
{
var uri = UriFormatter.Limits(InstanceUrl, ApiVersion);
JsonClient client = new JsonClient(AccessToken, SharedHttpClient);
return await client.HttpGetAsync<OrganizationLimits>(uri).ConfigureAwait(false);
}
/// <summary>
/// List summary information about each REST API version currently available, including the version, label, and a link to each version's root.
/// You do not need authentication to retrieve the list of versions.
/// </summary>
/// <param name="currentInstanceUrl">Current instance URL. If the client has been initialized, the parameter is optional and the client's current instance URL will be used</param>
/// <returns>List of SalesforceVersion objects</returns>
public async Task<List<SalesforceVersion>> GetAvailableRestApiVersions(string currentInstanceUrl = null)
{
return await GetAvailableRestApiVersions(currentInstanceUrl, true).ConfigureAwait(false);
}
private async Task<List<SalesforceVersion>> GetAvailableRestApiVersions(string currentInstanceUrl = null, bool deserializeResponse = true)
{
if (string.IsNullOrEmpty(currentInstanceUrl))
{
if (string.IsNullOrEmpty(this.InstanceUrl))
{
throw new ForceApiException("currentInstanceUrl must be specified since the client's instance URL has not been initialized.");
}
currentInstanceUrl = this.InstanceUrl;
}
if (!Uri.IsWellFormedUriString(currentInstanceUrl, UriKind.Absolute))
{
throw new ForceApiException("Specified currentInstanceUrl is not a well formed URI");
}
var uri = UriFormatter.Versions(currentInstanceUrl);
JsonClient client = new JsonClient(AccessToken, SharedHttpClient);
return await client.HttpGetAsync<List<SalesforceVersion>>(uri: uri, deserializeResponse: deserializeResponse).ConfigureAwait(false);
}
/// <summary>
/// Get current user's info via Identity URL
/// <para>https://developer.salesforce.com/docs/atlas.en-us.mobile_sdk.meta/mobile_sdk/oauth_using_identity_urls.htm</para>
/// </summary>
/// <param name="identityUrl"></param>
/// <returns>UserInfo</returns>
public async Task<UserInfo> GetUserInfo(string identityUrl)
{
JsonClient client = new JsonClient(AccessToken, SharedHttpClient);
return await client.HttpGetAsync<UserInfo>(new Uri(identityUrl)).ConfigureAwait(false);
}
/// <summary>
/// Retrieve (basic) metadata for an object.
/// <para>Use the SObject Basic Information resource to retrieve metadata for an object.</para>
/// </summary>
/// <param name="objectTypeName">SObject name, e.g. Account</param>
/// <returns>Returns SObjectMetadataBasic with basic object metadata and a list of recently created items.</returns>
public async Task<SObjectBasicInfo> GetObjectBasicInfo(string objectTypeName)
{
Dictionary<string, string> headers = HeaderFormatter.SforceCallOptions(ClientName);
var uri = UriFormatter.SObjectBasicInformation(InstanceUrl, ApiVersion, objectTypeName);
JsonClient client = new JsonClient(AccessToken, SharedHttpClient);
return await client.HttpGetAsync<SObjectBasicInfo>(uri, headers).ConfigureAwait(false);
}
/// <summary>
/// Get field and other metadata for an object.
/// <para>Use the SObject Describe resource to retrieve all the metadata for an object, including information about each field, URLs, and child relationships.</para>
/// </summary>
/// <param name="objectTypeName">SObject name, e.g. Account</param>
/// <returns>Returns SObjectMetadataAll with full object meta including field metadata</returns>
public async Task<SObjectDescribeFull> GetObjectDescribe(string objectTypeName)
{
var uri = UriFormatter.SObjectDescribe(InstanceUrl, ApiVersion, objectTypeName);
JsonClient client = new JsonClient(AccessToken, SharedHttpClient);
return await client.HttpGetAsync<SObjectDescribeFull>(uri).ConfigureAwait(false);
}
/// <summary>
/// Get a List of Objects
/// <para>Use the Describe Global resource to list the objects available in your org and available to the logged-in user. This resource also returns the org encoding, as well as maximum batch size permitted in queries.</para>
/// </summary>
/// <returns>Returns DescribeGlobal object with a SObjectDescribe collection</returns>
public async Task<DescribeGlobal> DescribeGlobal()
{
var uri = UriFormatter.DescribeGlobal(InstanceUrl, ApiVersion);
JsonClient client = new JsonClient(AccessToken, SharedHttpClient);
return await client.HttpGetAsync<DescribeGlobal>(uri).ConfigureAwait(false);
}
#endregion
/// <summary>
/// Dispose client - only disposes instance HttpClient, if any. Shared static HttpClient is left as-is.
/// </summary>
public void Dispose()
{
//only dispose instance member, if any
if (_httpClient != null)
{
_httpClient.Dispose();
}
}
}
}