RestClient

Abstract client that calls remote endpoints using the HTTP/REST protocol.

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)
    • traceId: 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
  • *:traces:*:*:1.0 - (optional) ITracer components to record traces
  • *:discovery:*:*:1.0 - (optional) IDiscovery services to resolve a connection

Fields

counters

List of counters.

counters: CompositeCounters

client

HTTP client.

client: http.Client?

uri

Remote service uri which is defined on openning.

uri: String?

timeout

Invocation timeout in milliseconds.

timeout: int = 1000

connectionResolver

Connection resolver.

connectionResolver: HttpConnectionResolver = HttpConnectionResolver()

logger

Logger.

logger: CompositeLogger = CompositeLogger()

tracer

The tracer.

tracer: CompositeTracer = CompositeTracer()

counters

Performance counters.

counters: CompositeCounters = CompositeCounters()

options

Configuration options.

options: ConfigParams = ConfigParams()

baseRoute

Base route.

baseRoute: String?

retries

Number of retries.

retries: int = 1

headers

Default headers to be added to every request.

headers: Map<String, String>

connectTimeout

Connection timeout in milliseconds.

connectTimeout: int = 1000

Instance methods

addTraceId

Adds a trace id (traceId) to the invocation parameter map.

Map<String, String> addTraceId(Map<String, String>? params, IContext? context)

  • params: Map<String, String>? - invocation parameters.
  • context: IContext - (optional) a context to trace execution through a call chain.
  • returns: Map<String, String> - invocation parameters with added trace id.

addFilterParams

Adds filter parameters (with the same name as they defined) to the invocation parameter map.

Map<String, String> addFilterParams(Map<String, String>? params, FilterParams? filter)

  • params: Map<String, String>? - invocation parameters.
  • filter: FilterParams? - (optional) filter parameters
  • returns: Map<String, String> - invocation parameters with added filter parameters.

addPagingParams

Adds paging parameters (skip, take, total) to invocation parameter map.

Map<String, String> addPagingParams(Map<String, String>? params, PagingParams? paging)

  • params: Map<String, String>? - invocation parameters.
  • paging: PagingParams? - (optional) paging parameters
  • returns: Map<String, String> - invocation parameters with added paging parameters.

call

Calls a remote method via HTTP/REST protocol.

Future call(String method, String route, IContext? context, Map<String, String> params, [data])

  • method: String - HTTP method: “get”, “head”, “post”, “put”, “delete”
  • route: String - command route. Base route will be added to this route
  • context: IContext - (optional) a context to trace execution through a call chain.
  • params: Map<String, String> - (optional) query parameters.
  • data: dynamic - (optional) body object.
  • returns: Future - that receives result object

close

Closes a component and frees used resources.

@override

Future close(IContext? context)

  • context: IContext - (optional) a context to trace execution through a call chain.

configure

Configures a component by passing configuration parameters.

@override

void configure(ConfigParams config)

  • config: ConfigParams - configuration parameters to be set.

instrument

Adds instrumentation to log calls and measures call time. It returns a CounterTiming object that is used to end the time measurement.

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.

@override

bool isOpen()

  • returns: bool - true if the component is open and false otherwise.

open

Opens the component.

@override

Future open(IContext? context)

  • context: IContext - (optional) a context to trace execution through a call chain.

setReferences

Sets references to dependent components.

@override

void setReferences(IReferences references)

  • references: IReferences - references used to locate the component dependencies.

Examples

class MyRestClient extends RestClient implements IMyClient {
   ...
    Future<MyData> getData(IContext? contextd, String id) async {
       var counter_timing = instrument(context, 'myclient.get_data');
      try{
        var result = await call('get', '/get_data' context, { id: id }, null);
        counter_timing.endTiming();
        return result;
      } catch (err) {
           counter_timing.endTiming();
           rethrow;
       });
   }
   ...
}

var client = MyRestClient();
client.configure(ConfigParams.fromTuples([
    'connection.protocol', 'http',
    'connection.host', 'localhost',
    'connection.port', 8080
]));

var result = await client.getData('123', '1');
  ...

See also