Skip to content

Commit 5d12f09

Browse files
author
Adam Soos
committed
RCB-597: handling ErrorResponses
1 parent 25fa222 commit 5d12f09

3 files changed

Lines changed: 20 additions & 20 deletions

File tree

api/src/main/java/com/basistech/rosette/api/HttpRosetteAPI.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -620,21 +620,19 @@ private <T extends Object> T getResponse(HttpResponse httpResponse, Class<T> cla
620620
* @param request the request object
621621
* @param responseClass Response's class
622622
* @return RequestThread which when started sends the predefined request through this http client
623-
* @param <R> Type of the response
624623
*/
625-
public <R extends Response> RosetteRequest<R> createRosetteRequest(String endpoint,
626-
Request request,
627-
Class<R> responseClass) {
628-
return new RosetteRequest<>(this, request, endpoint, responseClass);
624+
public RosetteRequest createRosetteRequest(String endpoint,
625+
Request request,
626+
Class<? extends Response> responseClass) {
627+
return new RosetteRequest(this, request, endpoint, responseClass);
629628
}
630629

631630
/**
632631
* Sends the request concurrently
633632
* @param request the request to be sent
634633
* @return A Future with the response of the request
635-
* @param <R> type of the response object
636634
*/
637-
public <R extends Response> Future<R> submitRequest(RosetteRequest<R> request) {
635+
public Future<Response> submitRequest(RosetteRequest request) {
638636
return this.threadPool.submit(request);
639637
}
640638

@@ -643,7 +641,7 @@ public <R extends Response> Future<R> submitRequest(RosetteRequest<R> request) {
643641
* @param requests list of the requests to be sent
644642
* @return A list of Futures with the responses to the requests
645643
*/
646-
public List<Future<? extends Response>> submitRequests(Collection<RosetteRequest<? extends Response>> requests) {
644+
public List<Future<Response>> submitRequests(Collection<RosetteRequest> requests) {
647645
return requests.stream()
648646
.map(this::submitRequest)
649647
.collect(Collectors.toList());

api/src/main/java/com/basistech/rosette/api/RosetteRequest.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,34 @@
2222

2323
/**
2424
* This class encompasses a future request that can be sent concurrently
25-
* @param <R> type of the response object
2625
*/
27-
public class RosetteRequest<R extends Response> implements Callable<R> {
26+
public class RosetteRequest implements Callable<Response> {
2827
private final HttpRosetteAPI api;
2928
private final Request request;
3029
private final String servicePath;
31-
private final Class<R> responseClass;
32-
private R response;
30+
private final Class<? extends Response> responseClass;
31+
private Response response;
3332

3433
RosetteRequest(HttpRosetteAPI api,
3534
Request request,
36-
String servicePath, Class<R> responseClass) {
35+
String servicePath, Class<? extends Response> responseClass) {
3736
this.api = api;
3837
this.request = request;
3938
this.servicePath = servicePath;
4039
this.responseClass = responseClass;
4140
}
4241

4342
@Override
44-
public R call() {
45-
this.response = api.perform(this.servicePath, this.request, this.responseClass);
43+
public Response call() {
44+
try {
45+
this.response = api.perform(this.servicePath, this.request, this.responseClass);
46+
} catch (HttpRosetteAPIException ex) {
47+
this.response = ex.getErrorResponse();
48+
}
4649
return this.response;
4750
}
4851

49-
public R getResponse() {
52+
public Response getResponse() {
5053
return this.response;
5154
}
5255
}

examples/src/main/java/com/basistech/rosette/examples/ConcurrencyExample.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private void run() throws IOException, ExecutionException, InterruptedException
6767
.connectionConcurrency(maximumConcurrency)
6868
.build();
6969

70-
List<RosetteRequest<? extends Response>> threads = new ArrayList<>();
70+
List<RosetteRequest> threads = new ArrayList<>();
7171
// Setting up entities request
7272
String entitiesTextData =
7373
"The Securities and Exchange Commission today announced the leadership of the agency’s trial unit. "
@@ -89,10 +89,9 @@ private void run() throws IOException, ExecutionException, InterruptedException
8989
LanguageResponse.class)
9090
);
9191
// Setting up morphology request
92-
String morphologyCompleteData = "The quick brown fox jumped over the lazy dog. 👍🏾 Yes he did. B)";
9392
threads.add(
9493
rosetteApi.createRosetteRequest(MORPHOLOGY_SERVICE_PATH + "/" + MorphologicalFeature.COMPLETE,
95-
DocumentRequest.<MorphologyOptions>builder().content(morphologyCompleteData).build(),
94+
DocumentRequest.<MorphologyOptions>builder().build(),
9695
MorphologyResponse.class)
9796
);
9897
//Setting up names deduplication request
@@ -125,7 +124,7 @@ private void run() throws IOException, ExecutionException, InterruptedException
125124
);
126125

127126
// start the threads
128-
List<Future<? extends Response>> futures = rosetteApi.submitRequests(threads);
127+
List<Future<Response>> futures = rosetteApi.submitRequests(threads);
129128

130129
// wait for the threads to finish
131130
for (int i = 0; i < threads.size(); i++) {

0 commit comments

Comments
 (0)