Example #1
0
    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())
Example #2
0
    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())
 def test_json_data(self):
     resp = StateResponse(data=b'{"status": "ok"}')
     self.assertEqual({'status': 'ok'}, resp.json())
 def test_data(self):
     resp = StateResponse(data=b'hello dapr')
     self.assertEqual('hello dapr', resp.text())
     self.assertEqual(b'hello dapr', resp.data)