/** * Same as {@link #getRemoteResource(Class)} except explicitly specifies the URL of the REST interface. * * @param interfaceClass The interface to create a proxy for. * @param restUrl The URL of the REST interface. * @return The new proxy interface. */ public <T> T getRemoteResource(final Class<T> interfaceClass, final Object restUrl) { return getRemoteResource(interfaceClass, restUrl, serializer, parser); }
/** * Perform a <code>GET</code> request against the specified URL. * * @param url * The URL of the remote REST resource. * Can be any of the following: {@link String}, {@link URI}, {@link URL}. * @return * A {@link RestCall} object that can be further tailored before executing the request and getting the response * as a parsed object. * @throws RestCallException If any authentication errors occurred. */ public RestCall doGet(Object url) throws RestCallException { return doCall("GET", url, false); }
/** * Same as {@link #run()} but allows you to run the call asynchronously. * * @return The HTTP status code. * @throws RestCallException If the executor service was not defined. * @see RestClientBuilder#executorService(ExecutorService, boolean) for defining the executor service for creating * {@link Future Futures}. */ public Future<Integer> runFuture() throws RestCallException { return client.getExecutorService(true).submit( new Callable<Integer>() { @Override /* Callable */ public Integer call() throws Exception { return run(); } } ); }
this.httpClientBuilder = httpClientBuilder; this.httpClient = httpClient; this.keepHttpClientOpen = getBooleanProperty(RESTCLIENT_keepHttpClientOpen, false); this.headers = getMapProperty(RESTCLIENT_headers, String.class); this.query = getMapProperty(RESTCLIENT_query, String.class); this.retries = getIntegerProperty(RESTCLIENT_retries, 1); this.retryInterval = getIntegerProperty(RESTCLIENT_retryInterval, -1); this.retryOn = getInstanceProperty(RESTCLIENT_retryOn, RetryOn.class, RetryOn.DEFAULT); this.debug = getBooleanProperty(RESTCLIENT_debug, false); this.executorServiceShutdownOnClose = getBooleanProperty(RESTCLIENT_executorServiceShutdownOnClose, false); this.rootUrl = StringUtils.nullIfEmpty(getStringProperty(RESTCLIENT_rootUri, "").replaceAll("\\/$", "")); Object o = getProperty(RESTCLIENT_serializer, Object.class, null); if (o instanceof Serializer) { this.serializer = ((Serializer)o).builder().apply(ps).build(); o = getProperty(RESTCLIENT_parser, Object.class, null); if (o instanceof Parser) { this.parser = ((Parser)o).builder().apply(ps).build(); this.partSerializer = getInstanceProperty(RESTCLIENT_partSerializer, HttpPartSerializer.class, OpenApiSerializer.class, ResourceResolver.FUZZY, ps); this.partParser = getInstanceProperty(RESTCLIENT_partParser, HttpPartParser.class, OpenApiParser.class, ResourceResolver.FUZZY, ps); this.executorService = getInstanceProperty(RESTCLIENT_executorService, ExecutorService.class, null); RestCallInterceptor[] rci = getInstanceArrayProperty(RESTCLIENT_interceptors, RestCallInterceptor.class, new RestCallInterceptor[0]); if (debug) rci = ArrayUtils.append(rci, RestCallLogger.DEFAULT);
/** * Instantiates a new {@link RestClientBuilder} object using the specified serializer and parser. * * <p> * Shortcut for calling <code>RestClient.<jsm>create</jsm>().serializer(s).parser(p);</code> * * @param s The serializer to use for output. * @param p The parser to use for input. * @return A new {@link RestClientBuilder} object. */ public static RestClientBuilder create(Serializer s, Parser p) { return create().serializer(s).parser(p); }
try (RestClient rc = RestClient.create().json().rootUrl("http://localhost:" + port).build()) { PetStore ps = rc.getRemoteResource(PetStore.class);
HttpPartSerializer s = getPartSerializer(); try (RestCall rc = doCall(httpMethod, url, httpMethod.equals("POST") || httpMethod.equals("PUT"))) {
@SuppressWarnings("resource") @Override /* ContextBuilder */ public RestClient build() { try { CloseableHttpClient c = httpClient != null ? httpClient : createHttpClient(); PropertyStore ps = psb.build(); return new RestClient(ps, httpClientBuilder, c); } catch (Exception e) { throw new RuntimeException(e); } }
@Override /* InvocationHandler */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { ResponseBeanPropertyMeta pm = rbm.getProperty(method.getName()); if (pm != null) { HttpPartParser pp = pm.getParser(p); HttpPartSchema schema = pm.getSchema(); String name = pm.getPartName(); ClassMeta<?> type = rc.getClassMeta(method.getGenericReturnType()); HttpPartType pt = pm.getPartType(); if (pt == RESPONSE_BODY) { responseBodySchema(schema); return getResponseBody(type); } if (pt == RESPONSE_HEADER) return getResponseHeader(pp, schema, name, type); if (pt == RESPONSE_STATUS) return getResponseCode(); } return null; }
Exception ex = null; try { response = client.execute(request); sc = (response == null || response.getStatusLine() == null) ? -1 : response.getStatusLine().getStatusCode(); } catch (Exception e) {
this.httpClientBuilder = httpClientBuilder; this.httpClient = httpClient; this.keepHttpClientOpen = getBooleanProperty(RESTCLIENT_keepHttpClientOpen, false); this.headers = getMapProperty(RESTCLIENT_headers, String.class); this.query = getMapProperty(RESTCLIENT_query, String.class); this.retries = getIntegerProperty(RESTCLIENT_retries, 1); this.retryInterval = getIntegerProperty(RESTCLIENT_retryInterval, -1); this.retryOn = getInstanceProperty(RESTCLIENT_retryOn, RetryOn.class, RetryOn.DEFAULT); this.debug = getBooleanProperty(RESTCLIENT_debug, false); this.executorServiceShutdownOnClose = getBooleanProperty(RESTCLIENT_executorServiceShutdownOnClose, false); this.rootUrl = StringUtils.nullIfEmpty(getStringProperty(RESTCLIENT_rootUri, "").replaceAll("\\/$", "")); Object o = getProperty(RESTCLIENT_serializer, Object.class, null); if (o instanceof Serializer) { this.serializer = ((Serializer)o).builder().apply(ps).build(); o = getProperty(RESTCLIENT_parser, Object.class, null); if (o instanceof Parser) { this.parser = ((Parser)o).builder().apply(ps).build(); this.partSerializer = getInstanceProperty(RESTCLIENT_partSerializer, HttpPartSerializer.class, OpenApiSerializer.class, ResourceResolver.FUZZY, ps); this.partParser = getInstanceProperty(RESTCLIENT_partParser, HttpPartParser.class, OpenApiParser.class, ResourceResolver.FUZZY, ps); this.executorService = getInstanceProperty(RESTCLIENT_executorService, ExecutorService.class, null); RestCallInterceptor[] rci = getInstanceArrayProperty(RESTCLIENT_interceptors, RestCallInterceptor.class, new RestCallInterceptor[0]); if (debug) rci = ArrayUtils.append(rci, RestCallLogger.DEFAULT);
/** * Instantiates a new {@link RestClientBuilder} object using the specified serializer and parser. * * <p> * Shortcut for calling <code>RestClient.<jsm>create</jsm>().serializer(s).parser(p);</code> * * @param s The serializer class to use for output. * @param p The parser class to use for input. * @return A new {@link RestClientBuilder} object. */ public static RestClientBuilder create(Class<? extends Serializer> s, Class<? extends Parser> p) { return create().serializer(s).parser(p); }
try (RestClient rc = RestClient.create().json().rootUrl("http://localhost:" + port).build()) { PetStore ps = rc.getRemoteResource(PetStore.class);
HttpPartSerializer s = getPartSerializer(); try (RestCall rc = doCall(httpMethod, url, httpMethod.equals("POST") || httpMethod.equals("PUT"))) {
@SuppressWarnings("resource") @Override /* ContextBuilder */ public RestClient build() { try { CloseableHttpClient c = httpClient != null ? httpClient : createHttpClient(); PropertyStore ps = psb.build(); return new RestClient(ps, httpClientBuilder, c); } catch (Exception e) { throw new RuntimeException(e); } }
@Override /* InvocationHandler */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { ResponseBeanPropertyMeta pm = rbm.getProperty(method.getName()); if (pm != null) { HttpPartParser pp = pm.getParser(p); HttpPartSchema schema = pm.getSchema(); String name = pm.getPartName(); ClassMeta<?> type = rc.getClassMeta(method.getGenericReturnType()); HttpPartType pt = pm.getPartType(); if (pt == RESPONSE_BODY) { responseBodySchema(schema); return getResponseBody(type); } if (pt == RESPONSE_HEADER) return getResponseHeader(pp, schema, name, type); if (pt == RESPONSE_STATUS) return getResponseCode(); } return null; }
Exception ex = null; try { response = client.execute(request); sc = (response == null || response.getStatusLine() == null) ? -1 : response.getStatusLine().getStatusCode(); } catch (Exception e) {
this.httpClientBuilder = httpClientBuilder; this.httpClient = httpClient; this.keepHttpClientOpen = getBooleanProperty(RESTCLIENT_keepHttpClientOpen, false); this.headers = getMapProperty(RESTCLIENT_headers, String.class); this.query = getMapProperty(RESTCLIENT_query, String.class); this.retries = getIntegerProperty(RESTCLIENT_retries, 1); this.retryInterval = getIntegerProperty(RESTCLIENT_retryInterval, -1); this.retryOn = getInstanceProperty(RESTCLIENT_retryOn, RetryOn.class, RetryOn.DEFAULT); this.debug = getBooleanProperty(RESTCLIENT_debug, false); this.executorServiceShutdownOnClose = getBooleanProperty(RESTCLIENT_executorServiceShutdownOnClose, false); this.rootUrl = StringUtils.nullIfEmpty(getStringProperty(RESTCLIENT_rootUri, "").replaceAll("\\/$", "")); Object o = getProperty(RESTCLIENT_serializer, Object.class, null); if (o instanceof Serializer) { this.serializer = ((Serializer)o).builder().apply(ps).build(); o = getProperty(RESTCLIENT_parser, Object.class, null); if (o instanceof Parser) { this.parser = ((Parser)o).builder().apply(ps).build(); this.partSerializer = getInstanceProperty(RESTCLIENT_partSerializer, HttpPartSerializer.class, OpenApiSerializer.class, ResourceResolver.FUZZY, ps); this.partParser = getInstanceProperty(RESTCLIENT_partParser, HttpPartParser.class, OpenApiParser.class, ResourceResolver.FUZZY, ps); this.executorService = getInstanceProperty(RESTCLIENT_executorService, ExecutorService.class, null); RestCallInterceptor[] rci = getInstanceArrayProperty(RESTCLIENT_interceptors, RestCallInterceptor.class, new RestCallInterceptor[0]); if (debug) rci = ArrayUtils.append(rci, RestCallLogger.DEFAULT);
/** * Instantiates a new {@link RestClientBuilder} object using the specified serializer and parser. * * <p> * Shortcut for calling <code>RestClient.<jsm>create</jsm>().serializer(s).parser(p);</code> * * @param s The serializer class to use for output. * @param p The parser class to use for input. * @return A new {@link RestClientBuilder} object. */ public static RestClientBuilder create(Class<? extends Serializer> s, Class<? extends Parser> p) { return create().serializer(s).parser(p); }
/** * Perform a <code>DELETE</code> request against the specified URL. * * @param url * The URL of the remote REST resource. * Can be any of the following: {@link String}, {@link URI}, {@link URL}. * @return * A {@link RestCall} object that can be further tailored before executing the request and getting the response * as a parsed object. * @throws RestCallException If any authentication errors occurred. */ public RestCall doDelete(Object url) throws RestCallException { return doCall("DELETE", url, false); }