|
5 | 5 |
|
6 | 6 | import com.apollographql.apollo.ApolloCall; |
7 | 7 | import com.apollographql.apollo.ApolloClient; |
| 8 | +import com.apollographql.apollo.api.Mutation; |
8 | 9 | import com.apollographql.apollo.api.Operation; |
9 | 10 | import com.apollographql.apollo.api.Query; |
10 | 11 | import com.apollographql.apollo.api.Response; |
|
14 | 15 | import org.openimis.imisclaims.network.apollo.DateCustomTypeAdapter; |
15 | 16 | import org.openimis.imisclaims.network.apollo.DateTimeCustomTypeAdapter; |
16 | 17 | import org.openimis.imisclaims.network.apollo.DecimalCustomTypeAdapter; |
| 18 | +import org.openimis.imisclaims.network.exception.HttpException; |
17 | 19 | import org.openimis.imisclaims.network.util.OkHttpUtils; |
18 | 20 | import org.openimis.imisclaims.type.CustomType; |
19 | 21 |
|
| 22 | +import java.net.HttpURLConnection; |
20 | 23 | import java.util.concurrent.Semaphore; |
21 | 24 | import java.util.concurrent.TimeUnit; |
22 | 25 | import java.util.concurrent.TimeoutException; |
@@ -62,4 +65,54 @@ public void onFailure(@NonNull ApolloException e) { |
62 | 65 | } |
63 | 66 | return responses[0]; |
64 | 67 | } |
| 68 | + |
| 69 | + @NonNull |
| 70 | + @WorkerThread |
| 71 | + protected <T extends Operation.Data> Response<T> makeSynchronous(Operation<T, ?, ?> query) throws Exception { |
| 72 | + Semaphore semaphore = new Semaphore(0); |
| 73 | + final Exception[] exceptions = new Exception[1]; |
| 74 | + final Response<T>[] responses = new Response[1]; |
| 75 | + ApolloCall<?> call; |
| 76 | + if (query instanceof Query) { |
| 77 | + call = apolloClient.query((Query<T, ?, ?>) query); |
| 78 | + } else if(query instanceof Mutation) { |
| 79 | + call = apolloClient.mutate((Mutation<T, ?, ?>) query); |
| 80 | + } else { |
| 81 | + throw new IllegalArgumentException("Query is unsupported"); |
| 82 | + } |
| 83 | + call.enqueue(new ApolloCall.Callback() { |
| 84 | + @Override |
| 85 | + public void onResponse(@NonNull Response response) { |
| 86 | + responses[0] = response; |
| 87 | + semaphore.release(); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void onFailure(@NonNull ApolloException e) { |
| 92 | + exceptions[0] = e; |
| 93 | + semaphore.release(); |
| 94 | + } |
| 95 | + }); |
| 96 | + if (!semaphore.tryAcquire(TIME_OUT_IN_MS, TimeUnit.MILLISECONDS)) { |
| 97 | + throw new TimeoutException("Call couldn't finish within " + TIME_OUT_IN_MS + "ms"); |
| 98 | + } |
| 99 | + Exception exception = exceptions[0]; |
| 100 | + if (exception != null) { |
| 101 | + throw exception; |
| 102 | + } |
| 103 | + Response<T> response = responses[0]; |
| 104 | + if (response.hasErrors()) { |
| 105 | + String details = response.getErrors().get(0).getMessage(); |
| 106 | + if (details.equals("User not authorized for this operation")) { |
| 107 | + throw new HttpException( |
| 108 | + HttpURLConnection.HTTP_UNAUTHORIZED, |
| 109 | + details, |
| 110 | + null, |
| 111 | + null |
| 112 | + ); |
| 113 | + } |
| 114 | + throw new RuntimeException(response.toString()); |
| 115 | + } |
| 116 | + return response; |
| 117 | + } |
65 | 118 | } |
0 commit comments