Search in sources :

Example 1 with PreviousRequestException

use of org.folio.rest.tools.client.exceptions.PreviousRequestException 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;
}
Also used : WebClientOptions(io.vertx.ext.web.client.WebClientOptions) HttpResponse(io.vertx.ext.web.client.HttpResponse) LoadingCache(com.google.common.cache.LoadingCache) RestVerticle(org.folio.rest.RestVerticle) WebClient(io.vertx.ext.web.client.WebClient) XOkapiHeaders(org.folio.okapi.common.XOkapiHeaders) MultiMap(io.vertx.core.MultiMap) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) VertxUtils(org.folio.rest.tools.utils.VertxUtils) ArrayList(java.util.ArrayList) Matcher(java.util.regex.Matcher) Map(java.util.Map) AsyncResult(io.vertx.core.AsyncResult) PopulateTemplateException(org.folio.rest.tools.client.exceptions.PopulateTemplateException) PreviousRequestException(org.folio.rest.tools.client.exceptions.PreviousRequestException) Vertx(io.vertx.core.Vertx) JsonPathParser(org.folio.rest.tools.parser.JsonPathParser) PostgresClient(org.folio.rest.persist.PostgresClient) HttpRequest(io.vertx.ext.web.client.HttpRequest) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CacheLoader(com.google.common.cache.CacheLoader) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Buffer(io.vertx.core.buffer.Buffer) HttpClientInterface(org.folio.rest.tools.client.interfaces.HttpClientInterface) HttpMethod(io.vertx.core.http.HttpMethod) Pattern(java.util.regex.Pattern) CacheBuilder(com.google.common.cache.CacheBuilder) Handler(io.vertx.core.Handler) LogManager(org.apache.logging.log4j.LogManager) CacheStats(com.google.common.cache.CacheStats) HttpResponse(io.vertx.ext.web.client.HttpResponse) CompletableFuture(java.util.concurrent.CompletableFuture) PreviousRequestException(org.folio.rest.tools.client.exceptions.PreviousRequestException) PopulateTemplateException(org.folio.rest.tools.client.exceptions.PopulateTemplateException) JsonPathParser(org.folio.rest.tools.parser.JsonPathParser) PopulateTemplateException(org.folio.rest.tools.client.exceptions.PopulateTemplateException) PreviousRequestException(org.folio.rest.tools.client.exceptions.PreviousRequestException)

Aggregations

CacheBuilder (com.google.common.cache.CacheBuilder)1 CacheLoader (com.google.common.cache.CacheLoader)1 CacheStats (com.google.common.cache.CacheStats)1 LoadingCache (com.google.common.cache.LoadingCache)1 AsyncResult (io.vertx.core.AsyncResult)1 Handler (io.vertx.core.Handler)1 MultiMap (io.vertx.core.MultiMap)1 Vertx (io.vertx.core.Vertx)1 Buffer (io.vertx.core.buffer.Buffer)1 HttpMethod (io.vertx.core.http.HttpMethod)1 HttpRequest (io.vertx.ext.web.client.HttpRequest)1 HttpResponse (io.vertx.ext.web.client.HttpResponse)1 WebClient (io.vertx.ext.web.client.WebClient)1 WebClientOptions (io.vertx.ext.web.client.WebClientOptions)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 TimeUnit (java.util.concurrent.TimeUnit)1