Options
All
  • Public
  • Public/Protected
  • All
Menu

Class RestClient

Abstract client that calls remove endpoints using HTTP/REST protocol.

Configuration parameters

  • base_route: base route for remote URI
  • connection(s):
    • discovery_key: (optional) a 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)
    • correlation_id_place place for adding correalationId, query - in query string, headers - in headers, both - in query and headers (default: query)

References

  • *:logger:*:*:1.0 (optional) ILogger components to pass log messages
  • *:counters:*:*:1.0 (optional) ICounters components to pass collected measurements
  • *:discovery:*:*:1.0 (optional) IDiscovery services to resolve connection
see

RestService

see

CommandableHttpService

Example

class MyRestClient extends RestClient implements IMyClient {
   ...

   public getData(correlationId: string, id: string,
       callback: (err: any, result: MyData) => void): void {

       let timing = this.instrument(correlationId, 'myclient.get_data');
       this.call("get", "/get_data" correlationId, { id: id }, null, (err, result) => {
           timing.endTiming();
           callback(err, result);
       });
   }
   ...
}

let client = new MyRestClient();
client.configure(ConfigParams.fromTuples(
    "connection.protocol", "http",
    "connection.host", "localhost",
    "connection.port", 8080
));

client.getData("123", "1", (err, result) => {
  ...
});

Hierarchy

Implements

  • any
  • any
  • any

Index

Properties

Protected _baseRoute

_baseRoute: string

The base route.

Protected _client

_client: any

The HTTP client.

Protected _connectTimeout

_connectTimeout: number = 10000

The connection timeout in milliseconds.

Protected _connectionResolver

_connectionResolver: HttpConnectionResolver = new HttpConnectionResolver()

The connection resolver.

Protected _correlationIdPlace

_correlationIdPlace: string = "query"

Protected _counters

_counters: CompositeCounters = new CompositeCounters()

The performance counters.

Protected _headers

_headers: any

The default headers to be added to every request.

Protected _logger

_logger: CompositeLogger = new CompositeLogger()

The logger.

Protected _options

_options: ConfigParams = new ConfigParams()

The configuration options.

Protected _retries

_retries: number = 1

The number of retries.

Protected _timeout

_timeout: number = 10000

The invocation timeout in milliseconds.

Protected _uri

_uri: string

The remote service uri which is calculated on open.

Methods

Protected addCorrelationId

  • addCorrelationId(params: any, correlationId: string): any
  • Adds a correlation id (correlation_id) to invocation parameter map.

    Parameters

    • params: any

      invocation parameters.

    • correlationId: string

      (optional) a correlation id to be added.

    Returns any

    invocation parameters with added correlation id.

Protected addFilterParams

  • addFilterParams(params: any, filter: any): void
  • Adds filter parameters (with the same name as they defined) to invocation parameter map.

    Parameters

    • params: any

      invocation parameters.

    • filter: any

      (optional) filter parameters

    Returns void

    invocation parameters with added filter parameters.

Protected addPagingParams

  • addPagingParams(params: any, paging: any): void
  • Adds paging parameters (skip, take, total) to invocation parameter map.

    Parameters

    • params: any

      invocation parameters.

    • paging: any

      (optional) paging parameters

    Returns void

    invocation parameters with added paging parameters.

Protected call

  • call(method: string, route: string, correlationId?: string, params?: any, data?: any, callback?: function): void
  • Calls a remote method via HTTP/REST protocol.

    Parameters

    • method: string

      HTTP method: "get", "head", "post", "put", "delete"

    • route: string

      a command route. Base route will be added to this route

    • Optional correlationId: string

      (optional) transaction id to trace execution through call chain.

    • Default value params: any = {}

      (optional) query parameters.

    • Optional data: any

      (optional) body object.

    • Optional callback: function

      (optional) callback function that receives result object or error.

        • (err: any, result: any): void
        • Parameters

          • err: any
          • result: any

          Returns void

    Returns void

close

  • close(correlationId: string, callback?: function): void
  • Closes component and frees used resources.

    Parameters

    • correlationId: string

      (optional) transaction id to trace execution through call chain.

    • Optional callback: function

      callback function that receives error or null no errors occured.

        • (err: any): void
        • Parameters

          • err: any

          Returns void

    Returns void

configure

  • configure(config: ConfigParams): void
  • Configures component by passing configuration parameters.

    Parameters

    • config: ConfigParams

      configuration parameters to be set.

    Returns void

Protected instrument

  • instrument(correlationId: string, name: string): CounterTiming
  • Adds instrumentation to log calls and measure call time. It returns a CounterTiming object that is used to end the time measurement.

    Parameters

    • correlationId: string

      (optional) transaction id to trace execution through call chain.

    • name: string

      a method name.

    Returns CounterTiming

    CounterTiming object to end the time measurement.

Protected instrumentError

  • instrumentError(correlationId: string, name: string, err: any, result?: any, callback?: function): void
  • Adds instrumentation to error handling.

    Parameters

    • correlationId: string

      (optional) transaction id to trace execution through call chain.

    • name: string

      a method name.

    • err: any

      an occured error

    • Default value result: any = null

      (optional) an execution result

    • Default value callback: function = null

      (optional) an execution callback

        • (err: any, result: any): void
        • Parameters

          • err: any
          • result: any

          Returns void

    Returns void

isOpen

  • isOpen(): boolean
  • Checks if the component is opened.

    Returns boolean

    true if the component has been opened and false otherwise.

open

  • open(correlationId: string, callback?: function): void
  • Opens the component.

    Parameters

    • correlationId: string

      (optional) transaction id to trace execution through call chain.

    • Optional callback: function

      callback function that receives error or null no errors occured.

        • (err: any): void
        • Parameters

          • err: any

          Returns void

    Returns void

setReferences

  • setReferences(references: IReferences): void
  • Sets references to dependent components.

    Parameters

    • references: IReferences

      references to locate the component dependencies.

    Returns void

Generated using TypeDoc