pip_services3_grpc.services package

Submodules

Module contents

class pip_services3_grpc.services.CommandableGrpcService(name: str)

Bases: pip_services3_grpc.services.GrpcService.GrpcService, abc.ABC

Abstract service that receives commands via GRPC protocol to operations automatically generated for commands defined in ICommandable. Each command is exposed as invoke method that receives command name and parameters.

Commandable services require only 3 lines of code to implement a robust external GRPC-based remote interface.

### Configuration parameters ###

  • dependencies:
    • endpoint: override for HTTP Endpoint dependency

    • controller: override for Controller dependency

  • 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

    ### 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 - *:endpoint:grpc:*:1.0 (optional) GrpcEndpoint <pip_services3_grpc.services.GrpcEndpoint.GrpcEndpoint reference

See CommandableGrpcClient <pip_services3_grpc.clients.CommandableGrpcClient.CommandableGrpcClient, GrpcService <pip_services3_grpc.services.GrpcService.GrpcService

class MyCommandableGrpcService(CommandableGrpcService):
   def __init__(self):
      super().__init__('service name')

      self._dependency_resolver.put(
            "controller",
            Descriptor("mygroup","controller","*","*","1.0")
      )

service = MyCommandableGrpcService()
service.configure(ConfigParams.from_tuples(
    "connection.protocol", "http",
    "connection.host", "localhost",
    "connection.port", 8080
))
service.set_references(References.from_tuples(
    Descriptor("mygroup","controller","default","default","1.0"), controller
))
service.open("123")
register()

Registers all service routes in gRPC endpoint. Call automaticaly in open component procedure

class pip_services3_grpc.services.GrpcEndpoint

Bases: pip_services3_commons.run.IOpenable.IOpenable, pip_services3_commons.config.IConfigurable.IConfigurable, pip_services3_commons.refer.IReferenceable.IReferenceable

Used for creating GRPC endpoints. An endpoint is a URL, at which a given service can be accessed by a client.

### Configuration parameters ###

Parameters to pass to the configure() method for component configuration:

  • connection(s) - the connection resolver’s connections:
    • “connection.discovery_key” - the key to use for connection resolving in a discovery service;

    • “connection.protocol” - the connection’s protocol;

    • “connection.host” - the target host;

    • “connection.port” - the target port;

    • “connection.uri” - the target URI.

  • credential - the HTTPS credentials:
    • “credential.ssl_key_file” - the SSL private key in PEM

    • “credential.ssl_crt_file” - the SSL certificate in PEM

    • “credential.ssl_ca_file” - the certificate authorities (root cerfiticates) in PEM

### References ###

A logger, counters, and a connection resolver can be referenced by passing the following references to the object’s set_references() method:

  • logger: *:logger:*:*:1.0”;

  • counters: “*:counters:*:*:1.0”;

  • discovery: “*:discovery:*:*:1.0” (for the connection resolver).

def my_method(self, _config, _references):
    endpoint = GrpcEndpoint()
    if self._config:
        endpoint.configure(self._config)
    if self._references:
        endpoint.set_references(self._references)
    ...

    self._endpoint.open(correlation_id)
    ...
close(correlation_id: Optional[str])

Closes this endpoint and the GRPC server (service) that was opened earlier.

Parameters

correlation_id – (optional) transaction id to trace execution through call chain.

configure(config: pip_services3_commons.config.ConfigParams.ConfigParams)

Configures this HttpEndpoint using the given configuration parameters.

### Configuration parameters ###
  • connection(s) - the connection resolver’s connections;

  • “connection.discovery_key” - the key to use for connection resolving in a dis

  • “connection.protocol” - the connection’s protocol;

  • “connection.host” - the target host;

  • “connection.port” - the target port;

  • “connection.uri” - the target URI.

  • “credential.ssl_key_file” - SSL private key in PEM

  • “credential.ssl_crt_file” - SSL certificate in PEM

  • “credential.ssl_ca_file” - Certificate authority (root certificate) in PEM

Parameters

config – configuration parameters, containing a “connection(s)” section.

is_open()bool
Returns

whether or not this endpoint is open with an actively listening GRPC server.

open(correlation_id: Optional[str])

Opens a connection using the parameters resolved by the referenced connection resolver and creates a GRPC server (service) using the set options and parameters.

Parameters

correlation_id – (optional) transaction id to trace execution through call chain.

register(registration: pip_services3_grpc.services.IRegisterable.IRegisterable)

Registers a registerable object for dynamic endpoint discovery.

Parameters

registration – the registration to add.

register_service(service: Any)

Registers a service with related implementation

Parameters

service – a GRPC service object.

set_references(references: pip_services3_commons.refer.IReferences.IReferences)

Sets references to this endpoint’s logger, counters, and connection resolver.

__References:__ - logger: “*:logger:*:*:1.0” - counters: “*:counters:*:*:1.0” - discovery: “*:discovery:*:*:1.0” (for the connection resolver)

Parameters

references – an IReferences object, containing references to a logger, counters, and a connection resolver.

unregister(registration: pip_services3_grpc.services.IRegisterable.IRegisterable)

Unregisters a registerable object, so that it is no longer used in dynamic endpoint discovery.

Parameters

registration – the registration to remove.

class pip_services3_grpc.services.GrpcService(service_name: str = None)

Bases: pip_services3_commons.run.IOpenable.IOpenable, pip_services3_commons.config.IConfigurable.IConfigurable, pip_services3_grpc.services.IRegisterable.IRegisterable, pip_services3_commons.refer.IUnreferenceable.IUnreferenceable

Abstract service that receives remove calls via GRPC protocol.

### Configuration parameters ###
  • dependencies: - endpoint: override for GRPC Endpoint dependency - controller: override for Controller dependency

  • 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

  • credential - the HTTPS credentials: - ssl_key_file: the SSL private key in PEM - ssl_crt_file: the SSL certificate in PEM - ssl_ca_file: the certificate authorities (root cerfiticates) in PEM

class MyGrpcService(GrpcService, my_data_pb2_grpc.MyDataServicer):
    __controller: IMyController
    ...
    def __init__(self):
        suoer().__init__('.. service name ...')
        self._dependency_resolver.put(
            "controller",
            Descriptor("mygroup","controller","*","*","1.0")
        )

    def add_servicer_to_server(self, server):
        my_data_pb2_grpc.add_MyDataServicer_to_server(self, server)

    def set_references(self, references):
        super().set_references(references)
        self._controller = this._dependency_resolver.get_required("controller")

    def __number_of_calls_interceptor(self, request: InvokeRequest, context: ServicerContext,
                            next: Callable[[InvokeRequest, ServicerContext], Any]) -> Any:
        self.__number_of_calls += 1
        return next(request, context)

    def __method(request: InvokeRequest, context: ServicerContext):
        correlationId = request.correlationId
        id = request.id
        return self._controller.get_my_data(correlationId, id)

    def register(self):

        self._register_interceptor(self.__number_of_calls_interceptor)
        self._register_method("get_mydata", None, method)

        self._register_service(self)
        ...



service = MyGrpcService()
service.configure(ConfigParams.from_tuples(
    "connection.protocol", "http",
    "connection.host", "localhost",
    "connection.port", 8080
))

service.set_references(References.from_tuples(
   Descriptor("mygroup","controller","default","default","1.0"), controller
))

service.open("123")
close(correlation_id: Optional[str])

Closes component and frees used resources.

Parameters

correlation_id – (optional) transaction id to trace execution through call chain.

configure(config: pip_services3_commons.config.ConfigParams.ConfigParams)

Configures component by passing configuration parameters. :param config: configuration parameters to be set.

is_open()bool

Checks if the component is opened.

Returns

true if the component has been opened and false otherwise.

open(correlation_id: Optional[str])

Opens the component.

Parameters

correlation_id – (optional) transaction id to trace execution through call chain.

abstract register()

Registers all service routes in Grpc endpoint. This method is called by the service and must be overriden in child classes.

set_references(references: pip_services3_commons.refer.IReferences.IReferences)

Sets references to this endpoint’s logger, counters, and connection resolver.

### References ###
  • logger: “*:logger:*:*:1.0”

  • counters: “*:counters:*:*:1.0”

  • discovery: “*:discovery:*:*:1.0” (for the connection resolver)

Parameters

references – an IReferences object, containing references to a logger, counters, and a connection resolver.

unset_references()

Unsets (clears) previously set references to dependent components.

class pip_services3_grpc.services.IRegisterable

Bases: abc.ABC

Interface to perform on-demand registrations.

register()

Perform required registration steps.