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
addTraceId(params: any, context: IContext): any
- params: any - invocation parameters.
- context: IContext - (optional) a context to trace execution through a call chain.
- returns: any - invocation parameters with added correlation id.
addFilterParams
Adds filter parameters (with the same name as they defined) to the invocation parameter map.
protected
addFilterParams(params: any, filters: any): any
- params: any - invocation parameters.
- filters: any - (optional) filter parameters
- returns: any - invocation parameters with added filter parameters.
addPagingParams
Adds paging parameters (skip, take, total) to invocation parameter map.
protected
addPagingParams(params: any, paging: any): any
- params: any - invocation parameters.
- paging: any - (optional) paging parameters
- returns: any - invocation parameters with added paging parameters.
call
Calls a remote method via HTTP/REST protocol.
protected
call<T>(method: string, route: string, context?: string, params: any = {}, data?: any): Promise<T>
- method: string - HTTP method: “get”, “head”, “post”, “put”, “delete”
- route: string - a command route. Base route will be added to this route
- context: IContext - (optional) a context to trace execution through a call chain.
- params: any - (optional) query parameters.
- data: any - (optional) body object.
- returns: Promise<T> - result object
close
Closes a component and frees used resources.
public
close(context: IContext): Promise<void>
- context: IContext - (optional) a context to trace execution through a call chain.
configure
Configures a component by passing configuration parameters.
public
configure(config: ConfigParams): void
- 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
instrument(context: IContext, name: string): InstrumentTiming
- 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
isOpen(): boolean
- returns: boolean - True if the component is open and False otherwise.
open
Opens the component.
public
open(context: IContext): Promise<void>
- context: IContext - (optional) a context to trace execution through a call chain.
setReferences
Sets references to dependent components.
public
setReferences(references: IReferences): void
- references: IReferences - references used to locate the component dependencies.
Examples
class MyRestClient extends RestClient implements IMyClient {
...
public async getData(context: IContext, id: string): Promise<MyData> {
let timing = this.instrument(context, 'myclient.get_data');
try {
return await this.call("get", "/get_data" context, { id: id }, null);
} catch (ex) {
timing.endFailure(ex);
} finally {
timing.endTiming();
}
}
...
}
let client = new MyRestClient();
client.configure(ConfigParams.fromTuples(
"connection.protocol", "http",
"connection.host", "localhost",
"connection.port", 8080
));
let result = await client.getData("123", "1");