clients

package
v1.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 26, 2021 License: MIT Imports: 17 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertComandResult

func ConvertComandResult(comRes interface{}, prototype reflect.Type) (convRes interface{}, err error)

ConvertComandResult method helps get correct result from JSON by prototype Parameters:

  • comRes interface{} input JSON string
  • prototype reflect.Type output object prototype

Returns: convRes interface{}, err error

Types

type CommandableHttpClient

type CommandableHttpClient struct {
	*RestClient
}

CommandableHttpClient is abstract client that calls commandable HTTP service.

Commandable services are generated automatically for ICommandable objects. Each command is exposed as POST operation that receives all parameters in body object.

Configuration parameters:

base_route:              base route for remote URI
- connection(s):
  - discovery_key:         (optional) a key to retrieve the connection from connect.idiscovery.html 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 - *:discovery:*:*:1.0 (optional) IDiscovery services to resolve connection

Example:

type MyCommandableHttpClient struct{
    *CommandableHttpClient
    prototype reflect.Type // type of operation data
   ...
}
   result, err := func (c * MyCommandableHttpClient) GetData(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(cconf.NewConfigParamsFromTuples(
    "connection.protocol", "http",
    "connection.host", "localhost",
    "connection.port", 8080
));

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

func NewCommandableHttpClient

func NewCommandableHttpClient(baseRoute string) *CommandableHttpClient

NewCommandableHttpClient is creates a new instance of the client. Parameters:

  • baseRoute string a base route for remote service.

Returns: *CommandableHttpClient pointer on new instance

func (*CommandableHttpClient) CallCommand

func (c *CommandableHttpClient) CallCommand(prototype reflect.Type, name string, correlationId string, params *cdata.AnyValueMap) (result interface{}, err error)

CallCommand is calls a remote method via HTTP commadable protocol. The call is made via POST operation and all parameters are sent in body object. The complete route to remote method is defined as baseRoute + "/" + name. Parameters:

  • prototype reflect.Type type of returned data
  • name string a name of the command to call.
  • correlationId string (optional) transaction id to trace execution through call chain.
  • params cdata.StringValueMap command parameters.

Returns: result interface{}, err error result or error.

type DirectClient

type DirectClient struct {
	//The controller reference.
	Controller interface{}
	//The open flag.
	Opened bool
	//The logger.
	Logger *clog.CompositeLogger
	//The performance counters
	Counters *ccount.CompositeCounters
	//The dependency resolver to get controller reference.
	DependencyResolver crefer.DependencyResolver
	// The tracer.
	Tracer *ctrace.CompositeTracer
}

DirectClient is bstract client that calls controller directly in the same memory space.

It is used when multiple microservices are deployed in a single container (monolyth) and communication between them can be done by direct calls rather then through the network.

Configuration parameters:

- dependencies:

  • controller: override controller descriptor

References:

- *:logger:*:*:1.0 (optional) ILogger components to pass log messages - *:counters:*:*:1.0 (optional) ICounters components to pass collected measurements - *:controller:*:*:1.0 controller to call business methods

Example:

	type MyDirectClient struct {
	*DirectClient
	}
        func MyDirectClient()* MyDirectClient {
		  c:= MyDirectClient{}
		  c.DirectClient = NewDirectClient()
          c.DependencyResolver.Put("controller", cref.NewDescriptor(
              "mygroup", "controller", "*", "*", "*"));
		}

		func (c *MyDirectClient) SetReferences(references cref.IReferences) {
			c.DirectClient.SetReferences(references)
			specificController, ok := c.Controller.(tdata.IMyDataController)
			if !ok {
				panic("MyDirectClient: Cant't resolv dependency 'controller' to IMyDataController")
			}
			c.specificController = specificController
		}
        ...

        func (c * MyDirectClient) GetData(correlationId string, id string)(result MyData, err error) {
           timing := c.Instrument(correlationId, "myclient.get_data")
           cmRes, cmdErr := c.specificController.GetData(correlationId, id)
           timing.EndTiming();
           return  c.InstrumentError(correlationId, "myclient.get_data", cmdRes, cmdErr)
        }
        ...

    client = NewMyDirectClient();
    client.SetReferences(cref.NewReferencesFromTuples(
        cref.NewDescriptor("mygroup","controller","default","default","1.0"), controller,
    ));

    res, err := client.GetData("123", "1")

func NewDirectClient

func NewDirectClient() *DirectClient

NewDirectClient is creates a new instance of the client.

func (*DirectClient) Close

func (c *DirectClient) Close(correlationId string) error

Close method are closes component and frees used resources.

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

Returns: error error or nil no errors occured.

func (*DirectClient) Configure

func (c *DirectClient) Configure(config *cconf.ConfigParams)

Configure method are configures component by passing configuration parameters. Parameters:

  • config *cconf.ConfigParams configuration parameters to be set.

func (*DirectClient) Instrument

func (c *DirectClient) Instrument(correlationId string, name string) *service.InstrumentTiming

Instrument method are adds instrumentation to log calls and measure call time. It returns a Timing 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 Timing object to end the time measurement.

func (*DirectClient) IsOpen

func (c *DirectClient) IsOpen() bool

IsOpen method are checks if the component is opened. Returns true if the component has been opened and false otherwise.

func (*DirectClient) Open

func (c *DirectClient) Open(correlationId string) error

Open method are opens the component.

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

Returns: error error or nil no errors occured.

func (*DirectClient) SetReferences

func (c *DirectClient) SetReferences(references crefer.IReferences)

SetReferences method are sets references to dependent components. Parameters: - references crefer.IReferences references to locate the component dependencies.

type RestClient

type RestClient struct {

	//The HTTP client.
	Client *http.Client
	//The connection resolver.
	ConnectionResolver rpccon.HttpConnectionResolver
	//The logger.
	Logger *clog.CompositeLogger
	//The performance counters.
	Counters *ccount.CompositeCounters
	// The tracer.
	Tracer *ctrace.CompositeTracer
	//The configuration options.
	Options cconf.ConfigParams
	//The base route.
	BaseRoute string
	//The number of retries.
	Retries int
	//The default headers to be added to every request.
	Headers cdata.StringValueMap
	//The connection timeout in milliseconds.
	ConnectTimeout int
	//The invocation timeout in milliseconds.
	Timeout int
	//The remote service uri which is calculated on open.
	Uri string
	// contains filtered or unexported fields
}

RestClient is 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)
  • connectTimeout: 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:
    type MyRestClient struct {
		*RestClient
	}
    ...
    func (c *MyRestClient) GetData(correlationId string, id string) (result *tdata.MyDataPage, err error) {

		params := cdata.NewEmptyStringValueMap()
		params.Set("id", id)

		calValue, calErr := c.Call(MyDataPageType, "get", "/data", correlationId, params, nil)
		if calErr != nil {
			return nil, calErr
		}

		result, _ = calValue.(*tdata.MyDataPage)
		c.Instrument(correlationId, "myData.get_page_by_filter")
		return result, nil
	}

    client := NewMyRestClient();
    client.Configure(cconf.NewConfigParamsFromTuples(
        "connection.protocol", "http",
        "connection.host", "localhost",
        "connection.port", 8080,
    ));

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

func NewRestClient

func NewRestClient() *RestClient

NewRestClient creates new instance of RestClient Retruns pointer on NewRestClient

func (*RestClient) AddCorrelationId

func (c *RestClient) AddCorrelationId(params *cdata.StringValueMap, correlationId string) *cdata.StringValueMap

AddCorrelationId method are adds a correlation id (correlation_id) to invocation parameter map. Parameters:

  • params *cdata.StringValueMap invocation parameters.
  • correlationId string (optional) a correlation id to be added. Return invocation parameters with added correlation id.

func (*RestClient) AddFilterParams

func (c *RestClient) AddFilterParams(params *cdata.StringValueMap, filter *cdata.FilterParams) *cdata.StringValueMap

AddFilterParams method are adds filter parameters (with the same name as they defined) to invocation parameter map. Parameters:

  • params *cdata.StringValueMap invocation parameters.
  • filter *cdata.FilterParams (optional) filter parameters

Return invocation parameters with added filter parameters.

func (*RestClient) AddPagingParams

func (c *RestClient) AddPagingParams(params *cdata.StringValueMap, paging *cdata.PagingParams) *cdata.StringValueMap

AddPagingParams method are adds paging parameters (skip, take, total) to invocation parameter map. Parameters:

  • params invocation parameters.
  • paging (optional) paging parameters

Return invocation parameters with added paging parameters.

func (*RestClient) Call

func (c *RestClient) Call(prototype reflect.Type, method string, route string, correlationId string, params *cdata.StringValueMap,
	data interface{}) (result interface{}, err error)

Call method are calls a remote method via HTTP/REST protocol. Parameters:

  • prototype reflect.Type type for convert JSON result. Set nil for return raw JSON string
  • method string HTTP method: "get", "head", "post", "put", "delete"
  • route string a command route. Base route will be added to this route
  • correlationId string (optional) transaction id to trace execution through call chain.
  • params cdata.StringValueMap (optional) query parameters.
  • data interface{} (optional) body object.

Returns: result interface{}, err error result object or error.

func (*RestClient) Close

func (c *RestClient) Close(correlationId string) error

Close method are closes component and frees used resources. Parameters:

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

Retruns: error error or nil no errors occured.

func (*RestClient) Configure

func (c *RestClient) Configure(config *cconf.ConfigParams)

Configures component by passing configuration parameters. Parameters: - config *cconf.ConfigParams configuration parameters to be set.

func (*RestClient) Instrument

func (c *RestClient) Instrument(correlationId string, name string) *service.InstrumentTiming

Instrument method are adds instrumentation to log calls and measure call time. It returns a Timing 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. Return Timing object to end the time measurement.

func (*RestClient) InstrumentError

func (c *RestClient) InstrumentError(correlationId string, name string, inErr error, inRes interface{}) (result interface{}, err error)

InstrumentError method are dds instrumentation to error handling.

  • correlationId string (optional) transaction id to trace execution through call chain.
  • name string a method name.
  • err error an occured error
  • result interface{} (optional) an execution result

Returns: result interface{}, err error an execution result and error

func (*RestClient) IsOpen

func (c *RestClient) IsOpen() bool

IsOpen are checks if the component is opened. Return true if the component has been opened and false otherwise.

func (*RestClient) Open

func (c *RestClient) Open(correlationId string) error

Open method are opens the component.

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

Returns: error error or nil no errors occured.

func (*RestClient) SetReferences

func (c *RestClient) SetReferences(references crefer.IReferences)

Sets references to dependent components. Parameters: - references crefer.IReferences references to locate the component dependencies.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL