Skip to content

Commit

Permalink
feat: refactor graphql endpoint (#50)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
jemlog authored Feb 3, 2025
1 parent 801c943 commit c98fd26
Show file tree
Hide file tree
Showing 7 changed files with 392 additions and 290 deletions.
465 changes: 269 additions & 196 deletions src/main/java/io/litmuschaos/LitmusClient.java

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/main/java/io/litmuschaos/constants/HttpStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.litmuschaos.constants;

public class HttpStatus {
public static final int OK = 200;
public static final int BAD_REQUEST = 400;
public static final int UNAUTHORIZED = 401;
public static final int FORBIDDEN = 403;
public static final int NOT_FOUND = 404;
}
16 changes: 16 additions & 0 deletions src/main/java/io/litmuschaos/exception/IOExceptionHolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.litmuschaos.exception;

import java.io.IOException;

public class IOExceptionHolder extends RuntimeException {

IOException exception;

public IOExceptionHolder(IOException exception) {
this.exception = exception;
}

public IOException getException() {
return exception;
}
}
46 changes: 29 additions & 17 deletions src/main/java/io/litmuschaos/graphql/LitmusGraphQLClient.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,59 @@
package io.litmuschaos.graphql;
import com.jayway.jsonpath.TypeRef;
import com.netflix.graphql.dgs.client.GraphQLClient;
import com.netflix.graphql.dgs.client.GraphQLResponse;
import com.netflix.graphql.dgs.client.HttpResponse;
import java.io.IOException;
import java.util.Map;

import io.litmuschaos.constants.HttpStatus;
import io.litmuschaos.exception.IOExceptionHolder;
import io.litmuschaos.exception.LitmusApiException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static io.litmuschaos.constants.RequestHeaders.BEARER;
import static io.litmuschaos.constants.RequestHeaders.*;


public class LitmusGraphQLClient {

public final GraphQLClient client;

public LitmusGraphQLClient(OkHttpClient okHttpClient, String host, String token) {
client = GraphQLClient.createCustom(host, ((url, headers, body) -> {
client = GraphQLClient.createCustom(host, (url, headers, body) -> {
Request request = new Request.Builder()
.url(url)
.addHeader("Authorization", BEARER + " " + token)
.post(RequestBody.create(body, MediaType.parse("application/json"))
.addHeader(AUTHORIZATION, BEARER + " " + token)
.post(RequestBody.create(body, MediaType.parse(APPLICATION_JSON + "; " + CHARSET_UTF_8))
).build();

try (Response response = okHttpClient.newCall(request).execute()) {
if(!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
return new HttpResponse(response.code(), response.body().string());
}catch (IOException e) {
throw new RuntimeException(e);
return new HttpResponse(HttpStatus.OK, response.body().string());
} catch (IOException e) {
// requestExecutor cannot throw a checked exception, so wrapped IOException with IOExceptionHolder which is unchecked exception
throw new IOExceptionHolder(e);
}
}));
});
}

public <T> T query(String query, String operationName, TypeRef<T> type) throws LitmusApiException, IOException {
try {
GraphQLResponse response = client.executeQuery(query);
return handleResponse(response, operationName, type);
} catch (IOExceptionHolder e) {
throw e.getException();
}
}

public GraphQLResponse query(String query, Map<String,Object> variables){
return client.executeQuery(query, variables);
private <T> T handleResponse(GraphQLResponse response, String operationName, TypeRef<T> type) throws LitmusApiException{
validateResponse(response);
return response.extractValueAsObject(operationName, type);
}

public GraphQLResponse query(String query){
return client.executeQuery(query);
private void validateResponse(GraphQLResponse response) throws LitmusApiException {
if (response.hasErrors()){
throw new LitmusApiException(response.getErrors().get(0).getMessage());
}
}
}
9 changes: 5 additions & 4 deletions src/main/java/io/litmuschaos/http/HttpResponseHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.litmuschaos.http;

import com.google.gson.*;
import io.litmuschaos.constants.HttpStatus;
import io.litmuschaos.constants.ResponseBodyFields;
import io.litmuschaos.exception.LitmusApiException;
import io.litmuschaos.exception.detailed.*;
Expand Down Expand Up @@ -37,13 +38,13 @@ private void handleErrorResponse(Response response) throws LitmusApiException, I
}
}
switch (response.code()) {
case 400:
case HttpStatus.BAD_REQUEST:
throw new BadRequestException(errorMessage);
case 401:
case HttpStatus.UNAUTHORIZED:
throw new UnauthorizedException(errorMessage);
case 403:
case HttpStatus.FORBIDDEN:
throw new ForbiddenException(errorMessage);
case 404:
case HttpStatus.NOT_FOUND:
throw new NotFoundException(errorMessage);
default:
throw new InternalServerErrorException(errorMessage);
Expand Down
11 changes: 1 addition & 10 deletions src/main/java/io/litmuschaos/http/LitmusHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

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

public class LitmusHttpClient implements AutoCloseable{
public class LitmusHttpClient {

private final OkHttpClient okHttpClient;
private final HttpResponseHandler httpResponseHandler;
Expand Down Expand Up @@ -108,13 +108,4 @@ private String toJson(Object object) {
Gson gson = new Gson();
return gson.toJson(object);
}

@Override
public void close() throws Exception {
this.okHttpClient.dispatcher().executorService().shutdown();
this.okHttpClient.connectionPool().evictAll();
if (this.okHttpClient.cache() != null) {
this.okHttpClient.cache().close();
}
}
}
Loading

0 comments on commit c98fd26

Please sign in to comment.