public class RestClient extends Object implements org.pipservices.commons.run.IOpenable, org.pipservices.commons.config.IConfigurable, org.pipservices.commons.refer.IReferenceable
### Configuration parameters ###
### References ###
### Example ###
class MyRestClient extends RestClient implements IMyClient {
...
public MyData getData(String correlationId, String id) {
Timing timing = this.instrument(correlationId, 'myclient.get_data');
MyData result = this.execute(MyData.class, correlationId, HttpMethod.POST, "/get_data", new MyData(id));
timing.endTiming();
return result;
}
...
}
MyRestClient client = new MyRestClient();
client.configure(ConfigParams.fromTuples(
"connection.protocol", "http",
"connection.host", "localhost",
"connection.port", 8080
));
MyData data = client.getData("123", "1");
...
| Modifier and Type | Field and Description |
|---|---|
protected String |
_baseRoute
The base route.
|
protected javax.ws.rs.client.Client |
_client
The HTTP client.
|
protected HttpConnectionResolver |
_connectionResolver
The connection resolver.
|
protected org.pipservices.components.count.CompositeCounters |
_counters
The performance counters.
|
protected org.pipservices.components.log.CompositeLogger |
_logger
The logger.
|
protected org.pipservices.commons.config.ConfigParams |
_options
The configuration options.
|
protected int |
_retries
The number of retries.
|
protected String |
_url
The remote service uri which is calculated on open.
|
| Modifier | Constructor and Description |
|---|---|
protected |
RestClient()
Creates a new instance of the client.
|
protected |
RestClient(String baseRoute)
Creates a new instance of the client.
|
| Modifier and Type | Method and Description |
|---|---|
protected String |
addCorrelationId(String route,
String correlationId)
Adds a correlation id (correlation_id) to invocation parameter map.
|
protected String |
addFilterParams(String route,
org.pipservices.commons.data.FilterParams filter)
Adds filter parameters (with the same name as they defined) to invocation
parameter map.
|
protected String |
addPagingParams(String route,
org.pipservices.commons.data.PagingParams paging)
Adds paging parameters (skip, take, total) to invocation parameter map.
|
void |
close(String correlationId)
Closes component and frees used resources.
|
void |
configure(org.pipservices.commons.config.ConfigParams config)
Configures component by passing configuration parameters.
|
protected <T> T |
execute(Class<T> type,
String correlationId,
String method,
String route,
Object requestEntity)
Executes a remote method via HTTP/REST protocol.
|
protected <T> T |
execute(javax.ws.rs.core.GenericType<T> type,
String correlationId,
String method,
String route,
Object requestEntity)
Executes a remote method via HTTP/REST protocol.
|
protected javax.ws.rs.core.Response |
executeRequest(String correlationId,
String method,
URI uri,
String mediaType,
javax.ws.rs.client.Entity<?> body) |
protected org.pipservices.components.count.Timing |
instrument(String correlationId,
String name)
Adds instrumentation to log calls and measure call time.
|
boolean |
isOpen()
Checks if the component is opened.
|
void |
open(String correlationId)
Opens the component.
|
void |
setReferences(org.pipservices.commons.refer.IReferences references)
Sets references to dependent components.
|
protected HttpConnectionResolver _connectionResolver
protected org.pipservices.components.log.CompositeLogger _logger
protected org.pipservices.components.count.CompositeCounters _counters
protected org.pipservices.commons.config.ConfigParams _options
protected String _baseRoute
protected int _retries
protected String _url
protected javax.ws.rs.client.Client _client
protected RestClient()
protected RestClient(String baseRoute)
baseRoute - a base route for remote service.public void configure(org.pipservices.commons.config.ConfigParams config)
throws org.pipservices.commons.errors.ConfigException
configure in interface org.pipservices.commons.config.IConfigurableconfig - configuration parameters to be set.org.pipservices.commons.errors.ConfigException - when configuration is wrong.public void setReferences(org.pipservices.commons.refer.IReferences references)
throws org.pipservices.commons.refer.ReferenceException
setReferences in interface org.pipservices.commons.refer.IReferenceablereferences - references to locate the component dependencies.org.pipservices.commons.refer.ReferenceException - when no found references.protected org.pipservices.components.count.Timing instrument(String correlationId, String name)
correlationId - (optional) transaction id to trace execution through
call chain.name - a method name.public boolean isOpen()
isOpen in interface org.pipservices.commons.run.IOpenablepublic void open(String correlationId) throws org.pipservices.commons.errors.ApplicationException
open in interface org.pipservices.commons.run.IOpenablecorrelationId - (optional) transaction id to trace execution through
call chain.org.pipservices.commons.errors.ApplicationException - when error occured.public void close(String correlationId) throws org.pipservices.commons.errors.ApplicationException
close in interface org.pipservices.commons.run.IClosablecorrelationId - (optional) transaction id to trace execution through
call chain.org.pipservices.commons.errors.ApplicationException - when error occured.protected String addCorrelationId(String route, String correlationId)
route - invocation parameters.correlationId - (optional) a correlation id to be added.protected String addFilterParams(String route, org.pipservices.commons.data.FilterParams filter)
route - invocation parameters.filter - (optional) filter parametersprotected String addPagingParams(String route, org.pipservices.commons.data.PagingParams paging)
route - invocation parameters.paging - (optional) paging parametersprotected javax.ws.rs.core.Response executeRequest(String correlationId, String method, URI uri, String mediaType, javax.ws.rs.client.Entity<?> body) throws org.pipservices.commons.errors.ApplicationException
org.pipservices.commons.errors.ApplicationExceptionprotected <T> T execute(Class<T> type, String correlationId, String method, String route, Object requestEntity) throws org.pipservices.commons.errors.ApplicationException
type - the class type of data.correlationId - (optional) transaction id to trace execution through
call chain.method - HTTP method: "get", "head", "post", "put", "delete"route - a command route. Base route will be added to this routerequestEntity - request body object.org.pipservices.commons.errors.ApplicationException - when error occured.protected <T> T execute(javax.ws.rs.core.GenericType<T> type,
String correlationId,
String method,
String route,
Object requestEntity)
throws org.pipservices.commons.errors.ApplicationException
type - the generic class type of data.correlationId - (optional) transaction id to trace execution through
call chain.method - HTTP method: "get", "head", "post", "put", "delete"route - a command route. Base route will be added to this routerequestEntity - request body object.org.pipservices.commons.errors.ApplicationException - when error occured.Copyright © 2018. All rights reserved.