Example #1
0
    def invoke_binding(
        self,
        binding_name: str,
        operation: str,
        data: Union[bytes, str],
        binding_metadata: Dict[str, str] = {},
        metadata: Optional[MetadataTuple] = ()
    ) -> BindingResponse:
        """Invokes the output binding with the specified operation.

        The data field takes any JSON serializable value and acts as the
        payload to be sent to the output binding. The metadata field is an
        array of key/value pairs and allows you to set binding specific metadata
        for each call. The operation field tells the Dapr binding which operation
        it should perform.

        The example calls output `binding` service with bytes data:

            from dapr.clients import DaprClient

            with DaprClient() as d:
                resp = d.invoke_binding(
                    binding_name = 'kafkaBinding',
                    operation = 'create',
                    data = b'message',
                )
                # resp.data includes the response data in bytes.

        Args:
            binding_name (str): the name of the binding as defined in the components
            operation (str): the operation to perform on the binding
            data (bytes or str): bytes or str for data which will sent to the binding
            binding_metadata (dict, optional): Dapr metadata for output binding
            metadata (tuple, optional, DEPRECATED): gRPC custom metadata

        Returns:
            :class:`InvokeBindingResponse` object returned from binding
        """
        if metadata is not None:
            warn(
                'metadata argument is deprecated. Dapr already intercepts API token headers '
                'and this is not needed.',
                DeprecationWarning,
                stacklevel=2)

        req_data = BindingRequest(data, binding_metadata)

        req = api_v1.InvokeBindingRequest(name=binding_name,
                                          data=req_data.data,
                                          metadata=req_data.binding_metadata,
                                          operation=operation)

        response, call = self._stub.InvokeBinding.with_call(req,
                                                            metadata=metadata)
        return BindingResponse(response.data, dict(response.metadata),
                               call.initial_metadata())
Example #2
0
    def invoke_binding(
            self,
            name: str,
            operation: str,
            data: Union[bytes, str],
            binding_metadata: Dict[str, str] = {},
            metadata: Optional[MetadataTuple] = ()) -> BindingResponse:
        """Invokes the output binding with the specified operation.

        The data field takes any JSON serializable value and acts as the
        payload to be sent to the output binding. The metadata field is an
        array of key/value pairs and allows you to set binding specific metadata
        for each call. The operation field tells the Dapr binding which operation
        it should perform.

        The example calls output `binding` service with bytes data:

            from dapr.clients import DaprClient

            with DaprClient() as d:
                resp = d.invoke_binding(
                    name = 'kafkaBinding',
                    operation = 'create',
                    data = b'message',
                    metadata = (
                        ('header1', 'value1)
                    ),
                )
                # resp.data includes the response data in bytes.
                # resp.metadata include the metadata returned from the external system.

        Args:
            name (str): the name of the binding as defined in the components
            operation (str): the operation to perform on the binding
            data (bytes or str): bytes or str for data which will sent to the binding
            binding_metadata (dict, optional): metadata for output binding
            metadata (tuple, optional): custom metadata to send to the binding

        Returns:
            :class:`InvokeBindingResponse` object returned from binding
        """
        req_data = BindingRequest(data, binding_metadata)

        req = api_v1.InvokeBindingRequest(
            name=name,
            data=req_data.data,
            metadata=req_data.binding_metadata,
            operation=operation
        )

        response, call = self._stub.InvokeBinding.with_call(req, metadata=metadata)
        return BindingResponse(
            response.data, dict(response.metadata),
            call.initial_metadata())
 def test_metadata(self):
     resp = BindingResponse(data=b'data', binding_metadata={'status': 'ok'})
     self.assertEqual({'status': 'ok'}, resp.binding_metadata)
     self.assertEqual(b'data', resp.data)
     self.assertEqual('data', resp.text())
 def test_bytes_message(self):
     resp = BindingResponse(data=b'data', binding_metadata={})
     self.assertEqual({}, resp.binding_metadata)
     self.assertEqual(b'data', resp.data)
     self.assertEqual('data', resp.text())
 def test_json_data(self):
     resp = BindingResponse(data=b'{"status": "ok"}', binding_metadata={})
     self.assertEqual({'status': 'ok'}, resp.json())