-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
665 lines (565 loc) · 30.7 KB
/
Program.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
using System.Runtime.InteropServices;
using System.Collections.ObjectModel;
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using System.Threading;
using System.Collections.Generic;
using System.Net;
using System.Linq;
using Newtonsoft.Json;
using Skylight.Client;
using Skylight.Mqtt;
using Skylight.FileClient;
using Skylight.Utilities;
using Skylight.Utilities.Extensions;
using Skylight.Utilities.Requests;
using Skylight.Utilities.Responses;
using Skylight.Api.Assignments.V1.AssignmentRequests;
using Skylight.Api.Assignments.V1.SequenceRequests;
using Skylight.Api.Assignments.V1.CardRequests;
using Skylight.Api.Assignments.V1.Models;
using Skylight.Api.Authentication.V1.Models;
using Skylight.Api.Authentication.V1.GroupsRequests;
using Skylight.Api.Authentication.V1.UsersRequests;
using Skylight.Api.Messaging.V1.Models;
using Skylight.Api.Messaging.V1.NotificationsRequests;
using log4net;
namespace Skylight.Sdk
{
public class Manager
{
public class SkylightApiClient : ApiClient {
private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private readonly int _maxApiPayloadSize;
private static Random random = new Random();
public SkylightApiClient(ConnectionInfo connectionInfo, int MaxApiPayloadSize) : base(connectionInfo)
{
_maxApiPayloadSize = MaxApiPayloadSize;
}
public static string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
private int GetEstimatedPayloadSize(object payload)
{
var serialized = JsonConvert.SerializeObject(payload);
//This is technically 2 times bigger than the actual, given that our payloads send as UTF-8 (1 byte per char) and C# uses UTF-16 (2 bytes per char)
return serialized.Length * sizeof(char);
}
/// <summary>
/// Create an assignment.
/// </summary>
/// <param name="assignment">assignment to create</param>
/// <returns>Created assignment</returns>
private async Task<Assignment> CreateAssignment(AssignmentNew assignment)
{
var request = new CreateAssignmentRequest(assignment);
var response = await base.ExecuteRequestAsync(request);
return response.Content;
}
/// <summary>
/// This method should be used for creating assignment that exceeds API maximum payload size
/// It will split creating of the assignment in multiple parts
/// In order to use this method, this Manager should be provided with maximum payload size that API supports
/// </summary>
/// <param name="assignment">Assignment model to create</param>
/// <returns>Created assignment</returns>
/// <exception cref="ArgumentNullException">Thrown when assignment model is null</exception>
/// <exception cref="ArgumentException">Thrown when maximum payload size that API supports was not provided</exception>
private async Task<Assignment> CreateAssignmentInMultipleRequests(AssignmentNew assignment)
{
if (assignment == null)
throw new ArgumentNullException(nameof(assignment));
var payloadSize = GetEstimatedPayloadSize(assignment);
Logger.Debug($"Estimated payload size = {payloadSize}, Max payload size = {_maxApiPayloadSize}");
if (payloadSize >= _maxApiPayloadSize)
{
var sequences = assignment.Sequences.ToList();
assignment.Sequences = new List<SequenceNew>();
var createdAssignment = await CreateAssignment(assignment);
Logger.Debug($"Created assignment {createdAssignment.Id} with no sequences.");
await ProcessSequencesCreation(createdAssignment.Id, sequences);
return createdAssignment;
}
return await CreateAssignment(assignment);
}
private async Task ProcessSequencesCreation(string assignmentId, List<SequenceNew> sequences)
{
if (sequences == null)
throw new ArgumentNullException(nameof(sequences));
Logger.Debug($"ProcessSequencesCreation({assignmentId}, sequences)");
// filter sequences that fit max payload size to create groups of them
var payloadFitSequences = sequences.Where(sequence => GetEstimatedPayloadSize(sequence) < _maxApiPayloadSize).ToList();
if (payloadFitSequences.Any())
{
// split sequences into groups
var groups = PackFitSequencesIntoGroups(payloadFitSequences).ToList();
var groupsCreationTasks = groups.Select(group => CreateSequences(assignmentId, group));
await Task.WhenAll(groupsCreationTasks);
}
var sequenceCreationTasks = sequences.Except(payloadFitSequences).Select(async sequence =>
{
var sequenceCards = sequence.Cards.ToList();
sequence.Cards = new List<CardNew>();
var createdSequence = await CreateSequence(assignmentId, sequence);
await ProcessSequenceCardsCreation(assignmentId, createdSequence.Id, sequenceCards);
});
await Task.WhenAll(sequenceCreationTasks);
}
/// <summary>
/// Create sequences
/// </summary>
/// <param name="assignmentId">Id of the assignment</param>
/// <param name="sequences">List of sequences</param>
public async Task<IEnumerable<Sequence>> CreateSequences(string assignmentId, List<SequenceNew> sequences)
{
string threadId = RandomString(10);
Logger.Debug($"[{threadId}] CreateSequences({assignmentId},{sequences?.Count} sequences)");
foreach (var seq in sequences)
{
Logger.Debug($"[{threadId}] new sequence {seq.Id}");
}
//cross-check new sequences with existing assignment sequences
List<string> sequenceIds = sequences.Select(s => s.Id).ToList();
GetAssignmentSequencesRequest assignmentRequest = new GetAssignmentSequencesRequest(assignmentId);
var assignmentResponse = await base.ExecuteRequestAsync(assignmentRequest);
//check to see if assignment already contains any of these sequences
var assignmentSequences = assignmentResponse.Content;
Logger.Debug($"[{threadId}] Assignment {assignmentId} currently has {assignmentSequences.Count} sequences. Cross-checking for duplicates in new sequences.");
var duplicate = assignmentSequences.FirstOrDefault(x => sequenceIds.Contains(x.Id));
if (duplicate != null)
{
Logger.Error($"[{threadId}] CreateSequences: precheck -- Assignment {assignmentId} already contains sequence with id {duplicate.Id}");
}
try
{
var request = new CreateSequencesRequest(sequences, assignmentId);
var response = await base.ExecuteRequestAsync(request);
var createdSequences = response.Content;
return createdSequences;
}
catch (Exception ex)
{
Logger.Error($"[{threadId}] CreateSequences error. Ex = {ex.Message}");
GetAssignmentSequencesRequest request = new GetAssignmentSequencesRequest(assignmentId);
var response = await base.ExecuteRequestAsync(request);
//check to see if assignment already contains any of these sequences
var assignmentSequences2 = response.Content;
Logger.Error($"[{threadId}] CreateSequences: after exception -- Assignment {assignmentId} currently has {assignmentSequences2.Count} sequences.");
var duplicate2 = assignmentSequences2.FirstOrDefault(x => sequenceIds.Contains(x.Id));
if (duplicate2 != null)
{
Logger.Error($"[{threadId}] CreateSequences: after exception -- Assignment {assignmentId} already contains sequence with id {duplicate2.Id}");
}
throw ex;
}
}
/// <summary>
/// Create single sequence.
/// </summary>
/// <param name="assignmentId">Id of the assignment</param>
/// <param name="sequence">sequence to create</param>
/// <returns>Created sequence</returns>
public async Task<Sequence> CreateSequence(string assignmentId, SequenceNew sequence)
{
//cross-check new sequences with existing assignment sequences
GetAssignmentSequencesRequest assignmentRequest = new GetAssignmentSequencesRequest(assignmentId);
var assignmentResponse = await base.ExecuteRequestAsync(assignmentRequest);
//check to see if assignment already contains any of these sequences
var assignmentSequences = assignmentResponse.Content;
string threadId = RandomString(10);
Logger.Debug($"[{threadId}] CreateSequence: Assignment {assignmentId} currently has {assignmentSequences.Count} sequences. Cross-checking for duplicate of new sequence.");
var duplicate = assignmentSequences.FirstOrDefault(x => x.Id == sequence.Id);
if (duplicate != null)
{
Logger.Error($"[{threadId}] CreateSequence: precheck -- Assignment {assignmentId} already contains sequence with id {duplicate.Id}");
}
try
{
var request = new CreateSequenceRequest(sequence, assignmentId);
var response = await base.ExecuteRequestAsync(request);
return response.Content;
}
catch (Exception ex)
{
Logger.Error($"CreateSequence error. Ex = {ex.Message}");
//cross-check new sequences with existing assignment sequences
GetAssignmentSequencesRequest request = new GetAssignmentSequencesRequest(assignmentId);
var response = await base.ExecuteRequestAsync(request);
//check to see if assignment already contains any of these sequences
var assignmentSequences2 = response.Content;
Logger.Error($"[{threadId}] CreateSequence: after exception -- Assignment {assignmentId} currently has {assignmentSequences2.Count} sequences. Cross-checking for duplicates.");
var duplicate2 = assignmentSequences2.FirstOrDefault(x => x.Id == sequence.Id);
if (duplicate2 != null)
{
Logger.Error($"[{threadId}] CreateSequence: after exception -- Assignment {assignmentId} already contains sequence with id {duplicate2.Id}");
}
throw ex;
}
}
private IEnumerable<List<SequenceNew>> PackFitSequencesIntoGroups(List<SequenceNew> sequences)
{
var maxGroupsCount = sequences.Count;
var groupPayloadSizes = Enumerable.Repeat(_maxApiPayloadSize, maxGroupsCount).ToArray();
// max groups count is equal to sequences count
var groups = new List<List<SequenceNew>>();
sequences.ForEach(_ => groups.Add(new List<SequenceNew>()));
// sort sequences in descending payload size order by to get grouping much more effective
sequences.Sort((seq1, seq2) => GetEstimatedPayloadSize(seq2).CompareTo(GetEstimatedPayloadSize(seq1)));
for (var sequence = 0; sequence < sequences.Count; ++sequence)
{
for (var group = 0; group < maxGroupsCount; ++group)
{
var payloadSize = GetEstimatedPayloadSize(sequences[sequence]);
if (groupPayloadSizes[group] - payloadSize >= 0)
{
groups[group].Add(sequences[sequence]);
groupPayloadSizes[group] -= payloadSize;
break;
}
}
}
return groups.Where(group => group.Count > 0);
}
private async Task ProcessSequenceCardsCreation(string assignmentId, string sequenceId, List<CardNew> cards)
{
if (cards == null)
throw new ArgumentNullException(nameof(cards));
var creationTasks = new List<Task>();
await ProcessCardsCreation(assignmentId, sequenceId, cards, creationTasks);
await Task.WhenAll(creationTasks);
}
private async Task ProcessCardsCreation(string assignmentId, string sequenceId, List<CardNew> cards, List<Task> cardCreationTasks)
{
var firstHalf = cards.Take((cards.Count + 1) / 2).ToList();
var firstHalfSize = GetEstimatedPayloadSize(firstHalf);
var secondHalf = cards.Skip((cards.Count + 1) / 2).ToList();
var secondHalfSize = GetEstimatedPayloadSize(secondHalf);
if (firstHalfSize < _maxApiPayloadSize)
{
cardCreationTasks.Add(CreateCards(assignmentId, sequenceId, firstHalf));
}
else
{
await ProcessCardsCreation(assignmentId, sequenceId, firstHalf, cardCreationTasks);
}
if (secondHalfSize < _maxApiPayloadSize)
{
cardCreationTasks.Add(CreateCards(assignmentId, sequenceId, secondHalf));
}
else
{
await ProcessCardsCreation(assignmentId, sequenceId, secondHalf, cardCreationTasks);
}
}
/// <summary>
/// Create cards.
/// </summary>
/// <param name="assignmentId">Id of the assignment</param>
/// <param name="sequenceId">Id of the sequence</param>
/// <param name="cards">List of cards</param>
/// <returns>List of created cards</returns>
public async Task<IEnumerable<Card>> CreateCards(string assignmentId, string sequenceId, List<CardNew> cards)
{
var request = new CreateCardsRequest(cards, assignmentId, sequenceId);
var response = await base.ExecuteRequestAsync(request);
return response.Content;
}
/// <summary>
/// Executes an ApiRequest
/// </summary>
/// <typeparam name="TResult">The type of response to send back specified by the ApiRequest</typeparam>
/// <param name="request">The ApiRequest to execute</param>
/// <returns>An ApiResponse with Content of type TResult</returns>
/// <exception cref="ApiException">Thrown if there was an error in the request</exception>
public new async Task<ApiResponse<TResult>> ExecuteRequestAsync<TResult>(ApiRequest<TResult> request)
{
if(request.Payload != null && request.Payload.Content is AssignmentNew assignmentNew) {
var assignment = await CreateAssignmentInMultipleRequests(assignmentNew);
if(assignment is TResult assignmentResult) {
return new ApiResponse<TResult>(HttpStatusCode.Created, assignmentResult);
}
}
return await base.ExecuteRequestAsync(request);
}
}
private SkylightApiClient _apiClient;
public SkylightApiClient ApiClient {
get {
if(_apiClient == null) throw new Exception("ApiClient is null, please make sure Connect() is called right after instantiating the Manager.");
return _apiClient;
}
}
private MessagingClient _messagingClient;
public MessagingClient MessagingClient {
get {
if(_messagingClient == null) throw new Exception("MessagingClient is null, please make sure Connect() is called right after instantiating the Manager.");
return _messagingClient;
}
}
private FileClient.FileTransferClient _mediaClient;
public FileClient.FileTransferClient MediaClient {
get {
if(_mediaClient == null) throw new Exception("MediaClient is null, please make sure Connect() is called right after instantiating the Manager.");
return _mediaClient;
}
}
public string IntegrationId;
public string Domain;
public string ApiUrl;
public string MqttUrl;
public string Username;
public string Password;
private dynamic Credentials;
private static ConnectionType MqttConnectionType = ConnectionType.Auto;
private static bool Connected = false;
private static Timer MqttCheckTimer;
private static int MqttCheckTimerIntervalInMs = 10000;
private static bool ReceivedMqttCheckForInterval = true;
/// <summary>
/// Maximum payload size that API supports (in bytes)
/// </summary>
private static int MaxApiPayloadSize = 7 * 1024 * 1024;
public Manager(string credentialsPath = "credentials.json") {
var potentialCredentialsPaths = new String[]{credentialsPath, "credentials.json", Path.Combine("config", "credentials.json")};
var successfullyReadCredentials = false;
foreach(var potentialCredentialPath in potentialCredentialsPaths){
successfullyReadCredentials = this.ReadCredentials(potentialCredentialPath);
if(successfullyReadCredentials)break;
}
if(!successfullyReadCredentials) {
Console.Error.WriteLine("Please ensure the credentials.json path points to a file with valid Skylight API credentials.");
throw new Exception("Credentials Error");
}
var mqttUrl = (string)Credentials.mqttUrl;
mqttUrl = mqttUrl.Substring(mqttUrl.IndexOf("://") + 3);
this.Setup((string)Credentials.id, (string)Credentials.username, (string)Credentials.password, (string)Credentials.domain, (string)Credentials.apiUrl, mqttUrl);
}
public Manager(string integrationId, string username, string password, string domain, string apiUrl, string mqttUrl) {
this.Setup(integrationId, username, password, domain, apiUrl, mqttUrl);
}
private void Setup(string integrationId, string username, string password, string domain, string apiUrl, string mqttUrl) {
//Set our integration id
IntegrationId = integrationId;
//Set our API Url
if(apiUrl.EndsWith("/"))apiUrl = apiUrl.Substring(0, apiUrl.Length-1);
Console.WriteLine(apiUrl);
ApiUrl = apiUrl;
//Set our Mqtt Url
MqttUrl = mqttUrl;
//Set our username
Username = username;
//Set our password
Password = password;
//Set our domain
Domain = domain;
}
public async Task Connect() {
//Set up a new connection
var connection = new ConnectionInfo(Username, Password, Domain, ApiUrl);
//Use the connection to create a client
_apiClient = new SkylightApiClient(connection, MaxApiPayloadSize);
//Test our connection
await TestConnection();
//Set up a new MQTT connection
var mqttConnection = new MqttConnectionInfo(Username, Password, Domain, ApiUrl, MqttUrl, 30, MqttConnectionType);
//Use the MQTT connection information to create a messaging client
_messagingClient = new MessagingClient(mqttConnection);
MessagingClient.MessageReceived += (sender, e) => {
if(!e.Topic.EndsWith("notifications"))return;
dynamic messageData = JsonConvert.DeserializeObject(e.Message);
if(!((string)messageData.to).Equals(IntegrationId))return;
if(!((string)messageData.from).Equals(IntegrationId))return;
ReceivedMqttCheckForInterval = true;
};
//Use our API client to create a media client
_mediaClient = new FileTransferClient(_apiClient, MaxApiPayloadSize);
Connected = true;
}
private async Task TestConnection() {
try {
await _apiClient.ExecuteRequestAsync(new Skylight.Api.Assignments.V1.APIRequests.GetApiRequest());
} catch (Exception e) {
Console.WriteLine("Connection to Skylight Web API failed. Please check that the username, password, and API URL are valid and that the extension can reach the Skylight server.");
throw e;//new Exception("Connection to Skylight Web API failed. Please check that the username, password, and API URL are valid and that the extension can reach the Skylight server.");
}
}
public async Task StartListening() {
var mqttSuccess = await MessagingClient.StartListeningAsync();
//Start our check timer
MqttCheckTimer = new Timer(async (o) => {
if(MqttCheckTimer == null) return; //If the timer is null, we've stopped listening
if(!ReceivedMqttCheckForInterval) {
//We've missed a packet -- perform stop start here
Console.WriteLine("MQTT disconnect detected, performing reconnect.");
await StopListening();
await StartListening();
}
else {
ReceivedMqttCheckForInterval = false;
try {
await SendNotification(IntegrationId, "mqttcheck", 0);
} catch(Exception e) {
}
}
}, null, 0, MqttCheckTimerIntervalInMs);
if(!mqttSuccess) {
Console.WriteLine("MQTT connection failed. Attempting reconnect, but please check that the MQTT URL is valid and the server is reachable by the extension.");
await StopListening();
await StartListening();
}
}
public async Task StopListening() {
if(MqttCheckTimer != null) {
MqttCheckTimer.Dispose();
MqttCheckTimer = null;
}
ReceivedMqttCheckForInterval = true;
await MessagingClient.CleanUp();
}
public static void SetMqttConnectionType(ConnectionType type) {
if(Connected) throw new Exception("Please call SetMqttConnectionType before calling Connect.");
MqttConnectionType = type;
}
public static void SetMaxApiPayloadSize(int size) {
if(Connected) throw new Exception("Please call SetMaxApiPayloadSize before calling Connect.");
if (size <= 0)
throw new ArgumentException("The maximum payload size should be greater than 0.");
MaxApiPayloadSize = size;
}
private bool ReadCredentials(string credentialsPath) {
try {
//Read in our credentials
using(StreamReader reader = new StreamReader(credentialsPath)){
string json = reader.ReadToEnd();
if(String.IsNullOrWhiteSpace(json))throw new Exception();
Credentials = JsonConvert.DeserializeObject(json);
return true;
}
} catch {
//Either the file doesn't exist, or the file's contents are corrupted
//Console.Error.WriteLine("Please ensure credentials.json path points to a file with valid Skylight API credentials. If using the Skytool CLI, copy the API credentials to the credentials.json file in the root working directory.");
return false;
}
}
public string GetFileIdFromUri(string fileUri) {
string[] parts = fileUri.Split('/');
//v2 does not end with content and v3 does
if (parts[parts.Length - 1] == "content")
return parts[parts.Length - 2];
else
return parts[parts.Length - 1];
}
public string GetFileUriFromId(string fileId) {
return $"{ApiUrl}{Skylight.Api.Media.V3.Constants.BaseEndpointPath}/files/{fileId}/content";
}
#region Notifications
/// <summary>
/// Send a notification to a user
/// </summary>
/// <param name="userId">Id of the user receiving the notification</param>
/// <param name="message">The message displayed to the user</param>
/// <param name="alertType">The alert type</param>
/// <exception cref="ArgumentNullException">If any argument is null</exception>
/// <exception cref="ArgumentException">If any argument is invalid</exception>
/// <exception cref="ApiException">Thrown if the API call fails</exception>
/// <exception cref="Exception">If the _apiClient is null; most likely because Init() was not run</exception>
public async Task SendNotification(string userId, string message, int alertType)
{
if (string.IsNullOrWhiteSpace(userId))
{
throw new ArgumentException("userId cannot be null or whitespace", nameof(userId));
}
if (string.IsNullOrWhiteSpace(message))
{
throw new ArgumentException("message cannot be null or whitespace", nameof(message));
}
var notification = new NotificationRequest
{
To = new Guid(userId),
Alert = new NotificationRequestAlert
{
Message = message,
Type = alertType
}
};
var notificationPost = new NotificationsPostRequest(notification);
await _apiClient.ExecuteRequestAsync(notificationPost);
}
#endregion Notifications
#region Group
/// <summary>
/// Is the user in the group?
/// </summary>
/// <param name="userId">The user id</param>
/// <param name="groupName">The group name</param>
/// <returns>Return true if the user is in the group, otherwise return false</returns>
/// <exception cref="ArgumentNullException">Thrown if null argument</exception>
/// <exception cref="ArgumentException">Thrown if invalid argument</exception>
/// <exception cref="ApiException">Thrown if the API call fails</exception>
public async Task<bool> IsUserInGroup(string userId, string groupName)
{
// check the arguments
if (string.IsNullOrWhiteSpace(userId))
{
throw new ArgumentException("userId cannot be null or whitespace", nameof(userId));
}
if (string.IsNullOrWhiteSpace(groupName))
{
throw new ArgumentException("groupName cannot be null or whitespace", nameof(groupName));
}
// get the groups for the user
var getUserGroupsRequest = new Skylight.Api.Authentication.V1.GroupsRequests.GetUserGroupsRequest(userId);
var response = await _apiClient.ExecuteRequestAsync(getUserGroupsRequest);
if (response.Content == null)
{
// the user isn't a member of any groups
return false;
}
// is the user a member of the group?
foreach (var group in response.Content)
{
if (group.Name == groupName)
{
// the user is a memeber of the group
return true;
}
}
// the user isn't a member of the group
return false;
}
/// <summary>
/// Get the Group with specified name.
/// </summary>
/// <param name="groupName">name of group to retrieve</param>
/// <returns>Group having the specified groupName; null if group not found</returns>
/// <exception cref="ArgumentException">Thrown if invalid argument</exception>
/// <exception cref="ApiException">Thrown if the API call fails</exception>
public async Task<GroupWithMembers> x(string groupName)
{
if (string.IsNullOrWhiteSpace(groupName))
{
throw new ArgumentException("groupName cannot be null or whitespace", nameof(groupName));
}
// get all groups
var getAllGroupsRequest = new GetGroupsRequest();
var getAllGroupsResponse = await _apiClient.ExecuteRequestAsync(getAllGroupsRequest);
// find the group matching "groupName"; this group object does NOT include all the members.
var group = getAllGroupsResponse.Content.FirstOrDefault(x => x.Name == groupName);
if (group == null)
{
//group with name of groupName not found
return null;
}
// get the full group data for the found group
var getGroupRequest = new GetGroupRequest(group.Id);
var getGroupResponse = await _apiClient.ExecuteRequestAsync(getGroupRequest);
return getGroupResponse.Content;
}
#endregion Group
}
}