-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
Showing
7 changed files
with
392 additions
and
290 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
src/main/java/io/litmuschaos/exception/IOExceptionHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
46
src/main/java/io/litmuschaos/graphql/LitmusGraphQLClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.