-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathApiClient.java
1918 lines (1783 loc) · 65.2 KB
/
ApiClient.java
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
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2019-Present Datadog, Inc.
*/
package com.datadog.api.client;
import com.datadog.api.client.auth.ApiKeyAuth;
import com.datadog.api.client.auth.Authentication;
import com.datadog.api.client.auth.HttpBasicAuth;
import com.datadog.api.client.auth.HttpBearerAuth;
import com.datadog.api.client.auth.OAuth;
import jakarta.ws.rs.client.AsyncInvoker;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.Invocation;
import jakarta.ws.rs.client.InvocationCallback;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.Form;
import jakarta.ws.rs.core.GenericType;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
import jakarta.ws.rs.core.Variant;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.text.DateFormat;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.HttpUrlConnectorProvider;
import org.glassfish.jersey.client.filter.EncodingFilter;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.media.multipart.Boundary;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.message.DeflateEncoder;
import org.glassfish.jersey.message.GZipEncoder;
@jakarta.annotation.Generated(
value = "https://github.com/DataDog/datadog-api-client-java/blob/master/.generator")
public class ApiClient {
protected Map<String, String> defaultHeaderMap = new HashMap<String, String>();
protected Map<String, String> defaultCookieMap = new HashMap<String, String>();
protected String basePath = "https://api.datadoghq.com";
protected String userAgent;
private DateTimeFormatter offsetDateTimeFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
protected List<ServerConfiguration> servers =
new ArrayList<ServerConfiguration>(
Arrays.asList(
new ServerConfiguration(
"https://{subdomain}.{site}",
"No description provided",
new HashMap<String, ServerVariable>() {
{
put(
"site",
new ServerVariable(
"The regional site for Datadog customers.",
"datadoghq.com",
new HashSet<String>(
Arrays.asList(
"datadoghq.com",
"us3.datadoghq.com",
"us5.datadoghq.com",
"ap1.datadoghq.com",
"datadoghq.eu",
"ddog-gov.com"))));
put(
"subdomain",
new ServerVariable(
"The subdomain where the API is deployed.",
"api",
new HashSet<String>()));
}
}),
new ServerConfiguration(
"{protocol}://{name}",
"No description provided",
new HashMap<String, ServerVariable>() {
{
put(
"name",
new ServerVariable(
"Full site DNS name.", "api.datadoghq.com", new HashSet<String>()));
put(
"protocol",
new ServerVariable(
"The protocol for accessing the API.",
"https",
new HashSet<String>()));
}
}),
new ServerConfiguration(
"https://{subdomain}.{site}",
"No description provided",
new HashMap<String, ServerVariable>() {
{
put(
"site",
new ServerVariable(
"Any Datadog deployment.", "datadoghq.com", new HashSet<String>()));
put(
"subdomain",
new ServerVariable(
"The subdomain where the API is deployed.",
"api",
new HashSet<String>()));
}
})));
protected Integer serverIndex = 0;
protected Map<String, String> serverVariables = null;
protected Map<String, List<ServerConfiguration>> operationServers =
new HashMap<String, List<ServerConfiguration>>() {
{
put(
"v1.IpRangesApi.getIPRanges",
new ArrayList<ServerConfiguration>(
Arrays.asList(
new ServerConfiguration(
"https://{subdomain}.{site}",
"No description provided",
new HashMap<String, ServerVariable>() {
{
put(
"site",
new ServerVariable(
"The regional site for Datadog customers.",
"datadoghq.com",
new HashSet<String>(
Arrays.asList(
"datadoghq.com",
"us3.datadoghq.com",
"us5.datadoghq.com",
"ap1.datadoghq.com",
"datadoghq.eu",
"ddog-gov.com"))));
put(
"subdomain",
new ServerVariable(
"The subdomain where the API is deployed.",
"ip-ranges",
new HashSet<String>()));
}
}),
new ServerConfiguration(
"{protocol}://{name}",
"No description provided",
new HashMap<String, ServerVariable>() {
{
put(
"name",
new ServerVariable(
"Full site DNS name.",
"ip-ranges.datadoghq.com",
new HashSet<String>()));
put(
"protocol",
new ServerVariable(
"The protocol for accessing the API.",
"https",
new HashSet<String>()));
}
}),
new ServerConfiguration(
"https://{subdomain}.datadoghq.com",
"No description provided",
new HashMap<String, ServerVariable>() {
{
put(
"subdomain",
new ServerVariable(
"The subdomain where the API is deployed.",
"ip-ranges",
new HashSet<String>()));
}
}))));
put(
"v1.LogsApi.submitLog",
new ArrayList<ServerConfiguration>(
Arrays.asList(
new ServerConfiguration(
"https://{subdomain}.{site}",
"No description provided",
new HashMap<String, ServerVariable>() {
{
put(
"site",
new ServerVariable(
"The regional site for Datadog customers.",
"datadoghq.com",
new HashSet<String>(
Arrays.asList(
"datadoghq.com",
"us3.datadoghq.com",
"us5.datadoghq.com",
"ap1.datadoghq.com",
"datadoghq.eu",
"ddog-gov.com"))));
put(
"subdomain",
new ServerVariable(
"The subdomain where the API is deployed.",
"http-intake.logs",
new HashSet<String>()));
}
}),
new ServerConfiguration(
"{protocol}://{name}",
"No description provided",
new HashMap<String, ServerVariable>() {
{
put(
"name",
new ServerVariable(
"Full site DNS name.",
"http-intake.logs.datadoghq.com",
new HashSet<String>()));
put(
"protocol",
new ServerVariable(
"The protocol for accessing the API.",
"https",
new HashSet<String>()));
}
}),
new ServerConfiguration(
"https://{subdomain}.{site}",
"No description provided",
new HashMap<String, ServerVariable>() {
{
put(
"site",
new ServerVariable(
"Any Datadog deployment.",
"datadoghq.com",
new HashSet<String>()));
put(
"subdomain",
new ServerVariable(
"The subdomain where the API is deployed.",
"http-intake.logs",
new HashSet<String>()));
}
}))));
put(
"v2.LogsApi.submitLog",
new ArrayList<ServerConfiguration>(
Arrays.asList(
new ServerConfiguration(
"https://{subdomain}.{site}",
"No description provided",
new HashMap<String, ServerVariable>() {
{
put(
"site",
new ServerVariable(
"The regional site for customers.",
"datadoghq.com",
new HashSet<String>(
Arrays.asList(
"datadoghq.com",
"us3.datadoghq.com",
"us5.datadoghq.com",
"ap1.datadoghq.com",
"datadoghq.eu",
"ddog-gov.com"))));
put(
"subdomain",
new ServerVariable(
"The subdomain where the API is deployed.",
"http-intake.logs",
new HashSet<String>()));
}
}),
new ServerConfiguration(
"{protocol}://{name}",
"No description provided",
new HashMap<String, ServerVariable>() {
{
put(
"name",
new ServerVariable(
"Full site DNS name.",
"http-intake.logs.datadoghq.com",
new HashSet<String>()));
put(
"protocol",
new ServerVariable(
"The protocol for accessing the API.",
"https",
new HashSet<String>()));
}
}),
new ServerConfiguration(
"https://{subdomain}.{site}",
"No description provided",
new HashMap<String, ServerVariable>() {
{
put(
"site",
new ServerVariable(
"Any Datadog deployment.",
"datadoghq.com",
new HashSet<String>()));
put(
"subdomain",
new ServerVariable(
"The subdomain where the API is deployed.",
"http-intake.logs",
new HashSet<String>()));
}
}))));
}
};
protected Map<String, Integer> operationServerIndex = new HashMap<String, Integer>();
protected Map<String, Map<String, String>> operationServerVariables =
new HashMap<String, Map<String, String>>();
protected boolean debugging = false;
protected RetryConfig retry = new RetryConfig(false, 2, 2, 3);
protected boolean compress = true;
protected ClientConfig clientConfig;
protected int connectionTimeout = 0;
private int readTimeout = 0;
protected Client httpClient;
protected JSON json;
protected String tempFolderPath = null;
protected Map<String, Authentication> authentications;
protected DateFormat dateFormat;
protected final Map<String, Boolean> unstableOperations =
new HashMap<String, Boolean>() {
{
put("v2.createOpenAPI", false);
put("v2.deleteOpenAPI", false);
put("v2.getOpenAPI", false);
put("v2.listAPIs", false);
put("v2.updateOpenAPI", false);
put("v2.cancelDataDeletionRequest", false);
put("v2.createDataDeletionRequest", false);
put("v2.getDataDeletionRequests", false);
put("v2.createDORADeployment", false);
put("v2.createDORAIncident", false);
put("v2.createIncident", false);
put("v2.createIncidentIntegration", false);
put("v2.createIncidentTodo", false);
put("v2.createIncidentType", false);
put("v2.deleteIncident", false);
put("v2.deleteIncidentIntegration", false);
put("v2.deleteIncidentTodo", false);
put("v2.deleteIncidentType", false);
put("v2.getIncident", false);
put("v2.getIncidentIntegration", false);
put("v2.getIncidentTodo", false);
put("v2.getIncidentType", false);
put("v2.listIncidentAttachments", false);
put("v2.listIncidentIntegrations", false);
put("v2.listIncidents", false);
put("v2.listIncidentTodos", false);
put("v2.listIncidentTypes", false);
put("v2.searchIncidents", false);
put("v2.updateIncident", false);
put("v2.updateIncidentAttachments", false);
put("v2.updateIncidentIntegration", false);
put("v2.updateIncidentTodo", false);
put("v2.updateIncidentType", false);
put("v2.createAWSAccount", false);
put("v2.createNewAWSExternalID", false);
put("v2.deleteAWSAccount", false);
put("v2.getAWSAccount", false);
put("v2.listAWSAccounts", false);
put("v2.listAWSNamespaces", false);
put("v2.updateAWSAccount", false);
put("v2.listAWSLogsServices", false);
put("v2.getAggregatedConnections", false);
put("v2.getFinding", false);
put("v2.getRuleVersionHistory", false);
put("v2.getSBOM", false);
put("v2.listFindings", false);
put("v2.listVulnerabilities", false);
put("v2.listVulnerableAssets", false);
put("v2.muteFindings", false);
put("v2.createScorecardOutcomesBatch", false);
put("v2.createScorecardRule", false);
put("v2.deleteScorecardRule", false);
put("v2.listScorecardOutcomes", false);
put("v2.listScorecardRules", false);
put("v2.updateScorecardRule", false);
put("v2.createIncidentService", false);
put("v2.deleteIncidentService", false);
put("v2.getIncidentService", false);
put("v2.listIncidentServices", false);
put("v2.updateIncidentService", false);
put("v2.createSLOReportJob", false);
put("v2.getSLOReport", false);
put("v2.getSLOReportJobStatus", false);
put("v2.createIncidentTeam", false);
put("v2.deleteIncidentTeam", false);
put("v2.getIncidentTeam", false);
put("v2.listIncidentTeams", false);
put("v2.updateIncidentTeam", false);
}
};
protected static final java.util.logging.Logger logger =
java.util.logging.Logger.getLogger(ApiClient.class.getName());
private static ApiClient defaultApiClient;
/**
* Get the default API client, which would be used when creating API instances without providing
* an API client.
*
* @return Default API client
*/
public static ApiClient getDefaultApiClient() {
if (defaultApiClient != null) {
return defaultApiClient;
}
defaultApiClient = new ApiClient();
// Configure the Datadog site to send API calls to
String site = System.getenv("DD_SITE");
if (site != null) {
HashMap<String, String> serverVariables = new HashMap<String, String>();
serverVariables.put("site", site);
defaultApiClient.setServerVariables(serverVariables);
}
// Configure API key authorization
HashMap<String, String> secrets = new HashMap<String, String>();
String apiKeyAuth = System.getenv("DD_API_KEY");
if (apiKeyAuth != null) {
secrets.put("apiKeyAuth", apiKeyAuth);
}
String appKeyAuth = System.getenv("DD_APP_KEY");
if (appKeyAuth != null) {
secrets.put("appKeyAuth", appKeyAuth);
}
defaultApiClient.configureApiKeys(secrets);
return defaultApiClient;
}
/**
* Set the default API client, which would be used when creating API instances without providing
* an API client.
*
* @param apiClient API client
*/
public static void setDefaultApiClient(ApiClient apiClient) {
defaultApiClient = apiClient;
}
/** Constructs a new ApiClient with default parameters. */
public ApiClient() {
this(null);
}
/**
* Constructs a new ApiClient with the specified authentication parameters.
*
* @param authMap A hash map containing authentication parameters.
*/
public ApiClient(Map<String, Authentication> authMap) {
json = new JSON();
httpClient = buildHttpClient();
this.dateFormat = new RFC3339DateFormat();
// Set default User-Agent.
setUserAgent();
// Setup authentications (key: authentication name, value: authentication).
authentications = new HashMap<String, Authentication>();
Authentication auth = null;
if (authMap != null) {
auth = authMap.get("AuthZ");
}
if (auth instanceof OAuth) {
authentications.put("AuthZ", auth);
} else {
authentications.put("AuthZ", new OAuth(basePath, "/oauth2/v1/token"));
}
if (authMap != null) {
auth = authMap.get("apiKeyAuth");
}
if (auth instanceof ApiKeyAuth) {
authentications.put("apiKeyAuth", auth);
} else {
authentications.put("apiKeyAuth", new ApiKeyAuth("header", "DD-API-KEY"));
}
if (authMap != null) {
auth = authMap.get("appKeyAuth");
}
if (auth instanceof ApiKeyAuth) {
authentications.put("appKeyAuth", auth);
} else {
authentications.put("appKeyAuth", new ApiKeyAuth("header", "DD-APPLICATION-KEY"));
}
// Prevent the authentications from being modified.
authentications = Collections.unmodifiableMap(authentications);
}
/**
* Get the date format used to parse/format {@code OffsetDateTime} parameters.
*
* @return DateTimeFormatter
*/
public DateTimeFormatter getOffsetDateTimeFormatter() {
return offsetDateTimeFormatter;
}
/**
* Add custom retry object in the client
*
* @param retry retry object
*/
public void setRetry(RetryConfig retry) {
this.retry = retry;
}
/**
* Return the retryConfig object
*
* @return retryConfig
*/
public RetryConfig getRetry() {
return retry;
}
/**
* Enable retry directly on the client instead of creating a new retry object
*
* @param enableRetry bool, enable retry or not
*/
public void enableRetry(boolean enableRetry) {
this.retry.setEnableRetry(enableRetry);
}
/**
* Set the date format used to parse/format {@code OffsetDateTime} parameters.
*
* @param offsetDateTimeFormatter {@code DateTimeFormatter}
*/
public void setOffsetDateTimeFormatter(DateTimeFormatter offsetDateTimeFormatter) {
this.offsetDateTimeFormatter = offsetDateTimeFormatter;
}
/**
* Format the given {@code OffsetDateTime} object into string.
*
* @param offsetDateTime {@code OffsetDateTime}
* @return {@code OffsetDateTime} in string format
*/
public String formatOffsetDateTime(OffsetDateTime offsetDateTime) {
return offsetDateTimeFormatter.format(offsetDateTime);
}
/**
* Gets the JSON instance to do JSON serialization and deserialization.
*
* @return JSON
*/
public JSON getJSON() {
return json;
}
public Client getHttpClient() {
return httpClient;
}
public ApiClient setHttpClient(Client httpClient) {
this.httpClient = httpClient;
return this;
}
/**
* Returns the base URL to the location where the OpenAPI document is being served.
*
* @return The base URL to the target host.
*/
public String getBasePath() {
return basePath;
}
/**
* Sets the base URL to the location where the OpenAPI document is being served.
*
* @param basePath The base URL to the target host.
* @return API client
*/
public ApiClient setBasePath(String basePath) {
this.basePath = basePath;
setOauthBasePath(basePath);
return this;
}
public List<ServerConfiguration> getServers() {
return servers;
}
public ApiClient setServers(List<ServerConfiguration> servers) {
this.servers = servers;
updateBasePath();
return this;
}
public Integer getServerIndex() {
return serverIndex;
}
public ApiClient setServerIndex(Integer serverIndex) {
this.serverIndex = serverIndex;
updateBasePath();
return this;
}
public Map<String, String> getServerVariables() {
return serverVariables;
}
public ApiClient setServerVariables(Map<String, String> serverVariables) {
this.serverVariables = serverVariables;
updateBasePath();
return this;
}
private void updateBasePath() {
if (serverIndex != null) {
setBasePath(servers.get(serverIndex).URL(serverVariables));
}
}
private void setOauthBasePath(String basePath) {
for (Authentication auth : authentications.values()) {
if (auth instanceof OAuth) {
((OAuth) auth).setBasePath(basePath);
}
}
}
/**
* Get authentications (key: authentication name, value: authentication).
*
* @return Map of authentication object
*/
public Map<String, Authentication> getAuthentications() {
return authentications;
}
/**
* Get authentication for the given name.
*
* @param authName The authentication name
* @return The authentication, null if not found
*/
public Authentication getAuthentication(String authName) {
return authentications.get(authName);
}
/**
* Helper method to set username for the first HTTP basic authentication.
*
* @param username Username
* @return API client
*/
public ApiClient setUsername(String username) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBasicAuth) {
((HttpBasicAuth) auth).setUsername(username);
return this;
}
}
throw new RuntimeException("No HTTP basic authentication configured!");
}
/**
* Helper method to set password for the first HTTP basic authentication.
*
* @param password Password
* @return API client
*/
public ApiClient setPassword(String password) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBasicAuth) {
((HttpBasicAuth) auth).setPassword(password);
return this;
}
}
throw new RuntimeException("No HTTP basic authentication configured!");
}
/**
* Helper method to set API key value for the first API key authentication.
*
* @param apiKey API key
* @return API client
*/
public ApiClient setApiKey(String apiKey) {
for (Authentication auth : authentications.values()) {
if (auth instanceof ApiKeyAuth) {
((ApiKeyAuth) auth).setApiKey(apiKey);
return this;
}
}
throw new RuntimeException("No API key authentication configured!");
}
/**
* Helper method to configure authentications which respects aliases of API keys.
*
* @param secrets Hash map from authentication name to its secret.
* @return API client
*/
public ApiClient configureApiKeys(Map<String, String> secrets) {
for (Map.Entry<String, Authentication> authEntry : authentications.entrySet()) {
Authentication auth = authEntry.getValue();
if (auth instanceof ApiKeyAuth) {
String name = authEntry.getKey();
if (secrets.containsKey(name)) {
((ApiKeyAuth) auth).setApiKey(secrets.get(name));
}
}
}
return this;
}
/**
* Helper method to set API key prefix for the first API key authentication.
*
* @param apiKeyPrefix API key prefix
* @return API client
*/
public ApiClient setApiKeyPrefix(String apiKeyPrefix) {
for (Authentication auth : authentications.values()) {
if (auth instanceof ApiKeyAuth) {
((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
return this;
}
}
throw new RuntimeException("No API key authentication configured!");
}
/**
* Helper method to set bearer token for the first Bearer authentication.
*
* @param bearerToken Bearer token
* @return API client
*/
public ApiClient setBearerToken(String bearerToken) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBearerAuth) {
((HttpBearerAuth) auth).setBearerToken(bearerToken);
return this;
}
}
throw new RuntimeException("No Bearer authentication configured!");
}
/**
* Helper method to set access token for the first OAuth2 authentication.
*
* @param accessToken Access token
* @return API client
*/
public ApiClient setAccessToken(String accessToken) {
for (Authentication auth : authentications.values()) {
if (auth instanceof OAuth) {
((OAuth) auth).setAccessToken(accessToken);
return this;
}
}
throw new RuntimeException("No OAuth2 authentication configured!");
}
/**
* Helper method to set the credentials for the first OAuth2 authentication.
*
* @param clientId the client ID
* @param clientSecret the client secret
* @return API client
*/
public ApiClient setOauthCredentials(String clientId, String clientSecret) {
for (Authentication auth : authentications.values()) {
if (auth instanceof OAuth) {
((OAuth) auth).setCredentials(clientId, clientSecret, isDebugging());
return this;
}
}
throw new RuntimeException("No OAuth2 authentication configured!");
}
/**
* Helper method to set the password flow for the first OAuth2 authentication.
*
* @param username the user name
* @param password the user password
* @return API client
*/
public ApiClient setOauthPasswordFlow(String username, String password) {
for (Authentication auth : authentications.values()) {
if (auth instanceof OAuth) {
((OAuth) auth).usePasswordFlow(username, password);
return this;
}
}
throw new RuntimeException("No OAuth2 authentication configured!");
}
/**
* Helper method to set the authorization code flow for the first OAuth2 authentication.
*
* @param code the authorization code
* @return API client
*/
public ApiClient setOauthAuthorizationCodeFlow(String code) {
for (Authentication auth : authentications.values()) {
if (auth instanceof OAuth) {
((OAuth) auth).useAuthorizationCodeFlow(code);
return this;
}
}
throw new RuntimeException("No OAuth2 authentication configured!");
}
/**
* Helper method to set the scopes for the first OAuth2 authentication.
*
* @param scope the oauth scope
* @return API client
*/
public ApiClient setOauthScope(String scope) {
for (Authentication auth : authentications.values()) {
if (auth instanceof OAuth) {
((OAuth) auth).setScope(scope);
return this;
}
}
throw new RuntimeException("No OAuth2 authentication configured!");
}
/**
* Set the User-Agent header's value (by adding to the default header map).
*
* @param userAgent Http user agent
* @return API client
*/
public ApiClient setUserAgent(String userAgent) {
addDefaultHeader("User-Agent", userAgent);
return this;
}
/**
* Get the User-Agent header's value.
*
* @return User-Agent string
*/
public String getUserAgent() {
return userAgent;
}
/**
* Set the default User-Agent header's value with telemetry information (by adding to the default
* header map).
*
* @return API client
*/
public ApiClient setUserAgent() {
final Properties properties = new Properties();
try {
properties.load(
getClass().getClassLoader().getResourceAsStream("com/datadog/api/project.properties"));
} catch (IOException e) {
logger.severe("Could not load client version: " + e.toString());
}
String userAgent =
"datadog-api-client-java/"
+ properties.getProperty("version")
+ " ("
+ "java "
+ System.getProperty("java.version")
+ "; "
+ "java_vendor "
+ System.getProperty("java.vendor")
+ "; "
+ "os "
+ System.getProperty("os.name")
+ "; "
+ "os_version "
+ System.getProperty("os.version")
+ "; "
+ "arch "
+ System.getProperty("os.arch")
+ ")";
addDefaultHeader("User-Agent", userAgent);
this.userAgent = userAgent;
return this;
}
/**
* Add a default header.
*
* @param key The header's key
* @param value The header's value
* @return API client
*/
public ApiClient addDefaultHeader(String key, String value) {
defaultHeaderMap.put(key, value);
return this;
}
/**
* Add a default cookie.
*
* @param key The cookie's key
* @param value The cookie's value
* @return API client
*/
public ApiClient addDefaultCookie(String key, String value) {
defaultCookieMap.put(key, value);
return this;
}
/**
* Gets the client config.
*
* @return Client config
*/
public ClientConfig getClientConfig() {
return clientConfig;
}
/**
* Set the client config.
*
* @param clientConfig Set the client config
* @return API client
*/
public ApiClient setClientConfig(ClientConfig clientConfig) {
this.clientConfig = clientConfig;
// Rebuild HTTP Client according to the new "clientConfig" value.
this.httpClient = buildHttpClient();
return this;
}
/**
* Check that whether debugging is enabled for this API client.
*
* @return True if debugging is switched on
*/
public boolean isDebugging() {
return debugging;
}
/**
* Enable/disable debugging for this API client.
*
* @param debugging To enable (true) or disable (false) debugging
* @return API client
*/
public ApiClient setDebugging(boolean debugging) {
this.debugging = debugging;
// Rebuild HTTP Client according to the new "debugging" value.
this.setClientConfig(null);