Untitled

mail@pastecode.io avatar
unknown
java
a year ago
1.1 kB
1
Indexable
Never
MyEndpoint endpoint = RestClientBuilder.newBuilder()
        .baseUri("http://example.com")
        .build(MyEndpoint.class);

CompletionStage<Response> responseStage = endpoint.getMyResource("value1", "value2");

String missingHeader = "value-from-mobile-app";

BiFunction<Response, Throwable, CompletionStage<Response>> handleStatus = (response, throwable) -> {
    if (response != null && response.getStatus() == 409) {
        MultivaluedMap<String, Object> headers = response.getHeaders();
        if (headers.containsKey("Missing-Header")) {
            headers.add("pin", missingHeader);
            // Call the endpoint again with the missing header
            return endpoint.getMyResource("value1", "value2");
        }
    }
    // Handle other response codes here
    return CompletableFuture.completedFuture(response);
};

responseStage.thenCompose(handleStatus)
        .thenApply(response -> {
            if (response.getStatus() == 200) {
                return response.readEntity(MyResource.class);
            } else {
                throw new MyException("Failed to get resource");
            }
        });