-
Notifications
You must be signed in to change notification settings - Fork 2
/
Connection.cs
952 lines (922 loc) · 39 KB
/
Connection.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
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
/*
Copyright 2019 Cogniac Corporation.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.IO;
using System.Collections.Generic;
using RestSharp;
using RestSharp.Authenticators;
using RestSharp.Deserializers;
using System.Net;
using System.Timers;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
namespace Cogniac
{
/// <summary>
/// Main entry point of the SDK
/// </summary>
public class Connection
{
private string _urlPrefix = "";
private string _username = "";
private string _password = "";
private string _tenantId = "";
private string _providedToken = "";
private Auth _authObject = null;
private bool _autoRenewToken = true;
private long? _tokenExpiresIn = 0;
private Timer _tokenTimer;
private const uint _expirationOffset = 30;
/// <summary>
/// Main class to establish a connection to the Cogniac public API.
/// </summary>
/// <param name="username">Username</param>
/// <param name="password">Password</param>
/// <param name="tenantId">Tenant ID</param>
/// <param name="token">Access Token</param>
/// <param name="urlPrefix">URL Prefix</param>
/// <param name="autoRenewToken">Auto Renew Access Token</param>
public Connection(string username = "", string password = "", string tenantId = "", string token = "", string urlPrefix = "https://api.cogniac.io/1", bool autoRenewToken = true)
{
_username = username;
_password = password;
_providedToken = token;
_tenantId = tenantId;
_urlPrefix = urlPrefix;
_autoRenewToken = autoRenewToken;
// if token is provided, no need to validate other input data
if (string.IsNullOrEmpty(_providedToken))
{
ValidateUserPass();
ValidateTenantId();
Authenticate();
}
if (string.IsNullOrEmpty(_urlPrefix))
{
throw new ArgumentException(message: "urlPrefix parameter is null or empty.");
}
}
/// <summary>
/// Gets an Auth object after a Connection object is created
/// </summary>
/// <returns>Cogniac.Auth object</returns>
public Auth GetAuthObject()
{
return _authObject;
}
private void Authenticate()
{
string fullUrl = $"{_urlPrefix}/token?tenant_id={_tenantId}";
IRestResponse resp = ExecuteRequest(fullUrl);
try
{
// Populate the Auth object with the missing information
_authObject = Auth.FromJson(resp.Content);
_providedToken = _authObject.AccessToken;
var decoded_token = new JwtSecurityToken(jwtEncodedString: _providedToken);
_authObject.UserId = decoded_token.Claims.First(c => c.Type == "sub").Value;
_authObject.TenantId = decoded_token.Claims.First(c => c.Type == "tid").Value;
_authObject.ExpiresIn = (long)(Convert.ToDouble(decoded_token.Claims.First(c => c.Type == "exp").Value)
- Convert.ToDouble(decoded_token.Claims.First(c => c.Type == "iat").Value));
_authObject.TenantName = GetTenant(_authObject.TenantId).Name;
_authObject.UserEmail = decoded_token.Claims.First(c => c.Type == "ema").Value;
_tokenExpiresIn = _authObject.ExpiresIn;
if (_autoRenewToken == true)
{
if (_tokenExpiresIn != null)
{
_tokenTimer = new Timer();
_tokenTimer.Elapsed += new ElapsedEventHandler(OnTokenTimedEvent);
_tokenTimer.Interval = TimeSpan.FromSeconds((double)(_tokenExpiresIn - _expirationOffset)).TotalMilliseconds;
_tokenTimer.Enabled = true;
_tokenTimer.Start();
}
}
}
catch
{
throw new ArgumentException(message: resp.Content);
}
}
private void OnTokenTimedEvent(object source, ElapsedEventArgs e)
{
string fullUrl = $"{_urlPrefix}/token?tenant_id={_tenantId}";
var client = new RestClient(fullUrl);
var request = new RestRequest(Method.GET);
IRestResponse resp = null;
try
{
client.Authenticator = new HttpBasicAuthenticator(_username, _password);
client.AddHandler("application/json", new JsonDeserializer());
request.AddHeader("Cache-Control", "no-cache");
resp = Retry.Do(() => client.Execute(request), TimeSpan.FromSeconds(5), 3);
_authObject = Auth.FromJson(resp.Content);
_providedToken = _authObject.AccessToken;
var decoded_token = new JwtSecurityToken(jwtEncodedString: _providedToken);
_authObject.UserId = decoded_token.Claims.First(c => c.Type == "sub").Value;
_authObject.TenantId = decoded_token.Claims.First(c => c.Type == "tid").Value;
_authObject.ExpiresIn = (long)(Convert.ToDouble(decoded_token.Claims.First(c => c.Type == "exp").Value)
- Convert.ToDouble(decoded_token.Claims.First(c => c.Type == "iat").Value));
_authObject.TenantName = GetTenant(_authObject.TenantId).Name;
_authObject.UserEmail = decoded_token.Claims.First(c => c.Type == "ema").Value;
_tokenExpiresIn = _authObject.ExpiresIn;
_tokenTimer.Interval = TimeSpan.FromSeconds((double)(_tokenExpiresIn - _expirationOffset)).TotalMilliseconds;
}
catch (Exception)
{
throw;
}
}
private IRestResponse ExecuteRequest(string fullUrl, RestRequest request = null)
{
var client = new RestClient(fullUrl);
if(request == null)
{
request = new RestRequest(Method.GET);
}
if (!string.IsNullOrEmpty(_providedToken))
{
request.AddHeader("Authorization", $"Bearer {_providedToken}");
}
else
{
client.Authenticator = new HttpBasicAuthenticator(_username, _password);
}
client.AddHandler("application/json", new JsonDeserializer());
request.AddHeader("Cache-Control", "no-cache");
return Retry.Do(() => client.Execute(request), TimeSpan.FromSeconds(5), 3);
}
/// <summary>
/// Gets an AuthorizedTenants object given a username and password
/// </summary>
/// <param name="username">Username</param>
/// <param name="password">Password</param>
/// <param name="urlPrefix">URL Prefix</param>
/// <returns>Cogniac.AuthorizedTenants object</returns>
public static AuthorizedTenants GetAllAuthorizedTenants(string username = "", string password = "", string urlPrefix = "https://api.cogniac.io/1")
{
var client = new RestClient($"{urlPrefix}/users/current/tenants");
var request = new RestRequest(Method.GET);
AuthorizedTenants returnValue = null;
IRestResponse resp = null;
try
{
client.Authenticator = new HttpBasicAuthenticator(username, password);
client.AddHandler("application/json", new JsonDeserializer());
request.AddHeader("Cache-Control", "no-cache");
resp = Retry.Do(() => client.Execute(request), TimeSpan.FromSeconds(5), 3);
returnValue = AuthorizedTenants.FromJson(resp.Content);
}
catch (Exception)
{
throw;
}
return returnValue;
}
private void ValidateTenantId()
{
// If tenantId is provided, we use it
if (string.IsNullOrEmpty(_tenantId))
{
// We don't have a tenantId, check environment variables
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("COG_TENANT")))
{
_tenantId = Environment.GetEnvironmentVariable("COG_TENANT");
}
else
{
// We don't have a tenantId in the environment and it is not provided
// Get list of tenants, if only 1, use it.
AuthorizedTenants at;
at = GetAllAuthorizedTenants(_username, _password, urlPrefix: _urlPrefix);
if (at.Tenants.Length.Equals(1))
{
_tenantId = at.Tenants[0].TenantId;
}
else
{
throw new ArgumentException(message: "Provided user has multiple or no tenants, please use GetAllAuthorizedTenants(...) first and supply a single valid tenantId parameter.");
}
}
}
}
private void ValidateUserPass()
{
// If token is provided, username and passwords don't matter
if (string.IsNullOrEmpty(_providedToken))
{
if (string.IsNullOrEmpty(_username))
{
// We don't have a tenantId, check environment variables
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("COG_USER")))
{
_username = Environment.GetEnvironmentVariable("COG_USER");
}
else
{
throw new ArgumentException(message: "Parameter 'userName' not provided!");
}
}
if (string.IsNullOrEmpty(_password))
{
// We don't have a tenantId, check environment variables
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("COG_PASS")))
{
_password = Environment.GetEnvironmentVariable("COG_PASS");
}
else
{
throw new ArgumentException(message: "Parameter 'password' is not provided!");
}
}
}
}
/// <summary>
/// Uploads media to the Cogniac system
/// </summary>
/// <param name="forceSet">One of "training" or "validation"</param>
/// <param name="fileName">File to upload</param>
/// <param name="mediaTimestamp">Media time stamp</param>
/// <param name="forceOverwrite">Force overwrite flag</param>
/// <param name="metaTags">List of meta tags</param>
/// <param name="isPublic">Public flag</param>
/// <param name="externalMediaId">External media ID</param>
/// <param name="originalUrl">Original URL</param>
/// <param name="originalLandingUrl">Original landing URL</param>
/// <param name="license">License string</param>
/// <param name="authorProfileUrl">Author profile URL</param>
/// <param name="title">Title of media</param>
/// <param name="sourceUrl">Source URL</param>
/// <param name="previewUrl">Preview URL</param>
/// <param name="domainUnit">(E.G. serial number) for set assignment grouping</param>"
/// <param name="localGatewayUrl">Local gateway URL</param>
/// <returns>Cogniac.Media object</returns>
public Media UploadMedia
(
string forceSet = null,
string fileName = null,
double mediaTimestamp = 0,
bool forceOverwrite = true,
string[] metaTags = null,
bool isPublic = false,
string externalMediaId = null,
string originalUrl = null,
string originalLandingUrl = null,
string license = null,
string authorProfileUrl = null,
string title = null,
string sourceUrl = null,
string previewUrl = null,
string domainUnit = null,
string localGatewayUrl = null
)
{
if (!string.IsNullOrEmpty(_providedToken))
{
byte[] fileBytes;
if (!String.IsNullOrEmpty(sourceUrl))
{
fileBytes = Helpers.GetFileBytesContent(sourceUrl);
fileName = sourceUrl;
}
else
{
if (File.Exists(fileName))
{
fileBytes = Helpers.GetFileBytesContent(fileName);
}
else
{
throw new ArgumentException(message: "File does not exist: '" + fileName + "'");
}
}
if (String.IsNullOrEmpty(fileName) && String.IsNullOrEmpty(sourceUrl))
{
throw new ArgumentException(message: "No input file provided (fileName or sourceUrl");
}
string mediaFormat = Path.GetExtension(fileName);
mediaFormat = mediaFormat.Replace(".", string.Empty);
Dictionary<string, object> dict = new Dictionary<string, object>
{
{ "media_src", "c_sharp_sdk" },
{ "media_fomat", mediaFormat },
{ "filename", fileName },
{ "force_overwrite", forceOverwrite },
{ "public", isPublic },
};
if (mediaTimestamp > 0)
{
dict.Add("media_timestamp", mediaTimestamp);
}
if (!String.IsNullOrEmpty(forceSet))
{
if (forceSet.ToLower().Equals("training"))
{
dict.Add("force_set", "training");
}
else if (forceSet.ToLower().Equals("validation"))
{
dict.Add("force_set", "validation");
}
}
if (!(metaTags == null))
{
dict.Add("meta_tags", Helpers.FlattenStringArray(metaTags));
}
if (!String.IsNullOrEmpty(externalMediaId))
{
dict.Add("external_media_id", externalMediaId);
}
if (!String.IsNullOrEmpty(originalUrl))
{
dict.Add("original_url", originalUrl);
}
if (!String.IsNullOrEmpty(originalLandingUrl))
{
dict.Add("original_landing_url", originalLandingUrl);
}
if (!String.IsNullOrEmpty(license))
{
dict.Add("license", license);
}
if (!String.IsNullOrEmpty(authorProfileUrl))
{
dict.Add("author_profile_url", authorProfileUrl);
}
if (!String.IsNullOrEmpty(title))
{
dict.Add("title", title);
}
if (!String.IsNullOrEmpty(previewUrl))
{
dict.Add("preview_url", previewUrl);
}
if (!String.IsNullOrEmpty(sourceUrl))
{
dict.Add("source_url", sourceUrl);
}
if (!String.IsNullOrEmpty(domainUnit))
{
dict.Add("domain_unit", domainUnit);
}
string data = Helpers.MapToQueryString(dict);
var request = new RestRequest(Method.POST)
{
AlwaysMultipartFormData = true
};
request.AddFile("fileData", fileBytes, fileName);
IRestResponse response;
if (string.IsNullOrEmpty(localGatewayUrl))
{
response = ExecuteRequest($"{_urlPrefix}/media?{data}", request);
}
else
{
response = ExecuteRequest($"{localGatewayUrl}/media?{data}", request);
}
if ((response.StatusCode == HttpStatusCode.OK) && (response.IsSuccessful == true))
{
return Media.FromJson(response.Content);
}
else
{
throw new WebException(message: "Network error: " + response.Content);
}
}
else
{
throw new ArgumentException(message: "Access token is null or empty");
}
}
/// <summary>
/// Deletes a media file form the Cogniac system given a known media ID
/// </summary>
/// <param name="mediaId">Media ID</param>
/// <param name="localGatewayUrl">Local gateway URL</param>
/// <returns>True on success</returns>
public bool DeleteMedia(string mediaId, string localGatewayUrl = null)
{
string fullUrl = "";
if (!string.IsNullOrEmpty(mediaId))
{
if (string.IsNullOrEmpty(localGatewayUrl))
{
fullUrl = $"{_urlPrefix}/media/{mediaId}";
}
else
{
if (localGatewayUrl.EndsWith("/"))
{
fullUrl = $"{localGatewayUrl}media/{mediaId}";
}
else
{
fullUrl = $"{localGatewayUrl}/media/{mediaId}";
}
}
var request = new RestRequest(Method.DELETE);
var response = ExecuteRequest(fullUrl, request);
if (response.IsSuccessful && (response.StatusCode == HttpStatusCode.NoContent))
{
return true;
}
else
{
throw new WebException(message: "Error deleting media: " + response.Content);
}
}
else
{
throw new ArgumentException(message: "mediaId parameter is not provided.");
}
}
/// <summary>
/// Gets all subjects associated with a given tenant
/// </summary>
/// <param name="tenantId">Tenant ID</param>
/// <returns>Cogniac.Subjects object</returns>
public Subjects GetAllSubjects(string tenantId)
{
if (!String.IsNullOrEmpty(tenantId))
{
string fullUrl = $"{_urlPrefix}/tenants/{tenantId}/subjects";
var request = new RestRequest(Method.GET);
var response = ExecuteRequest(fullUrl, request);
if (response.IsSuccessful && (response.StatusCode == HttpStatusCode.OK))
{
return Subjects.FromJson(response.Content);
}
else
{
throw new WebException(message: "Network error while getting all subjects: " + response.Content);
}
}
else
{
throw new ArgumentException(message: "The tenant ID provided is null or empty.");
}
}
/// <summary>
/// Gets a subject given a subject UID
/// </summary>
/// <param name="subjectUid">Subject UID</param>
/// <returns>Cogniac.Subject object</returns>
public Subject GetSubject(string subjectUid)
{
if (!String.IsNullOrEmpty(subjectUid))
{
string fullUrl = $"{_urlPrefix}/subjects/{subjectUid}";
var request = new RestRequest(Method.GET);
var response = ExecuteRequest(fullUrl, request);
if (response.IsSuccessful && (response.StatusCode == HttpStatusCode.OK))
{
return Subject.FromJson(response.Content);
}
else
{
throw new WebException(message: "Network error while getting subjects: " + response.Content);
}
}
else
{
throw new ArgumentException(message: "The subject UID provided is null or empty.");
}
}
/// <summary>
/// Gets a list of applications given a tenant ID
/// </summary>
/// <param name="tenantId">Tenant ID</param>
/// <returns>Cogniac.Applications object</returns>
public Applications GetAllApplications(string tenantId)
{
if (!String.IsNullOrEmpty(tenantId))
{
string fullUrl = $"{_urlPrefix}/tenants/{tenantId}/applications";
var request = new RestRequest(Method.GET);
var response = ExecuteRequest(fullUrl, request);
if (response.IsSuccessful && (response.StatusCode == HttpStatusCode.OK))
{
return Applications.FromJson(response.Content);
}
else
{
throw new WebException(message: "Network error while getting all applications: " + response.Content);
}
}
else
{
throw new ArgumentException(message: "The tenant ID provided is null or empty.");
}
}
/// <summary>
/// Gets an applicatoin given an application ID
/// </summary>
/// <param name="applicationId">Application ID</param>
/// <returns>Cogniac.Application object</returns>
public Application GetApplication(string applicationId)
{
if (!String.IsNullOrEmpty(applicationId))
{
string fullUrl = $"{_urlPrefix}/applications/{applicationId}";
var request = new RestRequest(Method.GET);
var response = ExecuteRequest(fullUrl, request);
if (response.IsSuccessful && (response.StatusCode == HttpStatusCode.OK))
{
return Application.FromJson(response.Content);
}
else
{
throw new WebException(message: "Network error while getting application: " + response.Content);
}
}
else
{
throw new ArgumentException(message: "The application ID provided is null or empty.");
}
}
/// <summary>
/// Gets a tenant given a tenant ID
/// </summary>
/// <param name="tenantId">Tenant ID</param>
/// <returns>Cogniac.Tenant object</returns>
public Tenant GetTenant(string tenantId)
{
if (!String.IsNullOrEmpty(tenantId))
{
string fullUrl = $"{_urlPrefix}/tenants/{tenantId}";
var request = new RestRequest(Method.GET);
var response = ExecuteRequest(fullUrl, request);
if (response.IsSuccessful && (response.StatusCode == HttpStatusCode.OK))
{
return Tenant.FromJson(response.Content);
}
else
{
throw new WebException(message: "Network error while getting tenant: " + response.Content);
}
}
else
{
throw new ArgumentException(message: "The tenant ID provided is null or empty.");
}
}
/// <summary>
/// Associates a given media file with an exiting subject
/// </summary>
/// <param name="mediaId">Media ID</param>
/// <param name="subjectUid">Subject UID</param>
/// <param name="forceFeedback">Fore feedback flag</param>
/// <param name="enableWaitResult">Enable wait flag</param>
/// <param name="probability">Association probability, only valid if consensus is null</param>
/// <param name="consensus">Consensus value</param>
/// <returns>Cogniac.CaptureId object</returns>
public CaptureId AssociateMediaToSubject(string mediaId, string subjectUid, bool forceFeedback = false, bool enableWaitResult = false,
double? probability = null, bool? consensus = null)
{
if (string.IsNullOrEmpty(mediaId) || string.IsNullOrEmpty(subjectUid))
{
throw new ArgumentException(message: "Provided media ID or subject UID are is or empty.");
}
else
{
Dictionary<string, object> dict = new Dictionary<string, object>
{
{ "media_id", mediaId },
{ "force_feedback", forceFeedback },
{ "enable_wait_result", enableWaitResult }
};
// Perform some logic for the probability
if (consensus == null)
{
if (probability == null)
{
probability = 0.99;
}
// Insert it into the API call
dict.AddIfNotNull("uncal_prob", probability);
}
else
{
dict.AddIfNotNull("consensus", consensus);
}
var request = new RestRequest(Method.POST);
string data = Helpers.MapToQueryString(dict);
IRestResponse response = ExecuteRequest($"{_urlPrefix}/subjects/{subjectUid}/media?{data}", request);
if ((response.StatusCode == HttpStatusCode.OK) && (response.IsSuccessful == true))
{
return CaptureId.FromJson(response.Content);
}
else
{
throw new WebException(message: "Network error: " + response.Content);
}
}
}
/// <summary>
/// Gets media given a media ID
/// </summary>
/// <param name="mediaId">Media ID</param>
/// <returns>Cogniac.Media object</returns>
public Media GetMedia(string mediaId)
{
if (!String.IsNullOrEmpty(mediaId))
{
string fullUrl = $"{_urlPrefix}/media/{mediaId}";
var request = new RestRequest(Method.GET);
var response = ExecuteRequest(fullUrl, request);
if (response.IsSuccessful && (response.StatusCode == HttpStatusCode.OK))
{
return Media.FromJson(response.Content);
}
else
{
throw new WebException(message: "Network error while getting media: " + response.Content);
}
}
else
{
throw new ArgumentException(message: "The media ID provided is null or empty.");
}
}
/// <summary>
/// Creates an application in the Cogniac system
/// </summary>
/// <param name="name">Application name</param>
/// <param name="type">Application type</param>
/// <param name="description">Description of application</param>
/// <param name="inputSubjects">List of input subjects</param>
/// <param name="outputSubjects">List of output subjects</param>
/// <param name="releaseMetrics">Release metrics</param>
/// <param name="detectionThresholds">Detection thresholds</param>
/// <param name="detectionPostUrls">Detection post URL's</param>
/// <param name="gatewayPostUrls">Gateway post URL's</param>
/// <param name="active">Active flag</param>
/// <param name="requestedFeedbackPerHour">Requested feedback per hour value</param>
/// <param name="refreshFeedback">Refresh feedback flag</param>
/// <param name="appManagers">List of application managers</param>
/// <returns>Cogniac.Application object</returns>
public Application CreateApplication
(
string name,
string type,
string description = null,
string[] inputSubjects = null,
string[] outputSubjects = null,
string releaseMetrics = null,
Dictionary<string, string> detectionThresholds = null,
string[] detectionPostUrls = null,
string[] gatewayPostUrls = null,
bool active = false,
int? requestedFeedbackPerHour = null,
bool refreshFeedback = false,
string[] appManagers = null
)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException("message", nameof(name));
}
if (string.IsNullOrEmpty(type))
{
throw new ArgumentException("message", nameof(type));
}
Dictionary<string, object> dict = new Dictionary<string, object>
{
{ "name", name },
{ "type", type },
};
dict.AddIfNotNull("description", description);
dict.AddIfNotNull("input_subjects", Helpers.FlattenStringArray(inputSubjects));
dict.AddIfNotNull("output_subjects", Helpers.FlattenStringArray(outputSubjects));
dict.AddIfNotNull("release_metrics", releaseMetrics);
dict.AddIfNotNull("detection_post_urls", Helpers.FlattenStringArray(detectionPostUrls));
dict.AddIfNotNull("gateway_post_urls", Helpers.FlattenStringArray(gatewayPostUrls));
dict.AddIfNotNull("active", active);
dict.AddIfNotNull("requested_feedback_per_hour", requestedFeedbackPerHour);
dict.AddIfNotNull("refresh_feedback", refreshFeedback);
dict.AddIfNotNull("app_managers", Helpers.FlattenStringArray(appManagers));
if (detectionThresholds != null)
{
dict.AddIfNotNull("detection_thresholds", detectionThresholds.ToJson());
}
string data = Helpers.MapToQueryString(dict);
var request = new RestRequest(Method.POST)
{
AlwaysMultipartFormData = true
};
string fullUrl = $"{_urlPrefix}/applications?{data}";
var response = ExecuteRequest(fullUrl, request);
if (response.IsSuccessful && (response.StatusCode == HttpStatusCode.OK))
{
return Application.FromJson(response.Content);
}
else
{
throw new WebException(message: "Network error while creating application: " + response.Content);
}
}
/// <summary>
/// Creates a subject in the Cogniac system
/// </summary>
/// <param name="name">Subject name</param>
/// <param name="description">Subject description</param>
/// <param name="publicRead">Public read flag</param>
/// <param name="publicWrite">Public write flag</param>
/// <returns>Cogniac.Subject object</returns>
public Subject CreateSubject
(
string name,
string description = null,
bool? publicRead = null,
bool? publicWrite = null
)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException("message", nameof(name));
}
Dictionary<string, object> dict = new Dictionary<string, object>
{
{ "name", name }
};
dict.AddIfNotNull("description", description);
dict.AddIfNotNull("public_read", publicRead);
dict.AddIfNotNull("public_write", publicWrite);
string data = Helpers.MapToQueryString(dict);
var request = new RestRequest(Method.POST)
{
AlwaysMultipartFormData = true
};
string fullUrl = $"{_urlPrefix}/subjects?{data}";
var response = ExecuteRequest(fullUrl, request);
if (response.IsSuccessful && (response.StatusCode == HttpStatusCode.OK))
{
return Subject.FromJson(response.Content);
}
else
{
throw new WebException(message: "Network error while creating subject: " + response.Content);
}
}
/// <summary>
/// Delete a subject from the Cogniac system
/// </summary>
/// <param name="subjectUid">Subject UID</param>
/// <returns>True on success</returns>
public bool DeleteSubject(string subjectUid)
{
if (string.IsNullOrEmpty(subjectUid))
{
throw new ArgumentException("message", nameof(subjectUid));
}
string fullUrl = $"{_urlPrefix}/subjects/{subjectUid}";
var request = new RestRequest(Method.DELETE);
var response = ExecuteRequest(fullUrl, request);
if (response.IsSuccessful && (response.StatusCode == HttpStatusCode.NoContent))
{
return true;
}
else
{
throw new WebException(message: "Error deleting subject: " + response.Content );
}
}
/// <summary>
/// Gets the subject media associations given a subject UID
/// </summary>
/// <param name="subjectUid">Subject UID</param>
/// <returns>Cogniac.SubjectMediaAssociations object</returns>
public SubjectMediaAssociations GetSubjectMediaAssociations(string subjectUid)
{
if (!String.IsNullOrEmpty(subjectUid))
{
string fullUrl = $"{_urlPrefix}/subjects/{subjectUid}/media";
var request = new RestRequest(Method.GET);
var response = ExecuteRequest(fullUrl, request);
if (response.IsSuccessful && (response.StatusCode == HttpStatusCode.OK))
{
return SubjectMediaAssociations.FromJson(response.Content);
}
else
{
throw new WebException(message: "Network error while getting subject media associations: " + response.Content);
}
}
else
{
throw new ArgumentException(message: "The subject UID provided is null or empty.");
}
}
/// <summary>
/// Get the subjects associated with a given media
/// </summary>
/// <param name="mediaId">Media ID</param>
/// <returns>Cogniac.MediaSubjects</returns>
public MediaSubjects GetMediaSubjects(string mediaId)
{
if (!String.IsNullOrEmpty(mediaId))
{
string fullUrl = $"{_urlPrefix}/media/{mediaId}/subjects";
var request = new RestRequest(Method.GET);
var response = ExecuteRequest(fullUrl, request);
if (response.IsSuccessful && (response.StatusCode == HttpStatusCode.OK))
{
return MediaSubjects.FromJson(response.Content);
}
else
{
throw new WebException(message: "Network error while getting media subjects: " + response.Content);
}
}
else
{
throw new ArgumentException(message: "The media ID provided is null or empty.");
}
}
/// <summary>
/// Get the detections associated with a media
/// </summary>
/// <param name="mediaId">Media ID</param>
/// <param name="waitCaptureId">Wait capture ID</param>
/// <returns>Cogniac.MediaDetections</returns>
public MediaDetections GetMediaDetections(string mediaId, string waitCaptureId = null)
{
if (!String.IsNullOrEmpty(mediaId))
{
string fullUrl = "";
if (string.IsNullOrEmpty(waitCaptureId))
{
fullUrl = $"{_urlPrefix}/media/{mediaId}/detections";
}
else
{
fullUrl = $"{_urlPrefix}/media/{mediaId}/detections?wait_capture_id={waitCaptureId}";
}
var request = new RestRequest(Method.GET);
var response = ExecuteRequest(fullUrl, request);
if (response.IsSuccessful && (response.StatusCode == HttpStatusCode.OK))
{
return MediaDetections.FromJson(response.Content);
}
else
{
throw new WebException(message: "Network error while getting media detections: " + response.Content);
}
}
else
{
throw new ArgumentException(message: "The media ID provided is null or empty.");
}
}
/// <summary>
/// Update a given tenant
/// </summary>
/// <param name="tenantId">Tenant ID</param>
/// <param name="name">Tenant name</param>
/// <param name="description">Tenant description</param>
/// <param name="azureSasTokens">Tenant Azure SAS token</param>
/// <returns>Cogniac.Tenant</returns>
public Tenant UpdateTenant
(
string tenantId,
string name = null,
string description = null,
string azureSasTokens = null
)
{
if (string.IsNullOrEmpty(tenantId))
{
throw new ArgumentException("message", nameof(tenantId));
}
Dictionary<string, object> dict = new Dictionary<string, object> { };
dict.AddIfNotNull("name", name);
dict.AddIfNotNull("description", description);
dict.AddIfNotNull("azure_sas_tokens", azureSasTokens);
string data = Helpers.MapToQueryString(dict);
var request = new RestRequest(Method.POST)
{
AlwaysMultipartFormData = true
};
string fullUrl = $"{_urlPrefix}/tenants/{tenantId}?{data}";
var response = ExecuteRequest(fullUrl, request);
if (response.IsSuccessful && (response.StatusCode == HttpStatusCode.OK))
{
return Tenant.FromJson(response.Content);
}
else
{
throw new WebException(message: "Network error while updating tenant: " + response.Content);
}
}
} // End of class
}