|
1 | 1 | package io.litmuschaos.graphql;
|
| 2 | +import com.jayway.jsonpath.TypeRef; |
2 | 3 | import com.netflix.graphql.dgs.client.GraphQLClient;
|
3 | 4 | import com.netflix.graphql.dgs.client.GraphQLResponse;
|
4 | 5 | import com.netflix.graphql.dgs.client.HttpResponse;
|
5 | 6 | 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; |
8 | 10 | import okhttp3.MediaType;
|
9 | 11 | import okhttp3.OkHttpClient;
|
10 | 12 | import okhttp3.Request;
|
11 | 13 | import okhttp3.RequestBody;
|
12 | 14 | import okhttp3.Response;
|
13 | 15 |
|
14 |
| -import static io.litmuschaos.constants.RequestHeaders.BEARER; |
| 16 | +import static io.litmuschaos.constants.RequestHeaders.*; |
15 | 17 |
|
16 | 18 |
|
17 | 19 | public class LitmusGraphQLClient {
|
18 | 20 |
|
19 | 21 | public final GraphQLClient client;
|
20 | 22 |
|
21 | 23 | public LitmusGraphQLClient(OkHttpClient okHttpClient, String host, String token) {
|
22 |
| - client = GraphQLClient.createCustom(host, ((url, headers, body) -> { |
| 24 | + client = GraphQLClient.createCustom(host, (url, headers, body) -> { |
23 | 25 | Request request = new Request.Builder()
|
24 | 26 | .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)) |
27 | 29 | ).build();
|
28 | 30 |
|
29 | 31 | 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); |
36 | 36 | }
|
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 | + } |
38 | 47 | }
|
39 | 48 |
|
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); |
42 | 52 | }
|
43 | 53 |
|
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 | + } |
46 | 58 | }
|
47 | 59 | }
|
0 commit comments