Search in sources :

Example 1 with PopulateTemplateException

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;
}
Also used : LoadingCache(com.google.common.cache.LoadingCache) 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) LoggerFactory(io.vertx.core.logging.LoggerFactory) ArrayList(java.util.ArrayList) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) Matcher(java.util.regex.Matcher) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) HttpClientOptions(io.vertx.core.http.HttpClientOptions) PopulateTemplateException(org.folio.rest.tools.client.exceptions.PopulateTemplateException) Logger(io.vertx.core.logging.Logger) 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) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CacheLoader(com.google.common.cache.CacheLoader) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) 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) CacheStats(com.google.common.cache.CacheStats) HttpClient(io.vertx.core.http.HttpClient) HttpClientResponse(io.vertx.core.http.HttpClientResponse) 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 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 HttpClient (io.vertx.core.http.HttpClient)1 HttpClientOptions (io.vertx.core.http.HttpClientOptions)1 HttpClientRequest (io.vertx.core.http.HttpClientRequest)1 HttpClientResponse (io.vertx.core.http.HttpClientResponse)1 HttpMethod (io.vertx.core.http.HttpMethod)1 JsonArray (io.vertx.core.json.JsonArray)1 JsonObject (io.vertx.core.json.JsonObject)1 Logger (io.vertx.core.logging.Logger)1 LoggerFactory (io.vertx.core.logging.LoggerFactory)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1