-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathProjectClient.cs
2193 lines (1918 loc) · 103 KB
/
ProjectClient.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
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Newtonsoft.Json;
using Sdl.Community.GroupShareKit.Exceptions;
using Sdl.Community.GroupShareKit.Helpers;
using Sdl.Community.GroupShareKit.Http;
using Sdl.Community.GroupShareKit.Models;
using Sdl.Community.GroupShareKit.Models.Response;
using Sdl.Community.GroupShareKit.Models.Response.ProjectPublishingInformation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Project = Sdl.Community.GroupShareKit.Models.Response.Project;
namespace Sdl.Community.GroupShareKit.Clients
{
/// <summary>
/// A client for GroupShare's ProjectServer API.
/// </summary>
public class ProjectClient : ApiClient, IProjectClient
{
public ProjectClient(IApiConnection apiConnection) : base(apiConnection)
{
}
#region Project management methods
/// <summary>
/// Gets a <see cref="Project"/>.
/// </summary>
/// <remarks>
/// <param name="request"><see cref="ProjectsRequest"/></param>
/// This method requires authentication.
/// </remarks>
/// <exception cref="AuthorizationException">
/// Thrown when the current user does not have permission to make the request.
/// </exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns><see cref="Project"/></returns>
public Task<Project> GetProject(ProjectsRequest request)
{
Ensure.ArgumentNotNull(request, "request");
return ApiConnection.Get<Project>(ApiUrls.GetAllProjects(), request.ToParametersDictionary());
}
/// <summary>
/// Gets all <see cref="Project"/>s
/// </summary>
/// <remarks>
/// This method requires authentication.
/// </remarks>
/// <exception cref="AuthorizationException">
/// Thrown when the current user does not have permission to make the request.
/// </exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns>A list of <see cref="Project"/>s.</returns>
public Task<Project> GetAllProjects()
{
return ApiConnection.Get<Project>(ApiUrls.GetAllProjects(), null);
}
/// <summary>
/// Gets all <see cref="Project"/>s for the organization.
/// </summary>
/// <remarks>
/// <param name="organizationName">string</param>
/// This method requires authentication.
/// </remarks>
/// <exception cref="AuthorizationException">
/// Thrown when the current user does not have permission to make the request.
/// </exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns>A list of <see cref="ProjectDetails"/>s.</returns>
public List<ProjectDetails> GetProjectsForOrganization(string organizationName)
{
var allProjects = ApiConnection.Get<Project>(ApiUrls.GetAllProjects(), null);
return allProjects.Result.Items.Where(o => o.OrganizationName == organizationName).ToList();
}
[Obsolete("This method is obsolete. Call 'GetProjectFiles(Guid)' instead.")]
public Task<IReadOnlyList<File>> GetAllFilesForProject(string projectId)
{
Ensure.ArgumentNotNullOrEmptyString(projectId, "projectId");
return ApiConnection.GetAll<File>(ApiUrls.ProjectFiles(projectId));
}
/// <summary>
/// Gets all <see cref="File"/>s of a project.
/// </summary>
/// <param name="projectId">The project's Guid.</param>
/// <remarks>
/// This method requires authentication.
/// </remarks>
/// <exception cref="AuthorizationException">
/// Thrown when the current user does not have permission to make the request.
/// </exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns>A list of <see cref="File"/>s.</returns>
public Task<IReadOnlyList<File>> GetProjectFiles(Guid projectId)
{
Ensure.ArgumentNotNull(projectId, "projectId");
return ApiConnection.GetAll<File>(ApiUrls.ProjectFiles(projectId));
}
[Obsolete("This method is obsolete. Call 'GetProjectPhases(Guid)' instead.")]
public Task<IReadOnlyList<Phase>> GetAllPhasesForProject(string projectId)
{
Ensure.ArgumentNotNullOrEmptyString(projectId, "projectId");
return ApiConnection.GetAll<Phase>(ApiUrls.ProjectPhases(projectId));
}
/// <summary>
/// Gets all <see cref="Phase"/>s for a project.
/// </summary>
/// <param name="projectId">The project's Guid.</param>
/// <remarks>
/// This method requires authentication.
/// </remarks>
/// <exception cref="AuthorizationException">
/// Thrown when the current user does not have permission to make the request.
/// </exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns>A list of <see cref="Phase"/>s.</returns>
public Task<IReadOnlyList<Phase>> GetProjectPhases(Guid projectId)
{
Ensure.ArgumentNotNull(projectId, "projectId");
return ApiConnection.GetAll<Phase>(ApiUrls.ProjectPhases(projectId));
}
[Obsolete("This method is obsolete. Call 'GetPhasesWithAssignees(Guid, int)' instead.")]
public Task<IReadOnlyList<PhasesWithAssignees>> GetPhasesWithAssignees(string projectId, int phaseId)
{
Ensure.ArgumentNotNullOrEmptyString(projectId, "projectId");
Ensure.ArgumentNotNullOrEmptyString(phaseId.ToString(), "phaseId");
return ApiConnection.GetAll<PhasesWithAssignees>(ApiUrls.ProjectPhasesWithAssignees(projectId, phaseId));
}
/// <summary>
/// Gets a list of files with all phases with assignee information.
/// </summary>
/// <param name="projectId">The project's Guid</param>
/// <param name="phaseId">The Id of the project phase used for filtering. This parameter is optional.</param>
/// <remarks>
/// This method requires authentication.
/// </remarks>
/// <exception cref="AuthorizationException">
/// Thrown when the current user does not have permission to make the request.
/// </exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns>A list of <see cref="PhasesWithAssignees"/>.</returns>
public Task<IReadOnlyList<PhasesWithAssignees>> GetPhasesWithAssignees(Guid projectId, int phaseId)
{
Ensure.ArgumentNotNull(projectId, "projectId");
Ensure.ArgumentNotNull(phaseId, "phaseId");
return ApiConnection.GetAll<PhasesWithAssignees>(ApiUrls.ProjectPhasesWithAssignees(projectId, phaseId));
}
[Obsolete("This method is obsolete. Call 'ChangePhase(ChangePhaseRequest)' instead.")]
public Task ChangePhases(string projectId, ChangePhaseRequest request)
{
Ensure.ArgumentNotNullOrEmptyString(projectId, "projectId");
Ensure.ArgumentNotNull(request, "request");
return ApiConnection.Post<string>(ApiUrls.ChangePhases(projectId), request, "application/json");
}
/// <summary>
/// Changes the phases for a list of files of a specific project.
/// </summary>
/// <param name="projectId">The project's Guid.</param>
/// <param name="request"><see cref="ChangePhaseRequest"/></param>
/// <remarks>
/// This method requires authentication.
/// </remarks>
/// <exception cref="AuthorizationException">
/// Thrown when the current user does not have permission to make the request.
/// </exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
public Task ChangePhase(Guid projectId, ChangePhaseRequest request)
{
Ensure.ArgumentNotNull(projectId, "projectId");
Ensure.ArgumentNotNull(request, "request");
return ApiConnection.Post<Guid>(ApiUrls.ChangePhase(projectId), request, "application/json");
}
[Obsolete("This method is obsolete. Call 'ChangeAssignment(Guid, ChangeAssignmentRequest)' instead.")]
public Task ChangeAssignments(string projectId, ChangeAssignmentRequest request)
{
Ensure.ArgumentNotNullOrEmptyString(projectId, "projectId");
Ensure.ArgumentNotNull(request, "request");
return ApiConnection.Post<string>(ApiUrls.ChangeAssignments(projectId), request, "application/json");
}
/// <summary>
/// Changes user assignment for a specific project files list for a specific phase.
/// </summary>
/// <param name="projectId">The project's Guid.</param>
/// <param name="request"><see cref="ChangeAssignmentRequest"/></param>
/// <remarks>
/// This method requires authentication.
/// </remarks>
/// <exception cref="AuthorizationException">
/// Thrown when the current user does not have permission to make the request.
/// </exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
public Task ChangeAssignment(Guid projectId, ChangeAssignmentRequest request)
{
Ensure.ArgumentNotNull(projectId, "projectId");
Ensure.ArgumentNotNull(request, "request");
return ApiConnection.Post<string>(ApiUrls.ChangeAssignment(projectId), request, "application/json");
}
/// <summary>
/// Create project
/// </summary>
/// <remarks>
/// This method requires authentication.
/// </remarks>
/// <exception cref="AuthorizationException">
/// Thrown when the current user does not have permission to make the request.
/// </exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
public async Task<string> CreateProject(CreateProjectRequest request)
{
Ensure.ArgumentNotNull(request, "request");
var projectUri = await ApiConnection.Post<string>(ApiUrls.GetAllProjects(), request, "application/json");
var projectId = projectUri.Split('/').Last();
await UploadFilesForProject(projectId, request.RawData, request.Name);
return projectId;
}
/// <summary>
/// Creates an empty project.
/// </summary>
/// <returns>The project id</returns>
public async Task<string> CreateProjectSkeleton(CreateProjectSkeletonRequest request)
{
Ensure.ArgumentNotNull(request, "request");
var projectUri = await ApiConnection.Post<string>(ApiUrls.GetAllProjects(), request, "application/json");
var projectId = projectUri.Split('/').Last();
return projectId;
}
/// <summary>
/// Create project
/// </summary>
/// <remarks>
/// This method requires authentication.
/// </remarks>
/// <param name="request">The basic project parameters</param>
/// <param name="filesPath">The path pointing to the files for the project. The path can be a zip file, a single file, or a directory.
/// If it is a zip file path, the zip should have a folder SourceFiles, an optional folder ReferenceFiles, and an optional folder PerfectMatchFiles.
/// If it is a file, the project will be created as a single file project. If it is a directory, all the files under the directory will be the project files.
/// </param>
/// <param name="referenceFilesPath">If filesPath parameter is not a zip file, this optional parameter points to a reference file or a directory containing reference files.</param>
/// <param name="perfectMatchFilesPaths">If filesPath parameter is not a zip file, this optional parameter points to a directories containing the perfect match files.
/// </param>
/// <returns>The project Id</returns>
/// <exception cref="AuthorizationException">
/// Thrown when the current user does not have permission to make the request.
/// </exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
public async Task<string> CreateProject(BasicCreateProjectRequest request,
string filesPath, string referenceFilesPath = null, string[] perfectMatchFilesPaths = null)
{
Ensure.ArgumentNotNullOrEmptyString(filesPath, "filesPath");
// register the project creation
var projectCreateResponse = await ApiConnection.Post<string>(ApiUrls.GetAllProjects(), request, "application/json");
var projectId = projectCreateResponse.Split('/').Last();
var started = await UploadFilesForProject(projectId, filesPath);
if (started)
{
return projectId;
}
if (!string.IsNullOrEmpty(referenceFilesPath))
{
await UploadReferenceFilesForProject(projectId, referenceFilesPath);
}
if (perfectMatchFilesPaths != null && perfectMatchFilesPaths.Length > 0)
{
for (var i = 0; i < perfectMatchFilesPaths.Length; i++)
{
var uri = ApiUrls.GetPerfectMatchFiles(projectId, i);
await UploadPerfectMatchFilesForProject(uri.ToString(), perfectMatchFilesPaths[i]);
}
}
await ApiConnection.Post<string>(ApiUrls.StartProjectCreationUri(projectId));
return projectId;
}
private async Task<bool> UploadFilesForProject(string projectId, string filesPath)
{
if (System.IO.File.Exists(filesPath))
{
if (filesPath.EndsWith(".zip", StringComparison.InvariantCultureIgnoreCase))
{
var uri = ApiUrls.UploadFilesForProject(projectId, false, true);
await UploadFilesForProject(uri, new[] { filesPath });
return true;
}
else
{
var uri = ApiUrls.UploadFilesForProject(projectId, false, false);
System.Diagnostics.Debug.WriteLine($"Upload files: {filesPath}");
await UploadFilesForProject(uri, new[] { filesPath });
return false;
}
}
else
{
var uri = ApiUrls.UploadFilesForProject(projectId, false, false);
System.Diagnostics.Debug.WriteLine($"Upload folder: {filesPath}");
await UploadDirectoryForProject(uri, filesPath);
return false;
}
}
private async Task UploadReferenceFilesForProject(string projectId, string referenceFilesPath)
{
var uri = ApiUrls.UploadFilesForProject(projectId, true, false);
if (System.IO.File.Exists(referenceFilesPath))
{
await UploadFilesForProject(uri, new[] { referenceFilesPath });
}
else
{
await UploadDirectoryForProject(uri, referenceFilesPath);
}
}
private async Task UploadPerfectMatchFilesForProject(string uri, string directory)
{
// first level are language folders
foreach (var languageDir in new System.IO.DirectoryInfo(directory).GetDirectories())
{
//var languageCode = languageDir.Name;
await UploadDirectoryForProject(uri, languageDir.FullName);
}
}
private async Task UploadDirectoryForProject(string uri, string directory)
{
var files = System.IO.Directory.GetFiles(directory);
if (files.Length > 0)
{
System.Diagnostics.Debug.WriteLine("Upload files: " + string.Join(Environment.NewLine, files));
await UploadFilesForProject(uri, files);
}
foreach (var subDir in new System.IO.DirectoryInfo(directory).GetDirectories())
{
var name = subDir.Name;
var folderUri = uri + name + "\\";
System.Diagnostics.Debug.WriteLine(folderUri);
System.Diagnostics.Debug.WriteLine($"Upload folder: {subDir.FullName}");
await UploadDirectoryForProject(folderUri, subDir.FullName);
}
}
private async Task UploadFilesForProject(string uri, string[] filesPaths)
{
using (var content = new MultipartFormDataContent())
{
foreach (var file in filesPaths)
{
var stream = new System.IO.FileStream(file, System.IO.FileMode.Open);
var streamContent = new StreamContent(stream);
streamContent.Headers.Add("Content-Type", "application/octet-stream");
content.Add(streamContent, "file", System.IO.Path.GetFileName(file));
}
await ApiConnection.Post<string>(uri, content, null);
}
}
[Obsolete("This method is obsolete. Call 'AddFiles(Guid, string, bool)' instead.")]
public async Task<MidProjectUpdateResponse> AddFiles(string projectId, string filesPath, bool reference = false)
{
var uri = ApiUrls.AddProjectFiles(projectId, reference);
using (var content = new MultipartFormDataContent())
{
var stream = new System.IO.FileStream(filesPath, System.IO.FileMode.Open);
var streamContent = new StreamContent(stream);
streamContent.Headers.Add("Content-Type", "application/octet-stream");
content.Add(streamContent, "file", System.IO.Path.GetFileName(filesPath));
return await ApiConnection.Post<MidProjectUpdateResponse>(uri, content, null);
}
}
public async Task<MidProjectUpdateResponse> AddFiles(Guid projectId, string filesPath, bool reference = false)
{
var uri = ApiUrls.AddProjectFiles(projectId, reference);
using (var content = new MultipartFormDataContent())
{
var stream = new System.IO.FileStream(filesPath, System.IO.FileMode.Open);
var streamContent = new StreamContent(stream);
streamContent.Headers.Add("Content-Type", "application/octet-stream");
content.Add(streamContent, "file", System.IO.Path.GetFileName(filesPath));
return await ApiConnection.Post<MidProjectUpdateResponse>(uri, content, null);
}
}
[Obsolete("This method is obsolete. Call 'AddFiles(Guid, string[], bool)' instead.")]
public async Task<MidProjectUpdateResponse> AddFiles(string projectId, string[] filesPaths, bool reference)
{
var uri = ApiUrls.AddProjectFiles(projectId, reference);
using (var content = new MultipartFormDataContent())
{
foreach (var file in filesPaths)
{
var stream = new System.IO.FileStream(file, System.IO.FileMode.Open);
var streamContent = new StreamContent(stream);
streamContent.Headers.Add("Content-Type", "application/octet-stream");
content.Add(streamContent, "file", System.IO.Path.GetFileName(file));
}
return await ApiConnection.Post<MidProjectUpdateResponse>(uri, content, null);
}
}
public async Task<MidProjectUpdateResponse> AddFiles(Guid projectId, string[] filesPaths, bool reference)
{
var uri = ApiUrls.AddProjectFiles(projectId, reference);
using (var content = new MultipartFormDataContent())
{
foreach (var file in filesPaths)
{
var stream = new System.IO.FileStream(file, System.IO.FileMode.Open);
var streamContent = new StreamContent(stream);
streamContent.Headers.Add("Content-Type", "application/octet-stream");
content.Add(streamContent, "file", System.IO.Path.GetFileName(file));
}
return await ApiConnection.Post<MidProjectUpdateResponse>(uri, content, null);
}
}
[Obsolete("This method is obsolete. Call 'UpdateFiles(Guid, string, bool)' instead.")]
public async Task<MidProjectUpdateResponse> UpdateFiles(string projectId, string filesPath, bool reference)
{
var uri = ApiUrls.UpdateProjectFiles(projectId, reference);
using (var content = new MultipartFormDataContent())
{
var stream = new System.IO.FileStream(filesPath, System.IO.FileMode.Open);
var streamContent = new StreamContent(stream);
streamContent.Headers.Add("Content-Type", "application/octet-stream");
content.Add(streamContent, "file", System.IO.Path.GetFileName(filesPath));
return await ApiConnection.Put<MidProjectUpdateResponse>(uri, content);
}
}
public async Task<MidProjectUpdateResponse> UpdateFiles(Guid projectId, string filesPath, bool reference)
{
var uri = ApiUrls.UpdateProjectFiles(projectId, reference);
using (var content = new MultipartFormDataContent())
{
var stream = new System.IO.FileStream(filesPath, System.IO.FileMode.Open);
var streamContent = new StreamContent(stream);
streamContent.Headers.Add("Content-Type", "application/octet-stream");
content.Add(streamContent, "file", System.IO.Path.GetFileName(filesPath));
return await ApiConnection.Put<MidProjectUpdateResponse>(uri, content);
}
}
[Obsolete("This method is obsolete. Call 'UpdateFiles(Guid, string[], bool)' instead.")]
public async Task<MidProjectUpdateResponse> UpdateFiles(string projectId, string[] filesPaths, bool reference)
{
var uri = ApiUrls.UpdateProjectFiles(projectId, reference);
using (var content = new MultipartFormDataContent())
{
foreach (var file in filesPaths)
{
var stream = new System.IO.FileStream(file, System.IO.FileMode.Open);
var streamContent = new StreamContent(stream);
streamContent.Headers.Add("Content-Type", "application/octet-stream");
content.Add(streamContent, "file", System.IO.Path.GetFileName(file));
}
return await ApiConnection.Put<MidProjectUpdateResponse>(uri, content);
}
}
public async Task<MidProjectUpdateResponse> UpdateFiles(Guid projectId, string[] filesPaths, bool reference)
{
var uri = ApiUrls.UpdateProjectFiles(projectId, reference);
using (var content = new MultipartFormDataContent())
{
foreach (var file in filesPaths)
{
var stream = new System.IO.FileStream(file, System.IO.FileMode.Open);
var streamContent = new StreamContent(stream);
streamContent.Headers.Add("Content-Type", "application/octet-stream");
content.Add(streamContent, "file", System.IO.Path.GetFileName(file));
}
return await ApiConnection.Put<MidProjectUpdateResponse>(uri, content);
}
}
[Obsolete("This method is obsolete. Call 'UpdateSelectedFiles(Guid, string, MidProjectFileIdsModel, bool)' instead.")]
public async Task<MidProjectUpdateResponse> UpdateSelectedFiles(string projectId, string filesPath, MidProjectFileIdsModel fileIds, bool reference = false)
{
var uri = ApiUrls.UpdateProjectFiles(projectId, reference);
using (var content = new MultipartFormDataContent())
{
var stream = new System.IO.FileStream(filesPath, System.IO.FileMode.Open);
var streamContent = new StreamContent(stream);
streamContent.Headers.Add("Content-Type", "application/octet-stream");
content.Add(streamContent, "file", System.IO.Path.GetFileName(filesPath));
var fileIdsString = new SimpleJsonSerializer().Serialize(fileIds);
content.Add(new StringContent(fileIdsString), "FileIds");
return await ApiConnection.Put<MidProjectUpdateResponse>(uri, content);
}
}
public async Task<MidProjectUpdateResponse> UpdateSelectedFiles(Guid projectId, string filesPath, MidProjectFileIdsModel fileIds, bool reference = false)
{
var uri = ApiUrls.UpdateProjectFiles(projectId, reference);
using (var content = new MultipartFormDataContent())
{
var stream = new System.IO.FileStream(filesPath, System.IO.FileMode.Open);
var streamContent = new StreamContent(stream);
streamContent.Headers.Add("Content-Type", "application/octet-stream");
content.Add(streamContent, "file", System.IO.Path.GetFileName(filesPath));
var fileIdsString = new SimpleJsonSerializer().Serialize(fileIds);
content.Add(new StringContent(fileIdsString), "FileIds");
return await ApiConnection.Put<MidProjectUpdateResponse>(uri, content);
}
}
[Obsolete("This method is obsolete. Call 'UpdateSelectedFiles(Guid, string, MidProjectFileIdsModel, bool)' instead.")]
public async Task<MidProjectUpdateResponse> UpdateSelectedFiles(string projectId, string[] filesPaths, MidProjectFileIdsModel fileIds, bool reference)
{
var uri = ApiUrls.UpdateProjectFiles(projectId, reference);
using (var content = new MultipartFormDataContent())
{
foreach (var file in filesPaths)
{
var stream = new System.IO.FileStream(file, System.IO.FileMode.Open);
var streamContent = new StreamContent(stream);
streamContent.Headers.Add("Content-Type", "application/octet-stream");
content.Add(streamContent, "file", System.IO.Path.GetFileName(file));
}
var fileIdsString = new SimpleJsonSerializer().Serialize(fileIds);
content.Add(new StringContent(fileIdsString), "FileIds");
return await ApiConnection.Put<MidProjectUpdateResponse>(uri, content);
}
}
public async Task<MidProjectUpdateResponse> UpdateSelectedFiles(Guid projectId, string[] filesPaths, MidProjectFileIdsModel fileIds, bool reference)
{
var uri = ApiUrls.UpdateProjectFiles(projectId, reference);
using (var content = new MultipartFormDataContent())
{
foreach (var file in filesPaths)
{
var stream = new System.IO.FileStream(file, System.IO.FileMode.Open);
var streamContent = new StreamContent(stream);
streamContent.Headers.Add("Content-Type", "application/octet-stream");
content.Add(streamContent, "file", System.IO.Path.GetFileName(file));
}
var fileIdsString = new SimpleJsonSerializer().Serialize(fileIds);
content.Add(new StringContent(fileIdsString), "FileIds");
return await ApiConnection.Put<MidProjectUpdateResponse>(uri, content);
}
}
[Obsolete("This method is obsolete. Call 'CancelProjectFiles(Guid, MidProjectFileIdsModel)' instead.")]
public async Task<string> CancelProjectFiles(string projectId, MidProjectFileIdsModel fileIds)
{
var uri = ApiUrls.CancelProjectFiles(projectId);
var fileIdsString = new SimpleJsonSerializer().Serialize(fileIds);
return await ApiConnection.Put<string>(uri, fileIdsString);
}
public async Task<string> CancelProjectFiles(Guid projectId, MidProjectFileIdsModel fileIds)
{
var uri = ApiUrls.CancelProjectFiles(projectId);
var fileIdsString = new SimpleJsonSerializer().Serialize(fileIds);
return await ApiConnection.Put<string>(uri, fileIdsString);
}
/// <summary>
/// Get background tasks list with filter and default sort options
/// </summary>
public async Task<JsonCollection<BackgroundTask>> GetBackgroundTasks(string filter, int limit = 50)
{
var sort = new SortOptions
{
Property = "CreatedAt",
Direction = "DESC"
};
var serializedSortOptions = JsonConvert.SerializeObject(sort);
return await ApiConnection.Get<JsonCollection<BackgroundTask>>(ApiUrls.GetBackgroundTasks(serializedSortOptions, filter), null);
}
[Obsolete("This method is obsolete. Call 'DeleteProject(Guid)' instead.")]
public Task DeleteProject(string projectId)
{
Ensure.ArgumentNotNullOrEmptyString(projectId, "projectId");
return ApiConnection.Delete(ApiUrls.Project(projectId));
}
/// <summary>
/// Deletes a project.
/// </summary>
/// <param name="projectId">The project's Guid.</param>
/// <remarks>
/// This method requires authentication.
/// </remarks>
/// <exception cref="AuthorizationException">
/// Thrown when the current user does not have permission to make the request.
/// </exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
public Task DeleteProject(Guid projectId)
{
Ensure.ArgumentNotNull(projectId, "projectId");
return ApiConnection.Delete(ApiUrls.Project(projectId));
}
[Obsolete("This method is obsolete. Call 'GetProject(Guid)' instead.")]
public Task<ProjectDetails> Get(string projectId)
{
Ensure.ArgumentNotNullOrEmptyString(projectId, "projectId");
return ApiConnection.Get<ProjectDetails>(ApiUrls.Project(projectId), null);
}
/// <summary>
/// Gets a project by Id.
/// </summary>
/// <param name="projectId">The project's Guid.</param>
/// <remarks>
/// This method requires authentication.
/// </remarks>
/// <exception cref="AuthorizationException">
/// Thrown when the current user does not have permission to make the request.
/// </exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns><see cref="ProjectDetails"/></returns>
public Task<ProjectDetails> GetProject(Guid projectId)
{
Ensure.ArgumentNotNull(projectId, "projectId");
return ApiConnection.Get<ProjectDetails>(ApiUrls.Project(projectId), null);
}
[Obsolete("This method is obsolete. Call 'PublishingStatus(Guid)' instead.")]
public async Task<PublishingStatus> PublishingStatus(string projectId)
{
Ensure.ArgumentNotNullOrEmptyString(projectId, "projectId");
return await ApiConnection.Get<PublishingStatus>(ApiUrls.PublishingStatus(projectId), null);
}
/// <summary>
/// Gets the publishing status of a project.
/// </summary>
/// <param name="projectId">The project's Guid.</param>
/// <remarks>
/// This method requires authentication.
/// </remarks>
/// <exception cref="AuthorizationException">
/// Thrown when the current user does not have permission to make the request.
/// </exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns><see cref="PublishingStatus"/></returns>
public async Task<PublishingStatus> GetPublishingStatus(Guid projectId)
{
Ensure.ArgumentNotNull(projectId, "projectId");
return await ApiConnection.Get<PublishingStatus>(ApiUrls.PublishingStatus(projectId), null);
}
/// <summary>
/// Gets publishing information for one or multiple projects.
/// </summary>
/// <param name="projectIds">IDs of projects to get publishing information for, separated by comma.</param>
/// <returns><see cref="ProjectPublishingInformation"/></returns>
public async Task<List<ProjectPublishingInformation>> GetProjectsPublishingInformation(string projectIds)
{
Ensure.ArgumentNotNullOrEmptyString(projectIds, "projectIds");
return await ApiConnection.Get<List<ProjectPublishingInformation>>(ApiUrls.PublishingInformation(projectIds), null);
}
[Obsolete("This method is obsolete. Call 'DownloadFiles(Guid, List<Guid>)' instead.")]
public async Task<byte[]> DownloadFiles(string projectId, List<string> languageFileIds)
{
Ensure.ArgumentNotEmpty(languageFileIds, "languageFileIds");
return await ApiConnection.Get<byte[]>(ApiUrls.DownloadFiles(projectId, LanguageIdQuery(languageFileIds)), null);
}
/// <summary>
/// Downloads files with specific language file Ids.
/// </summary>
/// <param name="projectId">The project's Guid.</param>
/// <param name="languageFileIds">Language file Guids.</param>
/// <remarks>
/// This method requires authentication.
/// </remarks>
/// <exception cref="AuthorizationException">
/// Thrown when the current user does not have permission to make the request.
/// </exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns>Downloaded files content.</returns>
public async Task<byte[]> DownloadFiles(Guid projectId, List<Guid> languageFileIds)
{
Ensure.ArgumentNotNull(languageFileIds, "languageFileIds");
return await ApiConnection.Get<byte[]>(ApiUrls.DownloadFiles(projectId, LanguageIdQuery(languageFileIds)), null);
}
[Obsolete("This method is obsolete. Call 'DownloadNative(Guid)' instead.")]
public async Task<byte[]> DownloadNative(string projectId)
{
return await ApiConnection.Get<byte[]>(ApiUrls.DownloadNative(projectId), null);
}
/// <summary>
/// Downloads the native files of a project.
/// </summary>
/// <param name="projectId">The project's Guid.</param>
/// <remarks>
/// This method requires authentication.
/// </remarks>
/// <exception cref="AuthorizationException">
/// Thrown when the current user does not have permission to make the request.
/// </exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns>Downloaded files content.</returns>
public async Task<byte[]> DownloadNative(Guid projectId)
{
return await ApiConnection.Get<byte[]>(ApiUrls.DownloadNative(projectId), null);
}
[Obsolete("This method is obsolete. Call 'Finalize(Guid, List<Guid>)' instead.")]
public async Task<byte[]> Finalize(string projectId, List<string> languageFileIds)
{
Ensure.ArgumentNotEmpty(languageFileIds, "languageFileIds");
return await ApiConnection.Post<byte[]>(ApiUrls.Finalize(projectId, LanguageIdQuery(languageFileIds)));
}
/// <summary>
/// Finalizes files of a project.
/// </summary>
/// <param name="projectId">The project's Guid</param>
/// <param name="languageFileIds">Language files Guids</param>
/// <remarks>
/// This method requires authentication.
/// </remarks>
/// <exception cref="AuthorizationException">
/// Thrown when the current user does not have permission to make the request.
/// </exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns>Downloaded file content.</returns>
public async Task<byte[]> Finalize(Guid projectId, List<Guid> languageFileIds)
{
Ensure.ArgumentNotNull(projectId, "languageFileIds");
Ensure.ArgumentNotNull(languageFileIds, "languageFileIds");
return await ApiConnection.Post<byte[]>(ApiUrls.Finalize(projectId, LanguageIdQuery(languageFileIds)));
}
/// <summary>
/// Helper method to create query.
/// </summary>
/// <param name="languageFileIds"></param>
/// <returns></returns>
public string LanguageIdQuery(List<string> languageFileIds)
{
var query = string.Empty;
if (languageFileIds.Count == 1)
{
return "languageFileIds=" + languageFileIds.FirstOrDefault();
}
foreach (var id in languageFileIds)
{
query = query + "languageFileIds=" + id + "&";
}
return query;
}
public string LanguageIdQuery(List<Guid> languageFileIds)
{
var query = string.Empty;
if (languageFileIds.Count == 1)
{
return "languageFileIds=" + languageFileIds.FirstOrDefault();
}
foreach (var id in languageFileIds)
{
query = query + "languageFileIds=" + id + "&";
}
return query;
}
/// <summary>
/// Helper method to create query
/// </summary>
/// <param name="languageFileIds"></param>
/// <returns></returns>
public string FileIdQuery(List<string> languageFileIds)
{
var query = string.Empty;
foreach (var id in languageFileIds)
{
query = query + "fileId=" + id + "&";
}
return query;
}
/// <summary>
///
/// </summary>
/// <param name="languageFileIds"></param>
/// <returns></returns>
public string FileIdQuery(List<Guid> languageFileIds)
{
var query = string.Empty;
foreach (var id in languageFileIds)
{
query = query + "fileId=" + id + "&";
}
return query;
}
/// <summary>
///Downloads the files with the specific type and language code.
/// </summary>
/// <remarks>
/// This method requires authentication.
/// </remarks>
/// <exception cref="AuthorizationException">
/// Thrown when the current user does not have permission to make the request.
/// </exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns>A list of byte[] which represents downloaded files.</returns>
public async Task<byte[]> DownloadFile(FileDownloadRequest downloadRequest)
{
if (downloadRequest.Type != null)
{
return await ApiConnection.Get<byte[]>(ApiUrls.DownloadFile(downloadRequest.ProjectId, Enum.GetName(typeof(FileDownloadRequest.Types), downloadRequest.Type)), null);
}
return await ApiConnection.Get<byte[]>(ApiUrls.DownloadFile(downloadRequest.ProjectId, "all"), null);
}
/// <summary>
/// Gets a list of user assignments
/// </summary>
/// <remarks>
/// This method requires authentication.
/// </remarks>
/// <exception cref="AuthorizationException">
/// Thrown when the current user does not have permission to make the request.
/// </exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns>A list of <see cref="UserAssignments"/>s.</returns>
public async Task<IReadOnlyList<UserAssignments>> GetUserAssignments()
{
return await ApiConnection.GetAll<UserAssignments>(ApiUrls.GetProjectsAssignments(), null);
}
[Obsolete("This method is obsolete. Call 'GetProjectAssignmentById(Guid, List<Guid>)' instead.")]
public async Task<IReadOnlyList<ProjectAssignment>> GetProjectAssignmentById(string projectId, List<string> fileIdsList)
{
Ensure.ArgumentNotNullOrEmptyString(projectId, "projectId");
Ensure.ArgumentNotNull(fileIdsList, "fileIdsList");
return await ApiConnection.GetAll<ProjectAssignment>(ApiUrls.GetProjectAssignmentById(projectId, FileIdQuery(fileIdsList)), null);
}
/// <summary>
/// Gets a list of assignments for a project.
/// </summary>
/// <param name="projectId">The project's Guid</param>
/// <param name="fileIdsList">Language file Ids</param>
/// <remarks>
/// This method requires authentication.
/// </remarks>
/// <exception cref="AuthorizationException">
/// Thrown when the current user does not have permission to make the request.
/// </exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns>A list of <see cref="ProjectAssignment"/>s.</returns>
public async Task<IReadOnlyList<ProjectAssignment>> GetProjectAssignmentById(Guid projectId, List<Guid> fileIdsList)
{
Ensure.ArgumentNotNull(projectId, "projectId");
Ensure.ArgumentNotNull(fileIdsList, "fileIdsList");
return await ApiConnection.GetAll<ProjectAssignment>(ApiUrls.GetProjectAssignmentById(projectId, FileIdQuery(fileIdsList)), null);
}
/// <summary>
/// Uploads file for a specific project
/// </summary>
/// <remarks>
/// This method requires authentication.
/// </remarks>
/// <exception cref="AuthorizationException">
/// Thrown when the current user does not have permission to make the request.
/// </exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
public async Task<string> UploadFilesForProject(string projectId, byte[] rawData, string projectName)
{
Ensure.ArgumentNotNullOrEmptyString(projectId, "projectId");
var byteContent = new ByteArrayContent(rawData);
byteContent.Headers.Add("Content-Type", "application/zip");
var multipartContent = new MultipartFormDataContent
{
{ byteContent, "file", projectName + ".zip" }
};
return await ApiConnection.Post<string>(ApiUrls.UploadFilesForProject(projectId), multipartContent, "application/zip");
}
/// <summary>
/// Change project status
/// <param name="statusRequest"><see cref="ChangeStatusRequest"/></param>
/// </summary>
/// <remarks>
/// This method requires authentication.
/// </remarks>
/// <exception cref="AuthorizationException">
/// Thrown when the current user does not have permission to make the request.
/// </exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
public async Task<string> ChangeProjectStatus(ChangeStatusRequest statusRequest)
{
return await ApiConnection.Put<string>(ApiUrls.ChangeProjectStatus(statusRequest.ProjectId, Enum.GetName(typeof(ChangeStatusRequest.ProjectStatus), statusRequest.Status)), statusRequest);
}
[Obsolete("This method is obsolete. Call 'DetachProject(Guid, bool)' instead.")]
public async Task DetachProject(string projectId, bool deleteProjectTMs = false)
{
await ApiConnection.Delete(ApiUrls.DetachProject(projectId, deleteProjectTMs));
}
/// <summary>
/// Detaches a project, with the possibility to delete project TMs.
/// </summary>
/// <param name="projectId">The project's Guid.</param>
/// <param name="deleteProjectTMs">If true, project TMs will be deleted after the project is detached.</param>
public async Task DetachProject(Guid projectId, bool deleteProjectTMs = false)