-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathRestClient.java
More file actions
964 lines (853 loc) · 39.7 KB
/
RestClient.java
File metadata and controls
964 lines (853 loc) · 39.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
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
package io.imagekit.sdk.tasks;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import io.imagekit.sdk.ImageKit;
import io.imagekit.sdk.exceptions.BadRequestException;
import io.imagekit.sdk.exceptions.ConflictException;
import io.imagekit.sdk.exceptions.ForbiddenException;
import io.imagekit.sdk.exceptions.InternalServerException;
import io.imagekit.sdk.exceptions.NotFoundException;
import io.imagekit.sdk.exceptions.PartialSuccessException;
import io.imagekit.sdk.exceptions.TooManyRequestsException;
import io.imagekit.sdk.exceptions.UnauthorizedException;
import io.imagekit.sdk.exceptions.UnknownException;
import io.imagekit.sdk.models.AITagsRequest;
import io.imagekit.sdk.models.BaseFile;
import io.imagekit.sdk.models.CopyFileRequest;
import io.imagekit.sdk.models.CopyFolderRequest;
import io.imagekit.sdk.models.CreateFolderRequest;
import io.imagekit.sdk.models.CustomMetaDataFieldCreateRequest;
import io.imagekit.sdk.models.CustomMetaDataFieldUpdateRequest;
import io.imagekit.sdk.models.DeleteFileVersionRequest;
import io.imagekit.sdk.models.DeleteFolderRequest;
import io.imagekit.sdk.models.FileCreateRequest;
import io.imagekit.sdk.models.GetFileListRequest;
import io.imagekit.sdk.models.MoveFileRequest;
import io.imagekit.sdk.models.MoveFolderRequest;
import io.imagekit.sdk.models.MetaData;
import io.imagekit.sdk.models.FileUpdateRequest;
import io.imagekit.sdk.models.RenameFileRequest;
import io.imagekit.sdk.models.TagsRequest;
import io.imagekit.sdk.models.results.*;
import io.imagekit.sdk.utils.Utils;
import okhttp3.*;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class RestClient {
public static String API_BASE_URL = "https://api.imagekit.io/";
public static String UPLOAD_BASE_URL = "https://upload.imagekit.io/";
private ImageKit imageKit;
Request request;
OkHttpClient client;
MultipartBuilder multipartBuilder;
public RestClient(ImageKit imageKit) {
this.imageKit = imageKit;
this.client = new OkHttpClient();
this.multipartBuilder = new MultipartBuilder();
}
public Result upload(FileCreateRequest fileCreateRequest) throws InternalServerException, BadRequestException,
UnknownException, ForbiddenException, TooManyRequestsException, UnauthorizedException {
Result result = null;
Map<String, String> headers = Utils.getHeaders(imageKit);
MultipartBody body = multipartBuilder.build(fileCreateRequest);
request = new Request.Builder().url(UPLOAD_BASE_URL.concat("api/v1/files/upload")).post(body)
.headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 200) {
respBody = response.body().string();
result = new Gson().fromJson(respBody, Result.class);
result.setRaw(respBody);
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, result.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return result;
}
public Result updateFileDetail(FileUpdateRequest fileUpdateRequest)
throws ForbiddenException, TooManyRequestsException, InternalServerException, UnauthorizedException,
BadRequestException, UnknownException {
Result result = null;
Map<String, String> headers = Utils.getHeaders(imageKit);
String url = String.format(Locale.US, API_BASE_URL.concat("v1/files/%s/details"),
fileUpdateRequest.getFileId());
request = new Request.Builder().url(url).patch(multipartBuilder.build(fileUpdateRequest))
.headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 200) {
respBody = response.body().string();
result = new Gson().fromJson(respBody, Result.class);
result.setRaw(respBody);
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, result.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return result;
}
public ResultList getFileList(GetFileListRequest getFileListRequest) throws ForbiddenException, TooManyRequestsException,
InternalServerException, UnauthorizedException, BadRequestException, UnknownException, InstantiationException, IllegalAccessException {
ResultList resultList = new ResultList();
Map<String, String> headers = Utils.getHeaders(imageKit);
QueryMaker queryMaker = new QueryMaker();
Map<String, String> options = new HashMap<>();
if (getFileListRequest.getType() != null) {
options.put("type", getFileListRequest.getType());
}
if (getFileListRequest.getSort() != null) {
options.put("sort", getFileListRequest.getSort());
}
if (getFileListRequest.getPath() != null) {
options.put("path", getFileListRequest.getPath());
}
if (getFileListRequest.getSearchQuery() != null) {
options.put("searchQuery", getFileListRequest.getSearchQuery());
}
if (getFileListRequest.getFileType() != null) {
options.put("fileType", getFileListRequest.getFileType());
}
if (getFileListRequest.getLimit() != null) {
options.put("limit", getFileListRequest.getLimit());
}
if (getFileListRequest.getSkip() != null) {
options.put("skip", getFileListRequest.getSkip());
}
if (getFileListRequest.getTags() != null) {
options.put("tags", String.join(",", getFileListRequest.getTags()));
}
for (Map.Entry<String, String> entry : options.entrySet()) {
queryMaker.put(String.format("%s=%s", entry.getKey(), entry.getValue()));
}
String url = String.format(Locale.US, API_BASE_URL.concat("v1/files?%s"), queryMaker.get());
request = new Request.Builder().url(url).get().headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 200) {
respBody = response.body().string();
List<BaseFile> files = new Gson().fromJson(respBody,
new TypeToken<List<BaseFile>>() {
}.getType());
resultList.setResults(files);
resultList.setRaw(respBody);
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, resultList.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return resultList;
}
public Result getFileDetail(String fileId) throws ForbiddenException, TooManyRequestsException,
InternalServerException, UnauthorizedException, BadRequestException, UnknownException {
Result result = new Result();
Map<String, String> headers = Utils.getHeaders(imageKit);
String url = String.format(Locale.US, API_BASE_URL.concat("v1/files/%s/details"), fileId);
request = new Request.Builder().url(url).get().headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 200) {
respBody = response.body().string();
result = new Gson().fromJson(respBody, Result.class);
result.setRaw(respBody);
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, result.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return result;
}
public ResultMetaData getFileMetaData(String fileId) throws ForbiddenException, TooManyRequestsException,
InternalServerException, UnauthorizedException, BadRequestException, UnknownException {
ResultMetaData result = new ResultMetaData();
Map<String, String> headers = Utils.getHeaders(imageKit);
String url = String.format(Locale.US, API_BASE_URL.concat("v1/files/%s/metadata"), fileId);
request = new Request.Builder().url(url).get().headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 200) {
respBody = response.body().string();
MetaData metaData = new Gson().fromJson(respBody, MetaData.class);
result.setResults(metaData);
result.setRaw(respBody);
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, result.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return result;
}
public ResultMetaData getRemoteFileMetaData(String url) throws ForbiddenException, TooManyRequestsException,
InternalServerException, UnauthorizedException, BadRequestException, UnknownException {
ResultMetaData result = new ResultMetaData();
Map<String, String> headers = Utils.getHeaders(imageKit);
String apiURL = API_BASE_URL.concat("v1/metadata?url=") + url;
request = new Request.Builder().url(apiURL).get().headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 200) {
respBody = response.body().string();
MetaData metaData = new Gson().fromJson(respBody, MetaData.class);
result.setResults(metaData);
result.setRaw(respBody);
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, result.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return result;
}
public Result deleteFile(String fileId) throws ForbiddenException, TooManyRequestsException,
InternalServerException, UnauthorizedException, BadRequestException, UnknownException {
Result result = new Result();
Map<String, String> headers = Utils.getHeaders(imageKit);
String url = String.format(Locale.US, API_BASE_URL.concat("v1/files/%s"), fileId);
request = new Request.Builder().url(url).delete().headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 204) {
respBody = response.body().string();
result.setFileId(fileId);
result.setRaw(respBody);
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, result.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return result;
}
public ResultFileDelete bulkDeleteFiles(List<String> fileIds)
throws ForbiddenException, TooManyRequestsException, InternalServerException, PartialSuccessException,
UnauthorizedException, NotFoundException, BadRequestException, UnknownException {
ResultFileDelete result = new ResultFileDelete();
Map<String, String> headers = Utils.getHeaders(imageKit);
String url = API_BASE_URL.concat("v1/files/batch/deleteByFileIds");
request = new Request.Builder().url(url)
.post(multipartBuilder.build(String.format("{\"fileIds\":%s}", new Gson().toJson(fileIds))))
.headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 200) {
respBody = response.body().string();
result = new Gson().fromJson(respBody, ResultFileDelete.class);
result.setRaw(respBody);
} else if (response.code() == 207 || response.code() == 404) {
Utils.throwOtherException(response);
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, result.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return result;
}
public ResultCache purgeCache(String url) throws ForbiddenException, TooManyRequestsException,
InternalServerException, UnauthorizedException, BadRequestException, UnknownException {
ResultCache result = new ResultCache();
Map<String, String> headers = Utils.getHeaders(imageKit);
request = new Request.Builder().url(API_BASE_URL.concat("v1/files/purge"))
.post(multipartBuilder.build(String.format("{\"url\":\"%s\"}", url))).headers(Headers.of(headers))
.build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 200 || response.code() == 201) {
respBody = response.body().string();
result = new Gson().fromJson(respBody, ResultCache.class);
result.setRaw(respBody);
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, result.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return result;
}
public ResultCacheStatus getPurgeCacheStatus(String requestId) throws ForbiddenException, TooManyRequestsException,
InternalServerException, UnauthorizedException, BadRequestException, UnknownException {
ResultCacheStatus result = new ResultCacheStatus();
Map<String, String> headers = Utils.getHeaders(imageKit);
String url = String.format(Locale.US, API_BASE_URL.concat("v1/files/purge/%s"), requestId);
request = new Request.Builder().url(url).get().headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 200) {
respBody = response.body().string();
result = new Gson().fromJson(respBody, ResultCacheStatus.class);
result.setRaw(respBody);
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, result.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return result;
}
public ResultTags manageTags(TagsRequest tagsRequest, String action)
throws NotFoundException, PartialSuccessException, BadRequestException, InternalServerException,
UnknownException, ForbiddenException, TooManyRequestsException, UnauthorizedException {
ResultTags resultTags = new ResultTags();
Map<String, String> headers = Utils.getHeaders(imageKit);
RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json"),
new Gson().toJson(tagsRequest));
request = new Request.Builder()
.url(action.equals("removeTags") ? API_BASE_URL.concat("v1/files/removeTags")
: API_BASE_URL.concat("v1/files/addTags"))
.post(requestBody).headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 200) {
respBody = response.body().string();
resultTags = new Gson().fromJson(respBody, ResultTags.class);
} else if (response.code() == 207 || response.code() == 404) {
Utils.throwOtherException(response);
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, resultTags.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return resultTags;
}
public ResultTags removeAITags(AITagsRequest aiTagsRequest)
throws PartialSuccessException, NotFoundException, BadRequestException, InternalServerException,
UnknownException, ForbiddenException, TooManyRequestsException, UnauthorizedException {
ResultTags resultTags = new ResultTags();
Map<String, String> headers = Utils.getHeaders(imageKit);
RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json"),
new Gson().toJson(aiTagsRequest));
request = new Request.Builder().url(API_BASE_URL.concat("v1/files/removeAITags")).post(requestBody)
.headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 200) {
respBody = response.body().string();
resultTags = new Gson().fromJson(respBody, ResultTags.class);
} else if (response.code() == 207 || response.code() == 404) {
Utils.throwOtherException(response);
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, resultTags.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return resultTags;
}
public ResultCustomMetaDataFieldList getCustomMetaDataFields(boolean includeDeleted) throws UnknownException {
ResultCustomMetaDataFieldList resultCustomMetaDataFieldList = new ResultCustomMetaDataFieldList();
Map<String, String> headers = Utils.getHeaders(imageKit);
request = new Request.Builder()
.url(API_BASE_URL.concat("v1/customMetadataFields?includeDeleted=" + includeDeleted)).get()
.headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 200) {
respBody = response.body().string();
List<ResultCustomMetaDataField> resultCustomMetaDataFields = new Gson().fromJson(respBody,
new TypeToken<List<ResultCustomMetaDataField>>() {
}.getType());
resultCustomMetaDataFieldList.setResultCustomMetaDataFieldList(resultCustomMetaDataFields);
}
Utils.populateResponseMetadata(respBody, resultCustomMetaDataFieldList.getResponseMetaData(),
response.code(), response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return resultCustomMetaDataFieldList;
}
public ResultCustomMetaDataField createCustomMetaDataFields(
CustomMetaDataFieldCreateRequest customMetaDataFieldCreateRequest)
throws BadRequestException, UnknownException {
if (customMetaDataFieldCreateRequest.getName() == null) {
throw new RuntimeException("Error: Name not provided.");
}
if (customMetaDataFieldCreateRequest.getLabel() == null) {
throw new RuntimeException("Error: Label not provided.");
}
ResultCustomMetaDataField resultCustomMetaDataField = new ResultCustomMetaDataField();
Map<String, String> headers = Utils.getHeaders(imageKit);
RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json"),
new Gson().toJson(customMetaDataFieldCreateRequest));
request = new Request.Builder().url(API_BASE_URL.concat("v1/customMetadataFields")).post(requestBody)
.headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 201) {
respBody = response.body().string();
JsonElement responseBody = new JsonParser().parse(respBody);
resultCustomMetaDataField = new Gson().fromJson(responseBody, ResultCustomMetaDataField.class);
} else {
if (response.code() == 400) {
ResultException result = Utils.populateResult(response);
throw new BadRequestException(result.getMessage(), null, false, false, result.getMessage(),
result.getHelp(), result.getResponseMetaData());
}
}
Utils.populateResponseMetadata(respBody, resultCustomMetaDataField.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return resultCustomMetaDataField;
}
public ResultNoContent deleteCustomMetaDataField(String id) throws NotFoundException, UnknownException {
ResultNoContent resultNoContent = new ResultNoContent();
Map<String, String> headers = Utils.getHeaders(imageKit);
String url = String.format(Locale.US, API_BASE_URL.concat("v1/customMetadataFields/%s"), id);
request = new Request.Builder().url(url).delete().headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 204) {
String respString = response.body().string();
respBody = respString == null ? "" : respString;
} else {
if (response.code() == 404) {
ResultException result = Utils.populateResult(response);
throw new NotFoundException(result.getMessage(), null, false, false, result.getMessage(),
result.getHelp(), result.getResponseMetaData());
}
}
Utils.populateResponseMetadata(respBody, resultNoContent.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return resultNoContent;
}
public ResultCustomMetaDataField updateCustomMetaDataFields(
CustomMetaDataFieldUpdateRequest customMetaDataFieldUpdateRequest)
throws BadRequestException, NotFoundException, UnknownException {
ResultCustomMetaDataField resultCustomMetaDataField = new ResultCustomMetaDataField();
Map<String, String> headers = Utils.getHeaders(imageKit);
RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json"),
new Gson().toJson(customMetaDataFieldUpdateRequest));
String url = String.format(Locale.US, API_BASE_URL.concat("v1/customMetadataFields/%s"),
customMetaDataFieldUpdateRequest.getId());
request = new Request.Builder().url(url).patch(requestBody).headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 200) {
respBody = response.body().string();
JsonElement responseBody = new JsonParser().parse(respBody);
resultCustomMetaDataField = new Gson().fromJson(responseBody, ResultCustomMetaDataField.class);
} else {
if (response.code() == 400 || response.code() == 404) {
ResultException result = Utils.populateResult(response);
if (response.code() == 400) {
throw new BadRequestException(result.getMessage(), null, false, false, result.getMessage(),
result.getHelp(), result.getResponseMetaData());
} else {
throw new NotFoundException(result.getMessage(), null, false, false, result.getMessage(),
result.getHelp(), result.getResponseMetaData());
}
}
}
Utils.populateResponseMetadata(respBody, resultCustomMetaDataField.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return resultCustomMetaDataField;
}
public ResultNoContent deleteFileVersion(DeleteFileVersionRequest deleteFileVersionRequest)
throws BadRequestException, NotFoundException, InternalServerException, UnknownException,
ForbiddenException, TooManyRequestsException, UnauthorizedException {
ResultNoContent resultNoContent = new ResultNoContent();
Map<String, String> headers = Utils.getHeaders(imageKit);
String url = String.format(Locale.US, API_BASE_URL.concat("v1/files/%s/versions/%s"),
deleteFileVersionRequest.getFileId(), deleteFileVersionRequest.getVersionId());
request = new Request.Builder().url(url).delete().headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 204) {
String respString = response.body().string();
respBody = respString == null ? "" : respString;
} else if (response.code() == 400 || response.code() == 404) {
ResultException result = Utils.populateResult(response);
if (response.code() == 400) {
throw new BadRequestException(result.getMessage(), null, false, false, result.getMessage(),
result.getHelp(), result.getResponseMetaData());
} else {
throw new NotFoundException(result.getMessage(), null, false, false, result.getMessage(),
result.getHelp(), result.getResponseMetaData());
}
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, resultNoContent.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return resultNoContent;
}
public ResultNoContent copyFile(CopyFileRequest copyFileRequest)
throws NotFoundException, BadRequestException, InternalServerException, UnknownException,
ForbiddenException, TooManyRequestsException, UnauthorizedException {
ResultNoContent resultNoContent = new ResultNoContent();
Map<String, String> headers = Utils.getHeaders(imageKit);
RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json"),
new Gson().toJson(copyFileRequest));
request = new Request.Builder().url(API_BASE_URL.concat("v1/files/copy")).post(requestBody)
.headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 204) {
String respString = response.body().string();
respBody = respString == null ? "" : respString;
} else if (response.code() == 404) {
ResultException result = Utils.populateResult(response);
throw new NotFoundException(result.getMessage(), null, false, false, result.getMessage(),
result.getHelp(), result.getResponseMetaData());
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, resultNoContent.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return resultNoContent;
}
public ResultNoContent moveFile(MoveFileRequest moveFileRequest)
throws NotFoundException, BadRequestException, InternalServerException, UnknownException,
ForbiddenException, TooManyRequestsException, UnauthorizedException {
ResultNoContent resultNoContent = new ResultNoContent();
Map<String, String> headers = Utils.getHeaders(imageKit);
RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json"),
new Gson().toJson(moveFileRequest));
request = new Request.Builder().url(API_BASE_URL.concat("v1/files/move")).post(requestBody)
.headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 204) {
String respString = response.body().string();
respBody = respString == null ? "" : respString;
} else if (response.code() == 404) {
ResultException result = Utils.populateResult(response);
throw new NotFoundException(result.getMessage(), null, false, false, result.getMessage(),
result.getHelp(), result.getResponseMetaData());
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, resultNoContent.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return resultNoContent;
}
public ResultRenameFile renameFile(RenameFileRequest renameFileRequest) throws PartialSuccessException,
ConflictException, NotFoundException, BadRequestException, InternalServerException, UnknownException,
ForbiddenException, TooManyRequestsException, UnauthorizedException {
ResultRenameFile resultRenameFile = new ResultRenameFile();
Map<String, String> headers = Utils.getHeaders(imageKit);
RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json"),
new Gson().toJson(renameFileRequest));
request = new Request.Builder().url(API_BASE_URL.concat("v1/files/rename")).put(requestBody)
.headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 200) {
String respString = response.body().string();
respBody = respString == null || respString.equals("") ? "{}" : respString;
resultRenameFile = new Gson().fromJson(respBody, ResultRenameFile.class);
} else if (response.code() == 207 || response.code() == 404) {
Utils.throwOtherException(response);
} else if (response.code() == 409) {
ResultException result = Utils.populateResult(response);
throw new ConflictException(result.getMessage(), null, false, false, result.getMessage(),
result.getHelp(), result.getResponseMetaData());
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, resultRenameFile.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return resultRenameFile;
}
public Result restoreFileVersion(String fileId, String versionId)
throws NotFoundException, BadRequestException, InternalServerException, UnknownException,
ForbiddenException, TooManyRequestsException, UnauthorizedException {
Result resultFileVersionDetails = new Result();
Map<String, String> headers = Utils.getHeaders(imageKit);
String url = String.format(Locale.US, API_BASE_URL.concat("v1/files/%s/versions/%s/restore"), fileId,
versionId);
RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json"), "");
request = new Request.Builder().url(url).put(requestBody).headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 200) {
System.out.println("20000000000000");
respBody = response.body().string();
resultFileVersionDetails = new Gson().fromJson(respBody, Result.class);
} else if (response.code() == 404) {
System.out.println("400000000000004");
ResultException result = Utils.populateResult(response);
throw new NotFoundException(result.getMessage(), null, false, false, result.getMessage(),
result.getHelp(), result.getResponseMetaData());
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, resultFileVersionDetails.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return resultFileVersionDetails;
}
public ResultEmptyBlock createFolder(CreateFolderRequest createFolderRequest) throws UnknownException {
ResultEmptyBlock resultEmptyBlock = new ResultEmptyBlock();
Map<String, String> headers = Utils.getHeaders(imageKit);
RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json"),
new Gson().toJson(createFolderRequest));
request = new Request.Builder().url(API_BASE_URL.concat("v1/folder/")).post(requestBody)
.headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 201) {
String respString = response.body().string();
respBody = respString == null || respString.equals("") ? "{}" : respString;
}
Utils.populateResponseMetadata(respBody, resultEmptyBlock.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return resultEmptyBlock;
}
public ResultNoContent deleteFolder(DeleteFolderRequest deleteFolderRequest)
throws NotFoundException, BadRequestException, InternalServerException, UnknownException,
ForbiddenException, TooManyRequestsException, UnauthorizedException {
ResultNoContent resultNoContent = new ResultNoContent();
Map<String, String> headers = Utils.getHeaders(imageKit);
RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json"),
new Gson().toJson(deleteFolderRequest));
request = new Request.Builder().url(API_BASE_URL.concat("v1/folder/")).delete(requestBody)
.headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 204) {
String respString = response.body().string();
respBody = respString == null ? "" : respString;
} else if (response.code() == 404) {
ResultException result = Utils.populateResult(response);
throw new NotFoundException(result.getMessage(), null, false, false, result.getMessage(),
result.getHelp(), result.getResponseMetaData());
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, resultNoContent.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return resultNoContent;
}
public ResultOfFolderActions copyFolder(CopyFolderRequest copyFolderRequest)
throws NotFoundException, BadRequestException, InternalServerException, UnknownException,
ForbiddenException, TooManyRequestsException, UnauthorizedException {
ResultOfFolderActions resultOfFolderActions = new ResultOfFolderActions();
Map<String, String> headers = Utils.getHeaders(imageKit);
RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json"),
new Gson().toJson(copyFolderRequest));
request = new Request.Builder().url(API_BASE_URL.concat("v1/bulkJobs/copyFolder")).post(requestBody)
.headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 200) {
respBody = response.body().string();
resultOfFolderActions = new Gson().fromJson(respBody, ResultOfFolderActions.class);
} else if (response.code() == 404) {
ResultException result = Utils.populateResult(response);
throw new NotFoundException(result.getMessage(), null, false, false, result.getMessage(),
result.getHelp(), result.getResponseMetaData());
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, resultOfFolderActions.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return resultOfFolderActions;
}
public ResultOfFolderActions moveFolder(MoveFolderRequest moveFolderRequest)
throws NotFoundException, BadRequestException, InternalServerException, UnknownException,
ForbiddenException, TooManyRequestsException, UnauthorizedException {
ResultOfFolderActions resultOfFolderActions = new ResultOfFolderActions();
Map<String, String> headers = Utils.getHeaders(imageKit);
RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json"),
new Gson().toJson(moveFolderRequest));
request = new Request.Builder().url(API_BASE_URL.concat("v1/bulkJobs/moveFolder")).post(requestBody)
.headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 200) {
respBody = response.body().string();
resultOfFolderActions = new Gson().fromJson(respBody, ResultOfFolderActions.class);
} else if (response.code() == 404) {
ResultException result = Utils.populateResult(response);
throw new NotFoundException(result.getMessage(), null, false, false, result.getMessage(),
result.getHelp(), result.getResponseMetaData());
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, resultOfFolderActions.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return resultOfFolderActions;
}
public ResultBulkJobStatus getBulkJobStatus(String jobId) throws ForbiddenException, TooManyRequestsException,
InternalServerException, UnauthorizedException, BadRequestException, UnknownException {
ResultBulkJobStatus resultBulkJobStatus = new ResultBulkJobStatus();
Map<String, String> headers = Utils.getHeaders(imageKit);
String url = String.format(Locale.US, API_BASE_URL.concat("v1/bulkJobs/%s"), jobId);
request = new Request.Builder().url(url).get().headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 200) {
respBody = response.body().string();
resultBulkJobStatus = new Gson().fromJson(respBody, ResultBulkJobStatus.class);
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, resultBulkJobStatus.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return resultBulkJobStatus;
}
public ResultFileVersions getFileVersions(String fileId)
throws NotFoundException, BadRequestException, InternalServerException, UnknownException,
ForbiddenException, TooManyRequestsException, UnauthorizedException {
ResultFileVersions resultFileVersions = new ResultFileVersions();
Map<String, String> headers = Utils.getHeaders(imageKit);
String url = String.format(Locale.US, API_BASE_URL.concat("v1/files/%s/versions"), fileId);
request = new Request.Builder().url(url).get().headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 200) {
respBody = response.body().string();
List<ResultFileVersionDetails> resultFileVersionDetailsList = new Gson().fromJson(respBody,
new TypeToken<List<ResultFileVersionDetails>>() {
}.getType());
resultFileVersions.setResultFileVersionDetailsList(resultFileVersionDetailsList);
} else if (response.code() == 404) {
ResultException result = Utils.populateResult(response);
throw new NotFoundException(result.getMessage(), null, false, false, result.getMessage(),
result.getHelp(), result.getResponseMetaData());
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, resultFileVersions.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return resultFileVersions;
}
public ResultFileVersionDetails getFileVersionDetails(String fileId, String versionId)
throws NotFoundException, BadRequestException, InternalServerException, UnknownException,
ForbiddenException, TooManyRequestsException, UnauthorizedException {
if (fileId == null) {
throw new RuntimeException("Error: FileId not provided.");
}
if (versionId == null) {
throw new RuntimeException("Error: versionId not provided.");
}
ResultFileVersionDetails resultFileVersionDetails = new ResultFileVersionDetails();
Map<String, String> headers = Utils.getHeaders(imageKit);
String url = String.format(Locale.US, API_BASE_URL.concat("v1/files/%s/versions/%s"), fileId, versionId);
request = new Request.Builder().url(url).get().headers(Headers.of(headers)).build();
try {
Response response = client.newCall(request).execute();
String respBody = "";
if (response.code() == 200) {
respBody = response.body().string();
resultFileVersionDetails = new Gson().fromJson(respBody, ResultFileVersionDetails.class);
} else if (response.code() == 404) {
ResultException result = Utils.populateResult(response);
throw new NotFoundException(result.getMessage(), null, false, false, result.getMessage(),
result.getHelp(), result.getResponseMetaData());
} else {
Utils.generalApiThrowException(response);
}
Utils.populateResponseMetadata(respBody, resultFileVersionDetails.getResponseMetaData(), response.code(),
response.headers().toMultimap());
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
return resultFileVersionDetails;
}
}