Skip to content

Commit deaf972

Browse files
authored
feat: add config class for httpClient setting (#53)
Signed-off-by: Jemin <jemin9812@gmail.com>
1 parent 16d370f commit deaf972

File tree

2 files changed

+115
-29
lines changed

2 files changed

+115
-29
lines changed

src/main/java/io/litmuschaos/LitmusClient.java

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.google.gson.reflect.TypeToken;
44
import com.jayway.jsonpath.TypeRef;
5-
import com.netflix.graphql.dgs.client.GraphQLResponse;
65
import com.netflix.graphql.dgs.client.codegen.GraphQLQueryRequest;
76
import io.litmuschaos.exception.LitmusApiException;
87
import io.litmuschaos.generated.client.*;
@@ -17,6 +16,7 @@
1716
import java.util.HashMap;
1817
import java.util.List;
1918
import java.util.Map;
19+
import java.util.concurrent.TimeUnit;
2020

2121
import static io.litmuschaos.constants.ApiEndpoints.*;
2222
import static io.litmuschaos.constants.OperationNames.*;
@@ -36,13 +36,37 @@ public class LitmusClient implements AutoCloseable {
3636
private final LitmusGraphQLClient graphQLClient;
3737

3838
public LitmusClient(String host, String token) {
39+
this(host, token, LitmusConfig.DEFAULT);
40+
}
41+
42+
public LitmusClient(String host, String token, LitmusConfig config){
3943
String sanitizedHost = sanitizeURL(host);
4044
this.token = token;
41-
okHttpClient = new OkHttpClient(); // TODO : apply detail options
45+
okHttpClient = buildOkHttpClient(config);
4246
this.httpClient = new LitmusHttpClient(okHttpClient, sanitizedHost + AUTH);
4347
this.graphQLClient = new LitmusGraphQLClient(okHttpClient, sanitizedHost + API_QUERY, this.token);
4448
}
4549

50+
public static OkHttpClient buildOkHttpClient(LitmusConfig config){
51+
52+
final OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder();
53+
54+
if(config.getHttpClientReadTimeoutMillis() != null){
55+
okHttpClient.readTimeout(config.getHttpClientReadTimeoutMillis(), TimeUnit.MILLISECONDS);
56+
}
57+
if(config.getHttpClientWriteTimeoutMillis() != null){
58+
okHttpClient.writeTimeout(config.getHttpClientWriteTimeoutMillis(), TimeUnit.MILLISECONDS);
59+
}
60+
if(config.getHttpClientCallTimeoutMillis() != null){
61+
okHttpClient.callTimeout(config.getHttpClientCallTimeoutMillis(), TimeUnit.MILLISECONDS);
62+
}
63+
if(config.getHttpClientConnectTimeoutMillis() != null){
64+
okHttpClient.connectTimeout(config.getHttpClientConnectTimeoutMillis(), TimeUnit.MILLISECONDS);
65+
}
66+
67+
return okHttpClient.build();
68+
}
69+
4670
/**
4771
* Returns all the api tokens for the user.
4872
*
@@ -1314,33 +1338,8 @@ public GetManifestWithInfraIDResponse getManifestWithInfraID(GetManifestWithInfr
13141338
// TODO: subscription is not supported in current version
13151339

13161340
// public InfraEventResponse getInfraEvents(GetInfraEventsGraphQLQuery query, GetInfraEventsProjectionRoot projectionRoot){
1317-
// String request = new GraphQLQueryRequest(query, projectionRoot).serialize();
1318-
// GraphQLResponse response = graphQLClient.query(request);
1319-
// return response.extractValueAsObject("getInfraEvents", new TypeRef<InfraEventResponse>(){});
1320-
// }
1321-
//
1322-
// public KubeNamespaceResponse getKubeNamespace(GetKubeNamespaceGraphQLQuery query, GetKubeNamespaceProjectionRoot projectionRoot){
1323-
// String request = new GraphQLQueryRequest(query,projectionRoot).serialize();
1324-
// GraphQLResponse response = graphQLClient.query(request);
1325-
// return response.extractValueAsObject("getKubeNamespace", new TypeRef<KubeNamespaceResponse>(){});
1326-
// }
1327-
//
1328-
// public KubeObjectResponse getKubeObject(GetKubeObjectGraphQLQuery query, GetKubeObjectProjectionRoot projectionRoot){
1329-
// String request = new GraphQLQueryRequest(query, projectionRoot).serialize();
1330-
// GraphQLResponse response = graphQLClient.query(request);
1331-
// return response.extractValueAsObject("getKubeObject", new TypeRef<KubeObjectResponse>(){});
1332-
// }
1333-
//
1334-
// public PodLogResponse getPodLog(GetPodLogGraphQLQuery query, GetPodLogProjectionRoot projectionRoot){
1335-
// String request = new GraphQLQueryRequest(query, projectionRoot).serialize();
1336-
// GraphQLResponse response = graphQLClient.query(request);
1337-
// return response.extractValueAsObject("getPodLog", new TypeRef<PodLogResponse>(){});
1338-
// }
1339-
//
1340-
// public InfraActionResponse infraConnect(InfraConnectGraphQLQuery query, InfraConnectProjectionRoot projectionRoot){
1341-
// String request = new GraphQLQueryRequest(query, projectionRoot).serialize();
1342-
// GraphQLResponse response = graphQLClient.query(request);
1343-
// return response.extractValueAsObject("infraConnect", new TypeRef<InfraActionResponse>(){});
1341+
// String request = new GraphQLQueryRequest(query).serialize();
1342+
// return graphQLClient.query(request, "getInfraEvents", new TypeRef<InfraEventResponse>(){});
13441343
// }
13451344

13461345
private String sanitizeURL(String url) {
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package io.litmuschaos;
2+
3+
/**
4+
* The basic configuration of this SDK.
5+
*/
6+
public class LitmusConfig {
7+
8+
public static final LitmusConfig DEFAULT = new LitmusConfig() {
9+
10+
void throwException() {
11+
throw new UnsupportedOperationException("This config is immutable");
12+
}
13+
14+
@Override
15+
public void setHttpClientReadTimeoutMillis(Long httpClientReadTimeoutMillis) {
16+
throwException();
17+
}
18+
19+
@Override
20+
public void setHttpClientWriteTimeoutMillis(Long httpClientWriteTimeoutMillis) {
21+
throwException();
22+
}
23+
24+
@Override
25+
public void setHttpClientCallTimeoutMillis(Long httpClientCallTimeoutMillis) {
26+
throwException();
27+
}
28+
29+
@Override
30+
public void setHttpClientConnectTimeoutMillis(Long httpClientConnectTimeoutMillis) {
31+
throwException();
32+
}
33+
};
34+
35+
/**
36+
* The underlying HTTP client's read timeout (in milliseconds). The default is 10 seconds.
37+
*/
38+
private Long httpClientReadTimeoutMillis;
39+
40+
/**
41+
* The underlying HTTP client's write timeout (in milliseconds). The default is 10 seconds.
42+
*/
43+
private Long httpClientWriteTimeoutMillis;
44+
45+
/**
46+
* The underlying HTTP client's call timeout (in milliseconds).
47+
* By default, there is no timeout for complete calls while there is for connect/write/read actions within a call.
48+
*/
49+
private Long httpClientCallTimeoutMillis;
50+
51+
/**
52+
* The underlying HTTP client's connect timeout (in milliseconds). The default is 10 seconds.
53+
*/
54+
private Long httpClientConnectTimeoutMillis;
55+
56+
public void setHttpClientReadTimeoutMillis(Long httpClientReadTimeoutMillis) {
57+
this.httpClientReadTimeoutMillis = httpClientReadTimeoutMillis;
58+
}
59+
60+
public void setHttpClientWriteTimeoutMillis(Long httpClientWriteTimeoutMillis) {
61+
this.httpClientWriteTimeoutMillis = httpClientWriteTimeoutMillis;
62+
}
63+
64+
public void setHttpClientCallTimeoutMillis(Long httpClientCallTimeoutMillis) {
65+
this.httpClientCallTimeoutMillis = httpClientCallTimeoutMillis;
66+
}
67+
68+
public void setHttpClientConnectTimeoutMillis(Long httpClientConnectTimeoutMillis) {
69+
this.httpClientConnectTimeoutMillis = httpClientConnectTimeoutMillis;
70+
}
71+
72+
public Long getHttpClientReadTimeoutMillis() {
73+
return httpClientReadTimeoutMillis;
74+
}
75+
76+
public Long getHttpClientWriteTimeoutMillis() {
77+
return httpClientWriteTimeoutMillis;
78+
}
79+
80+
public Long getHttpClientCallTimeoutMillis() {
81+
return httpClientCallTimeoutMillis;
82+
}
83+
84+
public Long getHttpClientConnectTimeoutMillis() {
85+
return httpClientConnectTimeoutMillis;
86+
}
87+
}

0 commit comments

Comments
 (0)