CommandableHttpClient

Commandable services are generated automatically for ICommandable

Implements: RestClient

Description

The CommandableHttpClient class allows you to create commandable services. Commandable services are generated automatically for ICommandable objects.

Important points

  • Each command is exposed as a POST operation that receives all parameters in the body object.

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)

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

Constructors

Creates a new instance of the client.

NewCommandableHttpClient(baseRoute string) *CommandableHttpClient

  • baseRoute: string - base route for a remote service.

Methods

CallCommand

Calls a remote method via the HTTP commadable protocol. The call is made via a POST operation and all parameters are sent in the body object. The complete route to the remote method is defined as baseRoute + “/” + name.

(c *CommandableHttpClient) CallCommand(ctx context.Context, prototype reflect.Type, name string, correlationId string, params *cdata.AnyValueMap) (*http.Response, error)

  • ctx: context.Context - operation context.
  • name: string - name of the command to call.
  • correlationId: string - (optional) transaction id used to trace execution through a call chain.
  • params: *cdata.AnyValueMap - command parameters.
  • returns: (*http.Response, err) - response or error.

Examples

type MyCommandableHttpClient struct{
	*CommandableHttpClient
	...
}
func (c * MyCommandableHttpClient) GetData(ctx context.Context, correlationId string, id string)(result MyData, err error){
	params:= cdata.NewEmptyStringValueMap()
	params.Set("id",id)
	res, err := c.CallCommand(
		prototype
		"get_data",
		correlationId,
		params,
	)
	...
	// convert response to MyData
	...
	return result, err
}


client = NewMyCommandableHttpClient();
client.Configure(context.Background(), cconf.NewConfigParamsFromTuples(
	"connection.protocol", "http",
	"connection.host", "localhost",
	"connection.port", 8080
));

result, err := client.GetData(context.Background(), "123", "1")
...