Implements: IConfigurable, IReferenceable, IOpenable
Description
The RestClient class allows you to create clients that call remote endpoints using the HTTP/REST protocol.
Configuration parameters
- base_route: base route for a remote URI
- connection(s):
- discovery_key: (optional) key to retrieve the connection from IDiscovery
- protocol: connection protocol (http or https)
- host: host name or IP address
- port: port number
- uri: resource URI or connection string with all parameters in it
- options:
- retries: number of retries (default: 3)
- connect_timeout: connection timeout in milliseconds (default: 10 sec)
- timeout: invocation timeout in milliseconds (default: 10 sec)
- context: a context to trace execution through a call chain.
References
- *:logger:*:*:1.0 - (optional) ILogger components to pass log messages
- *:counters:*:*:1.0 - (optional) ICounters components to pass collected measurements
- *:traces:*:*:1.0 - (optional) ITracer components to record traces
- *:discovery:*:*:1.0 - (optional) IDiscovery services to resolve a connection
Fields
Instance methods
addTraceId
Adds a trace id (trace_id) to invocation parameter map.
protected
String addTraceId(String route, IContext context)
- route: String - route.
- context: IContext - (optional) a context to trace execution through a call chain.
- returns: String - trace id.
addFilterParams
Adds filter parameters (with the same name as they defined) to the invocation parameter map.
protected
String addFilterParams(String route, FilterParams filter)
- params: route - route.
- filter: FilterParams - (optional) filter parameters
- returns: any - invocation parameters with added filter parameters.
addPagingParams
Adds paging parameters (skip, take, total) to invocation parameter map.
protected
String addPagingParams(String route, PagingParams paging)
- route: String - invocation parameters.
- paging: PagingParams - (optional) paging parameters
- returns: String - invocation parameters with added paging parameters.
call
Calls a remote method via HTTP/REST protocol.
protected
T call(Class type, IContext context, String method, String route, Object requestEntity) throws ApplicationException
- type: Class
- the class type of data. - context: IContext - (optional) a context to trace execution through a call chain.
- method: String - HTTP method: “get”, “head”, “post”, “put”, “delete”
- route: String - a command route. Base route will be added to this route
- requestEntity: Object - request body object.
- returns:
T - result object
close
Closes a component and frees used resources.
public
void close(IContext context)
- context: IContext - (optional) a context to trace execution through a call chain.
configure
Configures a component by passing configuration parameters.
public
void configure(ConfigParams config)
- config: ConfigParams - configuration parameters to be set.
instrument
Adds instrumentation to log calls and measures call time. It returns a Timing object that is used to end the time measurement.
protected
InstrumentTiming instrument(IContext context, String name)
- context: IContext - (optional) a context to trace execution through a call chain.
- name: string - method name.
- returns: InstrumentTiming - InstrumentTiming object used to end the time measurement.
isOpen
Checks if the component is open.
public
boolean isOpen()
- returns: boolean - True if the component is open and False otherwise.
open
Opens the component.
public
void open(IContext context) throws ApplicationException
- context: IContext - (optional) a context to trace execution through a call chain.
setReferences
Sets references to dependent components.
public
void setReferences(IReferences references) throws ReferenceException
- references: IReferences - references used to locate the component dependencies.
Examples
{@code
class MyRestClient extends RestClient implements IMyClient {
...
public MyData getData(IContext context, String id) {
Timing timing = this.instrument(context, 'myclient.get_data');
MyData result = this.execute(MyData.class, context, 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");
...
}