Skip to content

Commit c98fd26

Browse files
authored
feat: refactor graphql endpoint (#50)
* feat: add http status constant Signed-off-by: Jemin <[email protected]> * feat: move code that converts the type of the response into graphQLClient Signed-off-by: Jemin <[email protected]> * feat: update tests to throw LitmusApiException Signed-off-by: Jemin <[email protected]> * feat: extract handling response method Signed-off-by: Jemin <[email protected]> * feat: remove space Signed-off-by: Jemin <[email protected]> * feat: update graphQLClient request body media type Signed-off-by: Jemin <[email protected]> * feat: extract okHttpClient close method Signed-off-by: Jemin <[email protected]> * feat: update graphqlClient to throw IOException Signed-off-by: Jemin <[email protected]> --------- Signed-off-by: Jemin <[email protected]>
1 parent 801c943 commit c98fd26

File tree

7 files changed

+392
-290
lines changed

7 files changed

+392
-290
lines changed

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

+269-196
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.litmuschaos.constants;
2+
3+
public class HttpStatus {
4+
public static final int OK = 200;
5+
public static final int BAD_REQUEST = 400;
6+
public static final int UNAUTHORIZED = 401;
7+
public static final int FORBIDDEN = 403;
8+
public static final int NOT_FOUND = 404;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.litmuschaos.exception;
2+
3+
import java.io.IOException;
4+
5+
public class IOExceptionHolder extends RuntimeException {
6+
7+
IOException exception;
8+
9+
public IOExceptionHolder(IOException exception) {
10+
this.exception = exception;
11+
}
12+
13+
public IOException getException() {
14+
return exception;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,59 @@
11
package io.litmuschaos.graphql;
2+
import com.jayway.jsonpath.TypeRef;
23
import com.netflix.graphql.dgs.client.GraphQLClient;
34
import com.netflix.graphql.dgs.client.GraphQLResponse;
45
import com.netflix.graphql.dgs.client.HttpResponse;
56
import java.io.IOException;
6-
import java.util.Map;
7-
7+
import io.litmuschaos.constants.HttpStatus;
8+
import io.litmuschaos.exception.IOExceptionHolder;
9+
import io.litmuschaos.exception.LitmusApiException;
810
import okhttp3.MediaType;
911
import okhttp3.OkHttpClient;
1012
import okhttp3.Request;
1113
import okhttp3.RequestBody;
1214
import okhttp3.Response;
1315

14-
import static io.litmuschaos.constants.RequestHeaders.BEARER;
16+
import static io.litmuschaos.constants.RequestHeaders.*;
1517

1618

1719
public class LitmusGraphQLClient {
1820

1921
public final GraphQLClient client;
2022

2123
public LitmusGraphQLClient(OkHttpClient okHttpClient, String host, String token) {
22-
client = GraphQLClient.createCustom(host, ((url, headers, body) -> {
24+
client = GraphQLClient.createCustom(host, (url, headers, body) -> {
2325
Request request = new Request.Builder()
2426
.url(url)
25-
.addHeader("Authorization", BEARER + " " + token)
26-
.post(RequestBody.create(body, MediaType.parse("application/json"))
27+
.addHeader(AUTHORIZATION, BEARER + " " + token)
28+
.post(RequestBody.create(body, MediaType.parse(APPLICATION_JSON + "; " + CHARSET_UTF_8))
2729
).build();
2830

2931
try (Response response = okHttpClient.newCall(request).execute()) {
30-
if(!response.isSuccessful()) {
31-
throw new IOException("Unexpected code " + response);
32-
}
33-
return new HttpResponse(response.code(), response.body().string());
34-
}catch (IOException e) {
35-
throw new RuntimeException(e);
32+
return new HttpResponse(HttpStatus.OK, response.body().string());
33+
} catch (IOException e) {
34+
// requestExecutor cannot throw a checked exception, so wrapped IOException with IOExceptionHolder which is unchecked exception
35+
throw new IOExceptionHolder(e);
3636
}
37-
}));
37+
});
38+
}
39+
40+
public <T> T query(String query, String operationName, TypeRef<T> type) throws LitmusApiException, IOException {
41+
try {
42+
GraphQLResponse response = client.executeQuery(query);
43+
return handleResponse(response, operationName, type);
44+
} catch (IOExceptionHolder e) {
45+
throw e.getException();
46+
}
3847
}
3948

40-
public GraphQLResponse query(String query, Map<String,Object> variables){
41-
return client.executeQuery(query, variables);
49+
private <T> T handleResponse(GraphQLResponse response, String operationName, TypeRef<T> type) throws LitmusApiException{
50+
validateResponse(response);
51+
return response.extractValueAsObject(operationName, type);
4252
}
4353

44-
public GraphQLResponse query(String query){
45-
return client.executeQuery(query);
54+
private void validateResponse(GraphQLResponse response) throws LitmusApiException {
55+
if (response.hasErrors()){
56+
throw new LitmusApiException(response.getErrors().get(0).getMessage());
57+
}
4658
}
4759
}

src/main/java/io/litmuschaos/http/HttpResponseHandler.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.litmuschaos.http;
22

33
import com.google.gson.*;
4+
import io.litmuschaos.constants.HttpStatus;
45
import io.litmuschaos.constants.ResponseBodyFields;
56
import io.litmuschaos.exception.LitmusApiException;
67
import io.litmuschaos.exception.detailed.*;
@@ -37,13 +38,13 @@ private void handleErrorResponse(Response response) throws LitmusApiException, I
3738
}
3839
}
3940
switch (response.code()) {
40-
case 400:
41+
case HttpStatus.BAD_REQUEST:
4142
throw new BadRequestException(errorMessage);
42-
case 401:
43+
case HttpStatus.UNAUTHORIZED:
4344
throw new UnauthorizedException(errorMessage);
44-
case 403:
45+
case HttpStatus.FORBIDDEN:
4546
throw new ForbiddenException(errorMessage);
46-
case 404:
47+
case HttpStatus.NOT_FOUND:
4748
throw new NotFoundException(errorMessage);
4849
default:
4950
throw new InternalServerErrorException(errorMessage);

src/main/java/io/litmuschaos/http/LitmusHttpClient.java

+1-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import static io.litmuschaos.constants.RequestHeaders.*;
1313

14-
public class LitmusHttpClient implements AutoCloseable{
14+
public class LitmusHttpClient {
1515

1616
private final OkHttpClient okHttpClient;
1717
private final HttpResponseHandler httpResponseHandler;
@@ -108,13 +108,4 @@ private String toJson(Object object) {
108108
Gson gson = new Gson();
109109
return gson.toJson(object);
110110
}
111-
112-
@Override
113-
public void close() throws Exception {
114-
this.okHttpClient.dispatcher().executorService().shutdown();
115-
this.okHttpClient.connectionPool().evictAll();
116-
if (this.okHttpClient.cache() != null) {
117-
this.okHttpClient.cache().close();
118-
}
119-
}
120111
}

0 commit comments

Comments
 (0)