Implements: IOpenable, IReferenceable, IConfigurable
Description
The GrpcClient class allows you to create clients that call remote endpoints using the GRPC protocol.
Configuration parameters
- 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)
Constructors
Creates a new instance of the grpc client.
GrpcClient(service_client: Any, client_name: str)
- client_name: str - client’s name.
- service_client: Any - service client class
Fields
Instance methods
_add_filter_params
AddFilterParams method adds filter parameters (with the same name as they defined) to the invocation parameter map.
_add_filter_params(params: Any, filter: Any): Any
- params: Any - invocation parameters.
- filter: Any - (optional) filter parameters
- returns: Any - invocation parameters with added filter parameters.
_add_paging_params
AddPagingParams method adds paging parameters (skip, take, total) to the invocation parameter map.
_add_filter_params(params: Any, paging: Any): Any
- params: Any - invocation parameters.
- paging: Any - (optional) paging parameters
- returns: Any - invocation parameters with added paging parameters.
_call
Calls a remote method via GRPC protocol.
_call(method: str, context: Optional[IContext], request: Any): Any
- method: str - name of the calling method
- context: IContext - (optional) a context to trace execution through a call chain.
- request: Any - (optional) request object.
- returns: Any - (optional) feature that receives the result object or error.
close
Closes the component and frees used resources.
close(context: Optional[IContext])
- context: IContext - (optional) a context to trace execution through a call chain.
configure
Configures the component by passing its configuration parameters.
configure(config: ConfigParams)
- 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.
_instrument(context: Optional[IContext], name: str): InstrumentTiming
- context: IContext - (optional) a context to trace execution through a call chain.
- name: str - method name.
- returns: InstrumentTiming - InstrumentTiming object used to end the time measurement.
is_open
Checks if the component is open.
is_open(): bool
- returns: bool - Returns True if the component is open and False otherwise.
open
Opens the component.
open(context: Optional[IContext])
- context: IContext - (optional) a context to trace execution through a call chain.
set_references
Sets references to dependent components.
set_references(references: IReferences)
- references: IReferences - references to locate the component dependencies.
Examples
class MyGrpcClient(GrpcClient, IMyClient):
def __init__(self):
super().__init__(my_data_pb2_grpc.MyDataStub, 'my_data_v1')
...
def get_data(self, context, id ):
timing = self.instrument(context, 'myclient.get_data')
result = self._call("get_data", context, { id: id })
timing.end_timing()
return result
...
client = MyGrpcClient()
client.configure(ConfigParams.from_tuples(
"connection.protocol", "http",
"connection.host", "localhost",
"connection.port", 8080
))
result = client.get_data("123", "1")