use of org.folio.rest.tools.client.exceptions.PopulateTemplateException in project raml-module-builder by folio-org.
the class HttpModuleClient2 method chainedRequest.
/**
* A request that should be used within a thenCompose() completable future call and receive as
* input a Response object from the previous request.
* The chainedRequest will
* 1. callback to the passed in Consumer with the Response of the previous request for handling
* 2. if the passed in Response contains errors - the current request will not be sent and a
* completable future will be returned indicating that the request was not run
* 3. replace placeholder in the url {a.b[0]} with actual values appearing in the json passed in
* by the thenCompose(). For example: passing in:
* http://localhost:9130/users/{users[0].username}
* will look in the passed in json for the value found in the first user in a json array[].username
* NOTE that if a template has a placeholder, and the path indicated in the placeholder does not
* exist in the passed in Response - the current request will not be sent and a completeable future
* with a Response (containing the error) will be returned
* 4. build a cql via values in the passed in json
* 5. Send the request
* @param urlTempate
* @param headers
* @param inheritOkapiHeaders - take all okapi headers in passed in Response and over write / add to this request
* @param cql
* @param processPassedInResponse
* @return
*/
@Override
public Function<Response, CompletableFuture<Response>> chainedRequest(String urlTempate, Map<String, String> headers, boolean inheritOkapiHeaders, boolean cache, BuildCQL cql, Consumer<Response> processPassedInResponse) {
try {
List<String> replace = getTagValues(urlTempate);
return (resp) -> {
// create a new request based on the content of the passed in response (resp)//
try {
// response for errors / exceptions and return accordingly if found//
if (processPassedInResponse != null) {
processPassedInResponse.accept(resp);
}
if (resp.getError() != null || resp.getException() != null) {
CompletableFuture<Response> cf = new CompletableFuture<>();
PreviousRequestException pre = new PreviousRequestException("for template, " + urlTempate);
cf.complete(createResponse(urlTempate, pre));
return cf;
}
int size = replace.size();
String newURL = urlTempate;
if (size > 0) {
JsonPathParser jpp = new JsonPathParser(resp.getBody());
for (int i = 0; i < size; i++) {
String val = (String) jpp.getValueAt(replace.get(i));
if (val == null) {
log.error("Unable to replace {" + replace.get(i) + "} with content from received json result, does the json contain this field? Endpoint: " + resp.endpoint);
PopulateTemplateException pe = new PopulateTemplateException("Missing {" + replace.get(i) + "} value from results received from endpoint " + resp.endpoint);
CompletableFuture<Response> cf = new CompletableFuture<>();
cf.complete(createResponse(urlTempate, pe));
return cf;
}
newURL = urlTempate.replace("{" + replace.get(i) + "}", val);
}
}
if (cql != null) {
cql.setResponse(resp);
}
// call request//
if (inheritOkapiHeaders) {
mergeHeaders(headers, resp.headers, resp.endpoint);
}
return request(newURL, headers, cache, cql);
} catch (Exception e) {
CompletableFuture<Response> cf = new CompletableFuture<>();
cf.complete(createResponse(urlTempate, e));
return cf;
}
};
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
Aggregations