clients

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2023 License: MIT Imports: 17 Imported by: 16

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleHttpResponse

func HandleHttpResponse[T any](r *grpcproto.InvokeReply, correlationId string) (T, error)

HandleHttpResponse method helps handle http response body

Parameters:
	- r *grpcproto.InvokeReply response object from grpc server
	- correlationId string (optional) transaction id to trace execution through call chain.
Returns: T any result, err error

func ToError added in v1.0.2

func ToError(obj *protos.ErrorDescription) error

func ToMap added in v1.0.2

func ToMap(val map[string]string) map[string]interface{}

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(ctx context.Context, correlationId string, id string) (result *MyData, err error) {
   	params := cdata.NewEmptyStringValueMap()
   	params.Put("id", id)
	response, calErr := c.CallCommand(MyDataType, "get_mydata_by_id", correlationId, params)
	if calErr != nil {
	    return nil, calErr
	}
	return grpcclients.HandleHttpResponse[*MyData](response, correlationId)
}
...

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

result, err := client.GetData(ctx, "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(ctx context.Context, name string, correlationId string, params *cdata.AnyValueMap) (result *grpcproto.InvokeReply, 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:
	- ctx context.Context	operation context
	- 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 tracer.
	Tracer *ctrace.CompositeTracer
	//	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 MyGrpcClient struct{
	*GrpcClient
}

func  (c *MyGrpcClient) GetData(ctx context.Context, correlationId string, id string) (res any, 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 = NewMyGrpcClient();
client.Configure(ctx, NewConfigParamsFromTuples(
    "connection.protocol", "http",
    "connection.host", "localhost",
    "connection.port", 8080,
));

result, err := client.GetData(ctx, "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.

Parameters:
	- 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.

Parameters:
	- ctx context.Context	operation context
	- 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 any, response any) 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 any request query parameters.
	- response any
	- response body object.

Returns error

func (*GrpcClient) CallWithContext

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

CallWithContext method are calls a remote method via gRPC protocol.

Parameters:
	- ctx context.Context	operation context
	- method string   gRPC method name
	- correlationId string transaction id to trace execution through call chain.
	- request any request query parameters.
	- response any
	- response body object.

Returns error

func (*GrpcClient) Close

func (c *GrpcClient) Close(ctx context.Context, correlationId string) error

Close method are closes component and frees used resources.

	Parameters:
		- ctx context.Context	operation context
		- correlationId string
  transaction id to trace execution through call chain.

Returns error

func (*GrpcClient) Configure

func (c *GrpcClient) Configure(ctx context.Context, config *cconf.ConfigParams)

Configure method are configures component by passing configuration parameters.

	Parameters:
		- ctx context.Context	operation context
		- config *config.ConfigParams
  configuration parameters to be set.

func (*GrpcClient) Instrument

func (c *GrpcClient) Instrument(ctx context.Context, correlationId string, name string) *services.InstrumentTiming

Instrument method are adds instrumentation to log calls and measure call time. It returns a services.InstrumentTiming object that is used to end the time measurement.

Parameters:
	- ctx context.Context	operation context
	- correlationId string (optional) transaction id to trace execution through call chain.
	- name string a method name.
Returns: services.InstrumentTiming object to end the time measurement.

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(ctx context.Context, correlationId string) error

Open method are opens the component.

Parameters:
	- ctx context.Context	operation context
	- correlationId string

transaction id to trace execution through call chain. Returns error error or nil

func (*GrpcClient) SetReferences

func (c *GrpcClient) SetReferences(ctx context.Context, references cref.IReferences)

SetReferences method are sets references to dependent components.

Parameters:
	- ctx context.Context	operation context
	- 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