Beispiel #1
0
    def __init__(self,
                 address: Optional[str] = None,
                 tracer: Optional[Tracer] = None):
        """Connects to Dapr Runtime and initialize gRPC client stub.

        Args:
            address (str, optional): Dapr Runtime gRPC endpoint address.
        """
        if not address:
            address = f"{settings.DAPR_RUNTIME_HOST}:{settings.DAPR_GRPC_PORT}"
        self._address = address
        self._channel = grpc.insecure_channel(address)  # type: ignore

        if settings.DAPR_API_TOKEN:
            api_token_interceptor = DaprClientInterceptor([
                ('dapr-api-token', settings.DAPR_API_TOKEN),
            ])
            self._channel = grpc.intercept_channel(  # type: ignore
                self._channel, api_token_interceptor)
        if tracer:
            self._channel = grpc.intercept_channel(  # type: ignore
                self._channel,
                client_interceptor.OpenCensusClientInterceptor(tracer=tracer))

        self._stub = api_service_v1.DaprStub(self._channel)
Beispiel #2
0
    def __init__(self,
                 address: Optional[str] = None,
                 interceptors: Optional[List[Union[
                     UnaryUnaryClientInterceptor, UnaryStreamClientInterceptor,
                     StreamUnaryClientInterceptor,
                     StreamStreamClientInterceptor]]] = None):
        """Connects to Dapr Runtime and initialize gRPC client stub.

        Args:
            address (str, optional): Dapr Runtime gRPC endpoint address.
            interceptors (list of UnaryUnaryClientInterceptor or
                UnaryStreamClientInterceptor or
                StreamUnaryClientInterceptor or
                StreamStreamClientInterceptor, optional): gRPC interceptors.
        """
        if not address:
            address = f"{settings.DAPR_RUNTIME_HOST}:{settings.DAPR_GRPC_PORT}"
        self._address = address
        self._channel = grpc.insecure_channel(address)  # type: ignore

        if settings.DAPR_API_TOKEN:
            api_token_interceptor = DaprClientInterceptor([
                ('dapr-api-token', settings.DAPR_API_TOKEN),
            ])
            self._channel = grpc.intercept_channel(  # type: ignore
                self._channel, api_token_interceptor)
        if interceptors:
            self._channel = grpc.intercept_channel(  # type: ignore
                self._channel, *interceptors)

        self._stub = api_service_v1.DaprStub(self._channel)
Beispiel #3
0
    def __init__(
        self,
        address: Optional[str] = None,
        interceptors: Optional[List[Union[
            UnaryUnaryClientInterceptor,
            UnaryStreamClientInterceptor,
            StreamUnaryClientInterceptor,
            StreamStreamClientInterceptor]]] = None,
        max_grpc_message_length: Optional[int] = None
    ):
        """Connects to Dapr Runtime and initialize gRPC client stub.

        Args:
            address (str, optional): Dapr Runtime gRPC endpoint address.
            interceptors (list of UnaryUnaryClientInterceptor or
                UnaryStreamClientInterceptor or
                StreamUnaryClientInterceptor or
                StreamStreamClientInterceptor, optional): gRPC interceptors.
            max_grpc_messsage_length (int, optional): The maximum grpc send and receive
                message length in bytes.
        """
        if not address:
            address = f"{settings.DAPR_RUNTIME_HOST}:{settings.DAPR_GRPC_PORT}"
        self._address = address
        if not max_grpc_message_length:
            self._channel = grpc.insecure_channel(address)   # type: ignore
        else:
            self._channel = grpc.insecure_channel(address, options=[   # type: ignore
                ('grpc.max_send_message_length', max_grpc_message_length),
                ('grpc.max_receive_message_length', max_grpc_message_length),
            ])

        if settings.DAPR_API_TOKEN:
            api_token_interceptor = DaprClientInterceptor([
                ('dapr-api-token', settings.DAPR_API_TOKEN), ])
            self._channel = grpc.intercept_channel(   # type: ignore
                self._channel, api_token_interceptor)
        if interceptors:
            self._channel = grpc.intercept_channel(   # type: ignore
                self._channel, *interceptors)

        self._stub = api_service_v1.DaprStub(self._channel)
Beispiel #4
0
"""
dapr run --protocol grpc --grpc-port=50001 python example.py
"""

import grpc
import os

from dapr.proto import api_v1, api_service_v1, common_v1
from google.protobuf.any_pb2 import Any

# Get port from environment variable.
port = os.getenv('DAPR_GRPC_PORT', '50001')
daprUri = 'localhost:' + port
channel = grpc.insecure_channel(daprUri)

client = api_service_v1.DaprStub(channel)
client.PublishEvent(api_v1.PublishEventRequest(topic='sith', data='lala'.encode('utf-8')))
print('Published!')

key = 'mykey'
storeName = 'statestore'
req = common_v1.StateItem(key=key, value='my state'.encode('utf-8'))
state = api_v1.SaveStateRequest(store_name=storeName, states=[req])

client.SaveState(state)
print('Saved!')

resp = client.GetState(api_v1.GetStateRequest(store_name=storeName, key=key))
print('Got!')
print(resp)