コード例 #1
0
ファイル: client.py プロジェクト: maneeshs/python-sdk
    def get_state(
        self,
        store_name: str,
        key: str,
        metadata: Optional[MetadataTuple] = ()) -> DaprResponse:
        """Gets value from a statestore with a key

        The example gets value from a statestore:
            from dapr import DaprClient
            with DaprClient() as d:
                resp = d.get_state(
                    store_name='state_store'
                    key='key_1',
                    metadata=(
                        ('header1', 'value1')
                    ),
                )

        Args:
            store_name (str): the state store name to get from
            key (str): the key of the key-value pair to be gotten
            metadata (tuple, optional): custom metadata

        Returns:
            :class:`StateResponse` gRPC metadata returned from callee
            and value obtained from the state store
        """

        if len(store_name) == 0 or len(store_name.strip()) == 0:
            raise ValueError("State store name cannot be empty")
        req = api_v1.GetStateRequest(store_name=store_name, key=key)
        response, call = self._stub.GetState.with_call(req, metadata=metadata)
        return StateResponse(data=response.data,
                             headers=call.initial_metadata())
コード例 #2
0
 def GetBulkState(self, request, context):
     items = []
     for key in request.keys:
         req = api_v1.GetStateRequest(store_name=request.store_name, key=key)
         res = self.GetState(req, context)
         data = res.data
         etag = res.etag
         if request.metadata["upper"]:
             data = to_bytes(data.decode("utf-8").upper())
         items.append(api_v1.BulkStateItem(key=key, etag=etag, data=data))
     return api_v1.GetBulkStateResponse(items=items)
コード例 #3
0
ファイル: client.py プロジェクト: berndverst/python-sdk
    def get_state(
        self,
        store_name: str,
        key: str,
        state_metadata: Optional[Dict[str, str]] = dict(),
        metadata: Optional[MetadataTuple] = ()
    ) -> StateResponse:
        """Gets value from a statestore with a key

        The example gets value from a statestore:
            from dapr import DaprClient
            with DaprClient() as d:
                resp = d.get_state(
                    store_name='state_store'
                    key='key_1',
                    state={"key": "value"},
                    state_metadata={"metakey": "metavalue"},
                )

        Args:
            store_name (str): the state store name to get from
            key (str): the key of the key-value pair to be gotten
            state_metadata (Dict[str, str], optional): Dapr metadata for state request
            metadata (tuple, optional, DEPRECATED): gRPC custom metadata

        Returns:
            :class:`StateResponse` gRPC metadata returned from callee
            and value obtained from the state store
        """
        if metadata is not None:
            warn(
                'metadata argument is deprecated. Dapr already intercepts API token headers '
                'and this is not needed.',
                DeprecationWarning,
                stacklevel=2)

        if not store_name or len(store_name) == 0 or len(
                store_name.strip()) == 0:
            raise ValueError("State store name cannot be empty")
        req = api_v1.GetStateRequest(store_name=store_name,
                                     key=key,
                                     metadata=state_metadata)
        response, call = self._stub.GetState.with_call(req, metadata=metadata)
        return StateResponse(data=response.data,
                             etag=response.etag,
                             headers=call.initial_metadata())
コード例 #4
0
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)

resp = client.DeleteState(api_v1.DeleteStateRequest(store_name=storeName, key=key))
print('Deleted!')

channel.close()