-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathProgram.cs
More file actions
672 lines (498 loc) · 22.4 KB
/
Program.cs
File metadata and controls
672 lines (498 loc) · 22.4 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
using HP.HPTRIM.ServiceModel;
using Microsoft.Identity.Client;
using ServiceStack;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Diagnostics.Eventing.Reader;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Mime;
using System.Threading.Tasks;
using System.Web;
namespace ConsoleServiceAPIClient
{
class Program
{
private static JsonHttpClient _trimClient;
private static HttpClient _httpClient;
static IPublicClientApplication _app;
static async Task<string> getAuthToken()
{
string clientId = System.Configuration.ConfigurationManager.AppSettings["clientId"];
string tenantId = System.Configuration.ConfigurationManager.AppSettings["tenantId"];
if (_app == null)
{
_app = PublicClientApplicationBuilder.Create(clientId)
.WithRedirectUri("https://login.microsoftonline.com/common/oauth2/nativeclient")
.WithAuthority(AzureCloudInstance.AzurePublic, tenantId)
.Build();
TokenCacheHelper.EnableSerialization(_app.UserTokenCache);
}
var accounts = await _app.GetAccountsAsync();
AuthenticationResult result;
var scopes = new string[] { "User.Read", "offline_access", "openid", "profile" };
try
{
result = await _app.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
.ExecuteAsync();
}
catch (MsalUiRequiredException ex)
{
// A MsalUiRequiredException happened on AcquireTokenSilent.
// This indicates you need to call AcquireTokenInteractive to acquire a token
System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");
try
{
result = await _app.AcquireTokenInteractive(scopes)
.ExecuteAsync();
}
catch (MsalException msalex)
{
Console.WriteLine($"Error Acquiring Token:{System.Environment.NewLine}{msalex}");
throw;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error Acquiring Token Silently:{System.Environment.NewLine}{ex}");
throw;
}
return result.IdToken;
}
static async Task<string> getConfidentialClientAuthToken()
{
string clientId = System.Configuration.ConfigurationManager.AppSettings["clientId"];
string scopeClientId = System.Configuration.ConfigurationManager.AppSettings["scopeClientId"];
string tenantId = System.Configuration.ConfigurationManager.AppSettings["tenantId"];
string clientSecret = System.Configuration.ConfigurationManager.AppSettings["clientSecret"];
string[] scopes = new string[] { $"{scopeClientId}/.default" };
IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create(clientId)
.WithAuthority(AzureCloudInstance.AzurePublic, tenantId)
.WithClientSecret(clientSecret)
.Build();
// Microsoft.Identity.Client.AuthenticationResult result;
// var authResult = app.AcquireTokenForClient(scopes).ExecuteAsync();
try
{
var result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
return result.AccessToken;
}
catch (AggregateException agEx)
{
foreach (var ex in agEx.InnerExceptions)
{
Console.WriteLine(ex.Message, ex);
}
throw new TimeoutException($"Error connecting to identity provider.");
}
}
static async Task<JsonHttpClient> getServiceClient()
{
// this is only test code so we will bypass TLS certificate errors...
ServicePointManager
.ServerCertificateValidationCallback +=
(sender, cert, chain, sslPolicyErrors) => true;
if (_trimClient == null)
{
string url = System.Configuration.ConfigurationManager.AppSettings["url"];
if (string.IsNullOrWhiteSpace(url))
{
throw new ConfigurationErrorsException("Unable to find URL in my.config");
}
_trimClient = new JsonHttpClient(url);
}
string userName = System.Configuration.ConfigurationManager.AppSettings["userName"];
string password = System.Configuration.ConfigurationManager.AppSettings["password"];
string clientSecret = System.Configuration.ConfigurationManager.AppSettings["clientSecret"];
if (!string.IsNullOrWhiteSpace(clientSecret))
{
string token = await getConfidentialClientAuthToken();
_trimClient.Headers["Authorization"] = $"Bearer {token}";
}
else if (string.IsNullOrWhiteSpace(userName))
{
string token = await getAuthToken();
_trimClient.Headers["Authorization"] = $"Bearer {token}";
}
else
{
_trimClient.SetCredentials(userName, password);
_trimClient.AlwaysSendBasicAuthHeader = true;
}
return _trimClient;
}
static async Task<HttpClient> getHttpClient()
{
string token = await getAuthToken();
if (_httpClient == null)
{
_httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
return _httpClient;
}
static async Task Main(string[] args)
{
var stopWatch = Stopwatch.StartNew();
// await updateRecord();
// await removeContact();
// await getRecordByUri();
// await recordTypeSearch();
// await getRecordUri();
// await getMe();
// await getRecordTitle();
// await createRecord();
// await recordSearch();
// await streamSearch();
//await createRecordWithDocument();
//await getDocument();
await getDocumentInChunks();
// await uploadFileAndCreateRecord();
//await uploadBinaryFileAndCreateRecord();
Console.WriteLine($"=================== {stopWatch.ElapsedMilliseconds} =======================");
Console.ReadKey();
}
private async static Task recordTypeSearch()
{
var trimClient = await getServiceClient();
var response = trimClient.Get<RecordTypesResponse>(new RecordTypes() { q = "all" });
Console.WriteLine(response.Results[0].Uri);
}
private async static Task steamSearch()
{
var trimClient = await getServiceClient();
var response = trimClient.Get<RecordsResponse>(new RecordFind() { Id = "1501", Properties = new List<string>() { $"{PropertyIds.RecordTitle}" } });
Console.WriteLine(response.Results[0].Number);
Console.WriteLine(response.Results[0].Title);
}
private async static Task getRecordUri()
{
var trimClient = await getServiceClient();
var response = trimClient.Get<RecordsResponse>(new RecordFind() { Id = "REC_1" });
Console.WriteLine(response.Results[0].Uri);
}
private async static Task getMe()
{
var trimClient = await getServiceClient();
var response = trimClient.Get<LocationsResponse>(new LocationFind() { Id = "me" });
Console.WriteLine(response.Results[0].Uri);
}
private async static Task getRecordTitle()
{
var trimClient = await getServiceClient();
var response = trimClient.Get<RecordsResponse>(new RecordFind()
{
Id = "REC_1",
Properties = new List<string>() { $"{PropertyIds.RecordTitle}" }
});
Console.WriteLine(response.Results[0].Title);
}
private async static Task createRecord()
{
var trimClient = await getServiceClient();
var record = new Record()
{
RecordType = new RecordTypeRef() { FindBy = "Document" },
Title = "my test",
Properties = new List<string>() { $"{PropertyIds.RecordTitle}" }
};
var response = trimClient.Post<RecordsResponse>(record);
Console.WriteLine(response.Results[0].Title);
}
private async static Task updateRecord()
{
var trimClient = await getServiceClient();
var surfaces = new string[] { "Dirt", "Bitumen", "Concrete", "Blue Metal" };
for (int counter = 0; counter < 100; counter++)
{
Random random = new Random();
int number = random.Next(0, 3);
var fields = new Dictionary<string, string>();
fields["RoadSurface"] = surfaces[number];
Console.WriteLine("*****************************");
Console.WriteLine(fields["RoadSurface"]);
var record = new Record()
{
Uri = 9000000221,
AdditionalFields = fields,
};
trimClient.Post<RecordsResponse>(record);
var response = trimClient.Get<RecordsResponse>(new RecordFind()
{
Id = "9000000221",
Properties = new List<string>() { $"RoadSurface" },
PropertyValue = PropertyType.Both
});
Console.WriteLine(response.Results[0].Fields["RoadSurface"].StringValue);
Console.WriteLine("*****************************");
}
}
private async static Task recordSearch()
{
var trimClient = await getServiceClient();
trimClient.AddHeader("Next-Page-Id", $"{Guid.NewGuid()}");
PropertyIds[] propertyIds = new PropertyIds[] {
PropertyIds.RecordNumber,
// PropertyIds.RecordAuthor,
// PropertyIds.RecordAssignee,
// PropertyIds.RecordTitle,
// PropertyIds.RecordRevisionNumber,
// PropertyIds.RecordClassification,
// PropertyIds.RecordNotes,
// PropertyIds.RecordContainer,
// PropertyIds.RecordIsElectronic,
};
RecordsResponse response;
int pageSize = 3;
int start = 1;
do
{
response = trimClient.Get<RecordsResponse>(new Records()
{
q = "electronic",
Properties = propertyIds.Select(pid => $"{pid}").ToList(),
ResultsOnly = true,
PropertyValue = PropertyType.Both,
pageSize = pageSize,
// start = start,
sortBy = new string[] { "uri" }
});
Console.WriteLine("***********************************************");
foreach (var record in response.Results)
{
Console.WriteLine(record.Number);
}
start = start + pageSize;
} while (response.HasMoreItems == true && start < 70);
}
private async static Task removeContact()
{
var trimClient = await getServiceClient();
var response = trimClient.Post<RecordsResponse>(new Record()
{
Uri = 9000000001,
ChildLocations = new List<RecordLocation>() {
new RecordLocation() {
Uri = 9000004217,
TypeOfRecordLocation = RecordLocationType.Contact,
Delete = true
}
}
});
}
private async static Task streamSearch()
{
PropertyIds[] propertyIds = new PropertyIds[] {
PropertyIds.RecordNumber,
// PropertyIds.RecordAuthor,
PropertyIds.RecordAssignee,
PropertyIds.RecordTitle,
PropertyIds.RecordRevisionNumber,
PropertyIds.RecordClassification,
// PropertyIds.RecordNotes,
PropertyIds.RecordContainer,
PropertyIds.RecordIsElectronic,
};
bool more = false;
int start = 0;
var trimClient = await getServiceClient();
var hc = trimClient.GetHttpClient();
hc.Timeout = new System.TimeSpan(1, 30, 0);
string id = $"{Guid.NewGuid()}";
do
{
try
{
var response = trimClient.Get<RecordsResponse>(new TrimStreamSearch()
{
TrimType = BaseObjectTypes.Record,
q = "recElectronic",
Properties = propertyIds.Select(pid => $"{pid}").ToList(),
pageSize = 2000,
sortBy = new string[] { "updated" },
Filter = "updated>1970-01-01 00:00:00Z",
start = start,
Id = id
});
more = response.HasMoreItems;
start = start + response.Results.Count;
foreach (var record in response.Results)
{
Console.WriteLine($"{record.IsElectronic} - {record.Uri} - {record.Title} - {record.Number} - {record.Assignee}");
}
return;
}
catch (Exception ex)
{
Console.WriteLine("============ ERROR ==========");
Console.WriteLine(ex.Message);
more = true;
}
} while (more == true);
}
private async static Task createRecordWithDocument()
{
var trimClient = await getServiceClient();
var record = new Record()
{
RecordType = new RecordTypeRef() { FindBy = "Document" },
Title = "my test document",
Properties = new List<string>() { $"{PropertyIds.RecordTitle}" }
};
using (FileStream filestream = new FileStream("d:\\junk\\trim.png", FileMode.Open))
{
var uploadFile = new ServiceStack.UploadFile("trim.png", filestream);
uploadFile.ContentType = "image/png";
var response = trimClient.PostFilesWithRequest<RecordsResponse>(record, new ServiceStack.UploadFile[] { uploadFile });
Console.WriteLine(response.Results[0].Title);
}
}
private async static Task uploadFileAndCreateRecord()
{
var trimClient = await getServiceClient();
var httpClient = await getHttpClient();
HP.HPTRIM.ServiceModel.UploadFile uploadFileRequest = new HP.HPTRIM.ServiceModel.UploadFile();
string url = trimClient.ResolveTypedUrl("POST", uploadFileRequest);
using (var fileStream = File.OpenRead("d:\\junk\\trim.png"))
using (var formContent = new MultipartFormDataContent("NKdKd9Yk"))
using (var streamContent = new StreamContent(fileStream))
{
formContent.Headers.ContentType.MediaType = "multipart/form-data";
formContent.Add(streamContent, "Files", "trim.png");
var uploadedFileResponse = await httpClient.PostAsync(url, formContent);
var uploadedJson = await uploadedFileResponse.Content.ReadAsStringAsync();
var uploadedFile = uploadedJson.FromJson<UploadFileResponse>();
var record = new Record()
{
RecordType = new RecordTypeRef() { FindBy = "Document" },
Title = "my test document",
Properties = new List<string>() { $"{PropertyIds.RecordTitle}" },
FilePath = uploadedFile.FilePath
};
var response = await trimClient.PostAsync<RecordsResponse>(record);
Console.WriteLine(response.Results[0].Title);
}
}
// this is not supported up to CM 10. It may be supported in a later release.
private async static Task uploadBinaryFileAndCreateRecord()
{
var trimClient = await getServiceClient();
var httpClient = await getHttpClient();
HP.HPTRIM.ServiceModel.UploadFile uploadFileRequest = new HP.HPTRIM.ServiceModel.UploadFile();
string url = trimClient.ResolveTypedUrl("POST", uploadFileRequest);
using (var fileStream = File.OpenRead("d:\\junk\\trim.png"))
using (var streamContent = new StreamContent(fileStream))
{
streamContent.Headers.ContentType = new MediaTypeHeaderValue("image/png");
var uploadedFileResponse = await httpClient.PostAsync(url + "/trim.png", streamContent);
var uploadedJson = await uploadedFileResponse.Content.ReadAsStringAsync();
var uploadedFile = uploadedJson.FromJson<UploadFileResponse>();
var record = new Record()
{
RecordType = new RecordTypeRef() { FindBy = "Document" },
Title = "my test document",
Properties = new List<string>() { $"{PropertyIds.RecordTitle}" },
FilePath = uploadedFile.FilePath
};
var response = await trimClient.PostAsync<RecordsResponse>(record);
Console.WriteLine(response.Results[0].Title);
}
}
private async static Task getDocument()
{
var trimClient = await getServiceClient();
var httpClient = await getHttpClient();
var recordDownload = new RecordDownload()
{
Id = "REC_1",
DownloadType = DownloadType.Document
};
string url = trimClient.ResolveTypedUrl("GET", recordDownload);
var response = await httpClient.GetAsync(url).ConfigureAwait(false);
string fileName = "test.dat";
IEnumerable<string> values;
if (response.Content.Headers.TryGetValues("Content-Disposition", out values))
{
ContentDisposition contentDisposition = new ContentDisposition(values.First());
fileName = contentDisposition.FileName;
}
using (var fileStream = File.Create(Path.Combine($"C:\\junk\\{fileName}")))
{
var stream = await response.Content.ReadAsStreamAsync();
stream.CopyTo(fileStream);
}
}
// This only works in Content Manager versions subsequent to version 10.1 as the Content-Range header
// was not supported in 10.1.x and earlier
private async static Task getDocumentInChunks()
{
var trimClient = await getServiceClient();
var httpClient = await getHttpClient();
long to = 0;
long length = 0;
string filePath = null;
// long chunkSize = 1024 * 1000;
long chunkSize = 999;
var recordDownload = new RecordDownload()
{
Id = "REC_264",
DownloadType = DownloadType.Document
};
string url = trimClient.ResolveTypedUrl("GET", recordDownload);
do
{
var request = new HttpRequestMessage()
{
RequestUri = new Uri(url),
Method = HttpMethod.Get,
};
long endRange = chunkSize;
if (to > 0)
{
if ((length - to) > chunkSize)
{
endRange = (to + 1) + chunkSize;
}
else
{
endRange = length - 1;
}
}
request.Headers.Range = new RangeHeaderValue(to > 0 ? to + 1 : 0, endRange);
var response = await httpClient.SendAsync(request).ConfigureAwait(false);
if (response.Content.Headers.ContentRange.Length.HasValue
&& response.Content.Headers.ContentRange.To.HasValue)
{
length = (long)response.Content.Headers.ContentRange.Length;
}
if (response.Content.Headers.ContentRange.To.HasValue)
{
to = (long)response.Content.Headers.ContentRange.To;
}
if (string.IsNullOrWhiteSpace(filePath))
{
string fileName = "test.dat";
IEnumerable<string> values;
if (response.Content.Headers.TryGetValues("Content-Disposition", out values))
{
ContentDisposition contentDisposition = new ContentDisposition(values.First());
fileName = HttpUtility.UrlDecode(contentDisposition.FileName);
}
filePath = $"C:\\junk\\{fileName}";
File.Delete(filePath);
}
using (var fileStream = new FileStream(filePath, FileMode.Append))
{
var stream = await response.Content.ReadAsStreamAsync();
fileStream.Write(stream.ReadFully(), 0, Convert.ToInt32(stream.Length));
}
} while ((to + 1) < length);
}
}
}