UNPKG

8.32 kBTypeScriptView Raw
1import { IOpenable } from 'pip-services3-commons-node';
2import { IConfigurable } from 'pip-services3-commons-node';
3import { IReferenceable } from 'pip-services3-commons-node';
4import { IReferences } from 'pip-services3-commons-node';
5import { ConfigParams } from 'pip-services3-commons-node';
6import { CompositeLogger } from 'pip-services3-components-node';
7import { CompositeCounters } from 'pip-services3-components-node';
8import { CounterTiming } from 'pip-services3-components-node';
9import { HttpConnectionResolver } from '../connect/HttpConnectionResolver';
10/**
11 * Abstract client that calls remove endpoints using HTTP/REST protocol.
12 *
13 * ### Configuration parameters ###
14 *
15 * - base_route: base route for remote URI
16 * - connection(s):
17 * - discovery_key: (optional) a key to retrieve the connection from [[https://pip-services3-node.github.io/pip-services3-components-node/interfaces/connect.idiscovery.html IDiscovery]]
18 * - protocol: connection protocol: http or https
19 * - host: host name or IP address
20 * - port: port number
21 * - uri: resource URI or connection string with all parameters in it
22 * - options:
23 * - retries: number of retries (default: 3)
24 * - connect_timeout: connection timeout in milliseconds (default: 10 sec)
25 * - timeout: invocation timeout in milliseconds (default: 10 sec)
26 * - correlation_id_place place for adding correalationId, query - in query string, headers - in headers, both - in query and headers (default: query)
27 *
28 * ### References ###
29 *
30 * - <code>\*:logger:\*:\*:1.0</code> (optional) [[https://pip-services3-node.github.io/pip-services3-components-node/interfaces/log.ilogger.html ILogger]] components to pass log messages
31 * - <code>\*:counters:\*:\*:1.0</code> (optional) [[https://pip-services3-node.github.io/pip-services3-components-node/interfaces/count.icounters.html ICounters]] components to pass collected measurements
32 * - <code>\*:discovery:\*:\*:1.0</code> (optional) [[https://pip-services3-node.github.io/pip-services3-components-node/interfaces/connect.idiscovery.html IDiscovery]] services to resolve connection
33 *
34 * @see [[RestService]]
35 * @see [[CommandableHttpService]]
36 *
37 * ### Example ###
38 *
39 * class MyRestClient extends RestClient implements IMyClient {
40 * ...
41 *
42 * public getData(correlationId: string, id: string,
43 * callback: (err: any, result: MyData) => void): void {
44 *
45 * let timing = this.instrument(correlationId, 'myclient.get_data');
46 * this.call("get", "/get_data" correlationId, { id: id }, null, (err, result) => {
47 * timing.endTiming();
48 * callback(err, result);
49 * });
50 * }
51 * ...
52 * }
53 *
54 * let client = new MyRestClient();
55 * client.configure(ConfigParams.fromTuples(
56 * "connection.protocol", "http",
57 * "connection.host", "localhost",
58 * "connection.port", 8080
59 * ));
60 *
61 * client.getData("123", "1", (err, result) => {
62 * ...
63 * });
64 */
65export declare abstract class RestClient implements IOpenable, IConfigurable, IReferenceable {
66 private static readonly _defaultConfig;
67 /**
68 * The HTTP client.
69 */
70 protected _client: any;
71 /**
72 * The connection resolver.
73 */
74 protected _connectionResolver: HttpConnectionResolver;
75 /**
76 * The logger.
77 */
78 protected _logger: CompositeLogger;
79 /**
80 * The performance counters.
81 */
82 protected _counters: CompositeCounters;
83 /**
84 * The configuration options.
85 */
86 protected _options: ConfigParams;
87 /**
88 * The base route.
89 */
90 protected _baseRoute: string;
91 /**
92 * The number of retries.
93 */
94 protected _retries: number;
95 /**
96 * The default headers to be added to every request.
97 */
98 protected _headers: any;
99 /**
100 * The connection timeout in milliseconds.
101 */
102 protected _connectTimeout: number;
103 /**
104 * The invocation timeout in milliseconds.
105 */
106 protected _timeout: number;
107 /**
108 * The remote service uri which is calculated on open.
109 */
110 protected _uri: string;
111 protected _correlationIdPlace: string;
112 /**
113 * Configures component by passing configuration parameters.
114 *
115 * @param config configuration parameters to be set.
116 */
117 configure(config: ConfigParams): void;
118 /**
119 * Sets references to dependent components.
120 *
121 * @param references references to locate the component dependencies.
122 */
123 setReferences(references: IReferences): void;
124 /**
125 * Adds instrumentation to log calls and measure call time.
126 * It returns a CounterTiming object that is used to end the time measurement.
127 *
128 * @param correlationId (optional) transaction id to trace execution through call chain.
129 * @param name a method name.
130 * @returns CounterTiming object to end the time measurement.
131 */
132 protected instrument(correlationId: string, name: string): CounterTiming;
133 /**
134 * Adds instrumentation to error handling.
135 *
136 * @param correlationId (optional) transaction id to trace execution through call chain.
137 * @param name a method name.
138 * @param err an occured error
139 * @param result (optional) an execution result
140 * @param callback (optional) an execution callback
141 */
142 protected instrumentError(correlationId: string, name: string, err: any, result?: any, callback?: (err: any, result: any) => void): void;
143 /**
144 * Checks if the component is opened.
145 *
146 * @returns true if the component has been opened and false otherwise.
147 */
148 isOpen(): boolean;
149 /**
150 * Opens the component.
151 *
152 * @param correlationId (optional) transaction id to trace execution through call chain.
153 * @param callback callback function that receives error or null no errors occured.
154 */
155 open(correlationId: string, callback?: (err: any) => void): void;
156 /**
157 * Closes component and frees used resources.
158 *
159 * @param correlationId (optional) transaction id to trace execution through call chain.
160 * @param callback callback function that receives error or null no errors occured.
161 */
162 close(correlationId: string, callback?: (err: any) => void): void;
163 /**
164 * Adds a correlation id (correlation_id) to invocation parameter map.
165 *
166 * @param params invocation parameters.
167 * @param correlationId (optional) a correlation id to be added.
168 * @returns invocation parameters with added correlation id.
169 */
170 protected addCorrelationId(params: any, correlationId: string): any;
171 /**
172 * Adds filter parameters (with the same name as they defined)
173 * to invocation parameter map.
174 *
175 * @param params invocation parameters.
176 * @param filter (optional) filter parameters
177 * @returns invocation parameters with added filter parameters.
178 */
179 protected addFilterParams(params: any, filter: any): void;
180 /**
181 * Adds paging parameters (skip, take, total) to invocation parameter map.
182 *
183 * @param params invocation parameters.
184 * @param paging (optional) paging parameters
185 * @returns invocation parameters with added paging parameters.
186 */
187 protected addPagingParams(params: any, paging: any): void;
188 private createRequestRoute;
189 /**
190 * Calls a remote method via HTTP/REST protocol.
191 *
192 * @param method HTTP method: "get", "head", "post", "put", "delete"
193 * @param route a command route. Base route will be added to this route
194 * @param correlationId (optional) transaction id to trace execution through call chain.
195 * @param params (optional) query parameters.
196 * @param data (optional) body object.
197 * @param callback (optional) callback function that receives result object or error.
198 */
199 protected call(method: string, route: string, correlationId?: string, params?: any, data?: any, callback?: (err: any, result: any) => void): void;
200}