clients

package
v1.2.6 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2023 License: MIT Imports: 16 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommandableGrpcClient

type CommandableGrpcClient struct {
	*GrpcClient
	//The service name
	Name string
}

CommandableGrpcClient abstract client that calls commandable GRPC service.

Commandable services are generated automatically for ICommandable objects. Each command is exposed as Invoke method that receives all parameters as args.

Configuration parameters:

  • 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)

    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 MyCommandableGrpcClient struct {
	 *CommandableGrpcClient
       ...
	}
        func (c * MyCommandableGrpcClient) GetData(correlationId string, id string) (result *MyData, err error) {
           params := cdata.NewEmptyStringValueMap()
           params.Put("id", id)

            calValue, calErr := c.CallCommand(MyDataType, "get_mydata_by_id", correlationId, params)
            if calErr != nil {
                return nil, calErr
            }
            result, _ = calValue.(*tdata.MyData)
            return result, err
        }
        ...

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

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

func NewCommandableGrpcClient

func NewCommandableGrpcClient(name string) *CommandableGrpcClient

NewCommandableGrpcClient method are creates a new instance of the client. Parameters:

  • name a service name.

func (*CommandableGrpcClient) CallCommand

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

CallCommand method are calls a remote method via GRPC commadable protocol. The call is made via Invoke method and all parameters are sent in args object. The complete route to remote method is defined as serviceName + "." + name. Parameters:

  • prototype a prototype for properly convert result
  • name a name of the command to call.
  • correlationId (optional) transaction id to trace execution through call chain.
  • params command parameters.

Retruns: result or error.

type GrpcClient

type GrpcClient struct {

	//	The GRPC client.
	Client grpcproto.CommandableClient
	// The GRPC connection
	Connection *grpc.ClientConn
	//	The connection resolver.
	ConnectionResolver *rpccon.HttpConnectionResolver
	//	The logger.
	Logger *clog.CompositeLogger
	//	The performance counters.
	Counters *ccount.CompositeCounters
	//	The configuration options.
	Options *cconf.ConfigParams
	//	The connection timeout in milliseconds.
	ConnectTimeout time.Duration
	//	The invocation timeout in milliseconds.
	Timeout time.Duration
	//	The remote service uri which is calculated on open.
	Uri string
	// contains filtered or unexported fields
}

GrpcClient 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 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
}
    func  (c *MyCommandableHttpClient) GetData(correlationId string, id string) (res interface{}, err error) {

        req := &testproto.MyDataIdRequest{
            CorrelationId: correlationId,
            mydataId:       id,
        }

        reply := new(testproto.MyData)
        err = c.Call("get_mydata_by_id", correlationId, req, reply)
        c.Instrument(correlationId, "mydata.get_one_by_id")
        if err != nil {
            return nil, err
        }
        result = toMyData(reply)
        if result != nil && result.Id == "" && result.Key == "" {
            result = nil
        }
        return result, nil
	}

var client = NewMyCommandableHttpClient(); client.Configure(NewConfigParamsFromTuples(

"connection.protocol", "http",
"connection.host", "localhost",
"connection.port", 8080,

));

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

func NewGrpcClient

func NewGrpcClient(name string) *GrpcClient

NewGrpcClient method are creates a new instance of the client. Parameters:

  • baseRoute string a base route for remote service.

Returns *GrpcClient

func (*GrpcClient) AddFilterParams

func (c *GrpcClient) 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.

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

Return invocation parameters with added filter parameters.

func (*GrpcClient) AddInterceptors

func (c *GrpcClient) AddInterceptors(interceptors ...grpc.DialOption)

AddInterceptors method are registers a middleware for methods in gRPC client. See https://github.com/grpc/grpc-go/tree/master/examples/features/interceptor Parameters:

  • interceptors ...grpc.DialOption

interceptor functions (Stream or Unary use grpc.WithUnaryInterceptor() or grpc.WithStreamInterceptor() for inflate in grpc.ServerOption)

func (*GrpcClient) AddPagingParams

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

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

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

Return invocation parameters with added paging parameters.

func (*GrpcClient) Call

func (c *GrpcClient) Call(method string, correlationId string, request interface{}, response interface{}) error

Call method are calls a remote method via gRPC protocol. Parameters:

  • method string gRPC method name
  • correlationId string transaction id to trace execution through call chain.
  • request interface{} request query parameters.
  • response interface{}
  • response body object.

Returns error

func (*GrpcClient) CallWithContext added in v1.1.1

func (c *GrpcClient) CallWithContext(ctx context.Context, correlationId string, method string, request interface{}, response interface{}) error

CallWithContext method are calls a remote method via gRPC protocol. Parameters:

  • ctx context
  • correlationId string transaction id to trace execution through call chain.
  • method string// gRPC method name
  • request interface{} request query parameters.
  • response interface{}
  • response body object.

Returns error

func (*GrpcClient) Close

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

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

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

Returns error

func (*GrpcClient) Configure

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

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

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

func (*GrpcClient) Instrument

func (c *GrpcClient) Instrument(correlationId string, name string) *ccount.CounterTiming

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.

  • correlationId (optional) transaction id to trace execution through call chain.
  • name a method name.

Returns: Timing object to end the time measurement.

func (*GrpcClient) InstrumentError

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

InstrumentError mrthod are adds instrumentation to error handling.

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

Retruns: result interface{}, err error input result and error.

func (*GrpcClient) IsOpen

func (c *GrpcClient) IsOpen() bool

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

func (*GrpcClient) Open

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

Open method are opens the component. Parameters:

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

Returns error error or nil

func (*GrpcClient) SetReferences

func (c *GrpcClient) SetReferences(references cref.IReferences)

SetReferences method are sets references to dependent components.

  • references cref.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