-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathForceClient.cs
More file actions
470 lines (372 loc) · 22.6 KB
/
Copy pathForceClient.cs
File metadata and controls
470 lines (372 loc) · 22.6 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using System.Reflection;
using Salesforce.Common;
using Salesforce.Common.Models;
using Salesforce.Common.Soql;
using Salesforce.Common.Models.Json;
using Salesforce.Common.Models.Xml;
using System.Dynamic;
using System.Collections;
namespace Salesforce.Force
{
public class ForceClient : IForceClient, IDisposable
{
protected readonly XmlHttpClient _xmlHttpClient;
protected readonly JsonHttpClient _jsonHttpClient;
public ISelectListResolver SelectListResolver { get; set; }
public ForceClient(string instanceUrl, string accessToken, string apiVersion)
: this(instanceUrl, accessToken, apiVersion, new HttpClient(), new HttpClient())
{
}
public ForceClient(string instanceUrl, string accessToken, string apiVersion, HttpClient httpClientForJson, HttpClient httpClientForXml, bool callerWillDisposeHttpClients = false)
{
if (string.IsNullOrEmpty(instanceUrl)) throw new ArgumentNullException("instanceUrl");
if (string.IsNullOrEmpty(accessToken)) throw new ArgumentNullException("accessToken");
if (string.IsNullOrEmpty(apiVersion)) throw new ArgumentNullException("apiVersion");
if (httpClientForJson == null) throw new ArgumentNullException("httpClientForJson");
if (httpClientForXml == null) throw new ArgumentNullException("httpClientForXml");
_jsonHttpClient = new JsonHttpClient(instanceUrl, apiVersion, accessToken, httpClientForJson, callerWillDisposeHttpClients);
_xmlHttpClient = new XmlHttpClient(instanceUrl, apiVersion, accessToken, httpClientForXml, callerWillDisposeHttpClients);
SelectListResolver = new DataMemberSelectListResolver();
}
// STANDARD METHODS
public Task<QueryResult<T>> QueryAsync<T>(string query)
{
if (string.IsNullOrEmpty(query)) throw new ArgumentNullException("query");
return _jsonHttpClient.HttpGetAsync<QueryResult<T>>(string.Format("query?q={0}", Uri.EscapeDataString(query)));
}
public Task<QueryResult<T>> QueryContinuationAsync<T>(string nextRecordsUrl)
{
if (string.IsNullOrEmpty(nextRecordsUrl)) throw new ArgumentNullException("nextRecordsUrl");
return _jsonHttpClient.HttpGetAsync<QueryResult<T>>(nextRecordsUrl);
}
public Task<QueryResult<T>> QueryAllAsync<T>(string query)
{
if (string.IsNullOrEmpty(query)) throw new ArgumentNullException("query");
return _jsonHttpClient.HttpGetAsync<QueryResult<T>>(string.Format("queryAll/?q={0}", Uri.EscapeDataString(query)));
}
public async Task<System.IO.Stream> GetBlobAsync(String objectName, String objectId, String fieldName)
{
if (string.IsNullOrEmpty(objectName)) throw new ArgumentNullException(nameof(objectName));
if (string.IsNullOrEmpty(objectId)) throw new ArgumentNullException(nameof(objectId));
if (string.IsNullOrEmpty(fieldName)) throw new ArgumentNullException(nameof(fieldName));
return await _jsonHttpClient.HttpGetBlobAsync($"sobjects/{objectName}/{objectId}/{fieldName}");
}
public async Task<T> ExecuteRestApiAsync<T>(string apiName)
{
if (string.IsNullOrEmpty(apiName)) throw new ArgumentNullException("apiName");
var response = await _jsonHttpClient.HttpGetRestApiAsync<T>(apiName);
return response;
}
public async Task<T> ExecuteRestApiAsync<T>(string apiName, object inputObject)
{
if (string.IsNullOrEmpty(apiName)) throw new ArgumentNullException("apiName");
if (inputObject == null) throw new ArgumentNullException("inputObject");
var response = await _jsonHttpClient.HttpPostRestApiAsync<T>(apiName, inputObject);
return response;
}
public async Task<T> QueryByIdAsync<T>(string objectName, string recordId)
{
if (string.IsNullOrEmpty(objectName)) throw new ArgumentNullException("objectName");
if (string.IsNullOrEmpty(recordId)) throw new ArgumentNullException("recordId");
var fields = SelectListResolver.GetFieldsList<T>();
var query = string.Format("SELECT {0} FROM {1} WHERE Id = '{2}'", fields, objectName, recordId);
var results = await QueryAsync<T>(query).ConfigureAwait(false);
return results.Records.FirstOrDefault();
}
public async Task<T> QueryAllFieldsByIdAsync<T>(string objectName, string recordId)
{
if (string.IsNullOrEmpty(objectName)) throw new ArgumentNullException("objectName");
if (string.IsNullOrEmpty(recordId)) throw new ArgumentNullException("recordId");
var fields = await GetFieldsCommaSeparatedListAsync(objectName);
var query = string.Format("SELECT {0} FROM {1} WHERE Id = '{2}'", fields, objectName, recordId);
var results = await QueryAsync<T>(query).ConfigureAwait(false);
return results.Records.FirstOrDefault();
}
public async Task<T> QueryAllFieldsByExternalIdAsync<T>(string objectName, string externalIdFieldName, string externalId)
{
if (string.IsNullOrEmpty(objectName)) throw new ArgumentNullException("objectName");
if (string.IsNullOrEmpty(externalIdFieldName)) throw new ArgumentNullException("externalIdFieldName");
if (string.IsNullOrEmpty(externalId)) throw new ArgumentNullException("externalId");
var fields = await GetFieldsCommaSeparatedListAsync(objectName);
var query = string.Format("SELECT {0} FROM {1} WHERE {2} = '{3}'", fields, objectName, externalIdFieldName, externalId);
var results = await QueryAsync<T>(query).ConfigureAwait(false);
return results.Records.FirstOrDefault();
}
public async Task<SuccessResponse> CreateAsync(string objectName, object record)
{
if (string.IsNullOrEmpty(objectName)) throw new ArgumentNullException("objectName");
if (record == null) throw new ArgumentNullException("record");
return await _jsonHttpClient.HttpPostAsync<SuccessResponse>(record, string.Format("sobjects/{0}", objectName)).ConfigureAwait(false);
}
public async Task<SaveResponse> CreateAsync(string objectName, CreateRequest request)
{
if (string.IsNullOrEmpty(objectName)) throw new ArgumentNullException("objectName");
if (request == null)
throw new ArgumentNullException("request");
var result = await _jsonHttpClient.HttpPostAsync<SaveResponse>(request, string.Format("composite/tree/{0}", objectName)).ConfigureAwait(false);
return result;
}
public Task<SuccessResponse> UpdateAsync(string objectName, string recordId, object record)
{
if (string.IsNullOrEmpty(objectName)) throw new ArgumentNullException("objectName");
if (string.IsNullOrEmpty(recordId)) throw new ArgumentNullException("recordId");
if (record == null) throw new ArgumentNullException("record");
return _jsonHttpClient.HttpPatchAsync(record, string.Format("sobjects/{0}/{1}", objectName, recordId));
}
public Task<SuccessResponse> UpdateAsync(string objectName, string recordId, object record, bool ignoreNull)
{
if (string.IsNullOrEmpty(objectName)) throw new ArgumentNullException("objectName");
if (string.IsNullOrEmpty(recordId)) throw new ArgumentNullException("recordId");
if (record == null) throw new ArgumentNullException("record");
return _jsonHttpClient.HttpPatchAsync(record, string.Format("sobjects/{0}/{1}", objectName, recordId), ignoreNull);
}
public Task<SuccessResponse> UpsertExternalAsync(string objectName, string externalFieldName, string externalId, object record)
{
if (string.IsNullOrEmpty(objectName)) throw new ArgumentNullException("objectName");
if (string.IsNullOrEmpty(externalFieldName)) throw new ArgumentNullException("externalFieldName");
if (string.IsNullOrEmpty(externalId)) throw new ArgumentNullException("externalId");
if (record == null) throw new ArgumentNullException("record");
return _jsonHttpClient.HttpPatchAsync(record, string.Format("sobjects/{0}/{1}/{2}", objectName, externalFieldName, externalId));
}
public Task<SuccessResponse> UpsertExternalAsync(string objectName, string externalFieldName, string externalId, object record, bool ignoreNull)
{
if (string.IsNullOrEmpty(objectName)) throw new ArgumentNullException("objectName");
if (string.IsNullOrEmpty(externalFieldName)) throw new ArgumentNullException("externalFieldName");
if (string.IsNullOrEmpty(externalId)) throw new ArgumentNullException("externalId");
if (record == null) throw new ArgumentNullException("record");
return _jsonHttpClient.HttpPatchAsync(record, string.Format("sobjects/{0}/{1}/{2}", objectName, externalFieldName, externalId), ignoreNull);
}
public Task<bool> DeleteAsync(string objectName, string recordId)
{
if (string.IsNullOrEmpty(objectName)) throw new ArgumentNullException("objectName");
if (string.IsNullOrEmpty(recordId)) throw new ArgumentNullException("recordId");
return _jsonHttpClient.HttpDeleteAsync(string.Format("sobjects/{0}/{1}", objectName, recordId));
}
public Task<bool> DeleteExternalAsync(string objectName, string externalFieldName, string externalId)
{
if (string.IsNullOrEmpty(objectName)) throw new ArgumentNullException("objectName");
if (string.IsNullOrEmpty(externalFieldName)) throw new ArgumentNullException("externalFieldName");
if (string.IsNullOrEmpty(externalId)) throw new ArgumentNullException("externalId");
return _jsonHttpClient.HttpDeleteAsync(string.Format("sobjects/{0}/{1}/{2}", objectName, externalFieldName, externalId));
}
public Task<DescribeGlobalResult<T>> GetObjectsAsync<T>()
{
return _jsonHttpClient.HttpGetAsync<DescribeGlobalResult<T>>("sobjects");
}
public Task<T> BasicInformationAsync<T>(string objectName)
{
if (string.IsNullOrEmpty(objectName)) throw new ArgumentNullException("objectName");
return _jsonHttpClient.HttpGetAsync<T>(string.Format("sobjects/{0}", objectName));
}
public Task<T> DescribeAsync<T>(string objectName)
{
if (string.IsNullOrEmpty(objectName)) throw new ArgumentNullException("objectName");
return _jsonHttpClient.HttpGetAsync<T>(string.Format("sobjects/{0}/describe/", objectName));
}
public Task<T> GetDeleted<T>(string objectName, DateTime startDateTime, DateTime endDateTime)
{
var sdt = Uri.EscapeDataString(startDateTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss+00:00", System.Globalization.CultureInfo.InvariantCulture));
var edt = Uri.EscapeDataString(endDateTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss+00:00", System.Globalization.CultureInfo.InvariantCulture));
return _jsonHttpClient.HttpGetAsync<T>(string.Format("sobjects/{0}/deleted/?start={1}&end={2}", objectName, sdt, edt));
}
public Task<T> GetUpdated<T>(string objectName, DateTime startDateTime, DateTime endDateTime)
{
var sdt = Uri.EscapeDataString(startDateTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss+00:00", System.Globalization.CultureInfo.InvariantCulture));
var edt = Uri.EscapeDataString(endDateTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss+00:00", System.Globalization.CultureInfo.InvariantCulture));
return _jsonHttpClient.HttpGetAsync<T>(string.Format("sobjects/{0}/updated/?start={1}&end={2}", objectName, sdt, edt));
}
public Task<T> DescribeLayoutAsync<T>(string objectName)
{
if (string.IsNullOrEmpty(objectName)) throw new ArgumentNullException("objectName");
return _jsonHttpClient.HttpGetAsync<T>(string.Format("sobjects/{0}/describe/layouts/", objectName));
}
public Task<T> DescribeLayoutAsync<T>(string objectName, string recordTypeId)
{
if (string.IsNullOrEmpty(objectName)) throw new ArgumentNullException("objectName");
if (string.IsNullOrEmpty(recordTypeId)) throw new ArgumentNullException("recordTypeId");
return _jsonHttpClient.HttpGetAsync<T>(string.Format("sobjects/{0}/describe/layouts/{1}", objectName, recordTypeId));
}
public Task<T> RecentAsync<T>(int limit = 200)
{
return _jsonHttpClient.HttpGetAsync<T>(string.Format("recent/?limit={0}", limit));
}
public Task<List<T>> SearchAsync<T>(string query)
{
if (string.IsNullOrEmpty(query)) throw new ArgumentNullException("query");
if (!query.Contains("FIND")) throw new ArgumentException("query does not contain FIND");
if (!query.Contains("{") || !query.Contains("}")) throw new ArgumentException("search term must be wrapped in braces");
return _jsonHttpClient.HttpGetAsync<List<T>>(string.Format("search?q={0}", Uri.EscapeDataString(query)));
}
public async Task<T> UserInfo<T>(string url)
{
if (string.IsNullOrEmpty(url)) throw new ArgumentNullException("url");
if (!Uri.IsWellFormedUriString(url, UriKind.Absolute)) throw new FormatException("url");
var response = await _jsonHttpClient.HttpGetAsync<T>(new Uri(url));
return response;
}
public async Task<string> GetFieldsCommaSeparatedListAsync(string objectName)
{
IDictionary<string, object> objectProperties = await DescribeAsync<ExpandoObject>(objectName);
objectProperties.TryGetValue("fields", out object fields);
List<string> objectDescription = new List<string>();
foreach (ExpandoObject field in fields as IEnumerable)
{
objectDescription.Add((field).FirstOrDefault(x => x.Key == "name").Value.ToString());
}
return string.Join(", ", objectDescription);
}
public Task<T> ExecuteAnonymousAsync<T>(string apex)
{
if (string.IsNullOrEmpty(apex)) throw new ArgumentNullException("apex");
return _jsonHttpClient.HttpGetAsync<T>(string.Format("tooling/executeAnonymous/?anonymousBody={0}", Uri.EscapeDataString(apex)));
}
// BULK METHODS
public async Task<List<BatchInfoResult>> RunJobAsync<T>(string objectName, BulkConstants.OperationType operationType,
IEnumerable<ISObjectList<T>> recordsLists)
{
return await RunJobAsync(objectName, null, operationType, recordsLists);
}
public async Task<List<BatchInfoResult>> RunJobAsync<T>(string objectName, string externalIdFieldName,
BulkConstants.OperationType operationType, IEnumerable<ISObjectList<T>> recordsLists)
{
if (recordsLists == null) throw new ArgumentNullException("recordsLists");
if (operationType == BulkConstants.OperationType.Upsert && string.IsNullOrEmpty(externalIdFieldName)) throw new ArgumentNullException(nameof(externalIdFieldName));
var jobInfoResult = await CreateJobAsync(objectName, externalIdFieldName, operationType);
var batchResults = new List<BatchInfoResult>();
foreach (var recordList in recordsLists)
{
batchResults.Add(await CreateJobBatchAsync(jobInfoResult, recordList));
}
await CloseJobAsync(jobInfoResult);
return batchResults;
}
public async Task<List<BatchResultList>> RunJobAndPollAsync<T>(string objectName, BulkConstants.OperationType operationType,
IEnumerable<ISObjectList<T>> recordsLists)
{
return await RunJobAndPollAsync(objectName, null, operationType, recordsLists);
}
public async Task<List<BatchResultList>> RunJobAndPollAsync<T>(string objectName, string externalIdFieldName, BulkConstants.OperationType operationType,
IEnumerable<ISObjectList<T>> recordsLists)
{
const float pollingStart = 1000;
const float pollingIncrease = 2.0f;
var batchInfoResults = await RunJobAsync(objectName, externalIdFieldName, operationType, recordsLists);
var currentPoll = pollingStart;
var finishedBatchInfoResults = new List<BatchInfoResult>();
while (batchInfoResults.Count > 0)
{
var removeList = new List<BatchInfoResult>();
foreach (var batchInfoResult in batchInfoResults)
{
var batchInfoResultNew = await PollBatchAsync(batchInfoResult);
if (batchInfoResultNew.State.Equals(BulkConstants.BatchState.Completed.Value()) ||
batchInfoResultNew.State.Equals(BulkConstants.BatchState.Failed.Value()) ||
batchInfoResultNew.State.Equals(BulkConstants.BatchState.NotProcessed.Value()))
{
finishedBatchInfoResults.Add(batchInfoResultNew);
removeList.Add(batchInfoResult);
}
}
foreach (var removeItem in removeList)
{
batchInfoResults.Remove(removeItem);
}
await Task.Delay((int)currentPoll);
currentPoll *= pollingIncrease;
}
var batchResults = new List<BatchResultList>();
foreach (var batchInfoResultComplete in finishedBatchInfoResults)
{
batchResults.Add(await GetBatchResultAsync(batchInfoResultComplete));
}
return batchResults;
}
public async Task<JobInfoResult> CreateJobAsync(string objectName, BulkConstants.OperationType operationType)
{
return await CreateJobAsync(objectName, null, operationType);
}
public async Task<JobInfoResult> CreateJobAsync(string objectName, string externalIdFieldName, BulkConstants.OperationType operationType)
{
if (string.IsNullOrEmpty(objectName)) throw new ArgumentNullException(nameof(objectName));
if (operationType == BulkConstants.OperationType.Upsert && string.IsNullOrEmpty(externalIdFieldName)) throw new ArgumentNullException(nameof(externalIdFieldName));
var jobInfo = new JobInfo
{
ContentType = "XML",
Object = objectName,
ExternalIdFieldName = externalIdFieldName,
Operation = operationType.Value()
};
return await _xmlHttpClient.HttpPostAsync<JobInfoResult>(jobInfo, "/services/async/{0}/job");
}
public async Task<BatchInfoResult> CreateJobBatchAsync<T>(JobInfoResult jobInfo, ISObjectList<T> recordsList)
{
if (jobInfo == null) throw new ArgumentNullException("jobInfo");
return await CreateJobBatchAsync(jobInfo.Id, recordsList).ConfigureAwait(false);
}
public async Task<BatchInfoResult> CreateJobBatchAsync<T>(string jobId, ISObjectList<T> recordsObject)
{
if (string.IsNullOrEmpty(jobId)) throw new ArgumentNullException("jobId");
if (recordsObject == null) throw new ArgumentNullException("recordsObject");
return await _xmlHttpClient.HttpPostAsync<BatchInfoResult>(recordsObject, string.Format("/services/async/{{0}}/job/{0}/batch", jobId))
.ConfigureAwait(false);
}
public async Task<JobInfoResult> CloseJobAsync(JobInfoResult jobInfo)
{
if (jobInfo == null) throw new ArgumentNullException("jobInfo");
return await CloseJobAsync(jobInfo.Id);
}
public async Task<JobInfoResult> CloseJobAsync(string jobId)
{
if (string.IsNullOrEmpty(jobId)) throw new ArgumentNullException("jobId");
var state = new JobInfoState { State = "Closed" };
return await _xmlHttpClient.HttpPostAsync<JobInfoResult>(state, string.Format("/services/async/{{0}}/job/{0}", jobId))
.ConfigureAwait(false);
}
public async Task<JobInfoResult> PollJobAsync(JobInfoResult jobInfo)
{
if (jobInfo == null) throw new ArgumentNullException("jobInfo");
return await PollJobAsync(jobInfo.Id);
}
public async Task<JobInfoResult> PollJobAsync(string jobId)
{
if (string.IsNullOrEmpty(jobId)) throw new ArgumentNullException("jobId");
return await _xmlHttpClient.HttpGetAsync<JobInfoResult>(string.Format("/services/async/{{0}}/job/{0}", jobId))
.ConfigureAwait(false);
}
public async Task<BatchInfoResult> PollBatchAsync(BatchInfoResult batchInfo)
{
if (batchInfo == null) throw new ArgumentNullException("batchInfo");
return await PollBatchAsync(batchInfo.Id, batchInfo.JobId);
}
public async Task<BatchInfoResult> PollBatchAsync(string batchId, string jobId)
{
if (string.IsNullOrEmpty(batchId)) throw new ArgumentNullException("batchId");
if (string.IsNullOrEmpty(jobId)) throw new ArgumentNullException("jobId");
return await _xmlHttpClient.HttpGetAsync<BatchInfoResult>(string.Format("/services/async/{{0}}/job/{0}/batch/{1}", jobId, batchId))
.ConfigureAwait(false);
}
public async Task<BatchResultList> GetBatchResultAsync(BatchInfoResult batchInfo)
{
if (batchInfo == null) throw new ArgumentNullException("batchInfo");
return await GetBatchResultAsync(batchInfo.Id, batchInfo.JobId);
}
public async Task<BatchResultList> GetBatchResultAsync(string batchId, string jobId)
{
if (string.IsNullOrEmpty(batchId)) throw new ArgumentNullException("batchId");
if (string.IsNullOrEmpty(jobId)) throw new ArgumentNullException("jobId");
return await _xmlHttpClient.HttpGetAsync<BatchResultList>(string.Format("/services/async/{{0}}/job/{0}/batch/{1}/result", jobId, batchId))
.ConfigureAwait(false);
}
public void Dispose()
{
_jsonHttpClient.Dispose();
_xmlHttpClient.Dispose();
}
}
}