Esempio n. 1
0
    def OnBindingEvent(self, request, context):
        """Listens events from the input bindings
        User application can save the states or send the events to the output
        bindings optionally by returning BindingEventResponse.
        """
        if request.name not in self._binding_map:
            context.set_code(grpc.StatusCode.UNIMPLEMENTED)  # type: ignore
            raise NotImplementedError(
                f'{request.name} binding not implemented!')

        req = BindingRequest(request.data, request.metadata)
        req.metadata = context.invocation_metadata()
        self._binding_map[request.name](req)

        # TODO: support output bindings options
        return appcallback_v1.BindingEventResponse()
    def test_non_empty_metadata(self):
        # act
        data = BindingRequest(data='hello dapr', binding_metadata={'ttlInSeconds': '1000'})

        # arrange
        self.assertEqual(b'hello dapr', data.data)
        self.assertEqual({'ttlInSeconds': '1000'}, data.binding_metadata)
    def test_str_data(self):
        # act
        data = BindingRequest(data='hello dapr')

        # arrange
        self.assertEqual(b'hello dapr', data.data)
        self.assertEqual({}, data.metadata)
Esempio n. 4
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())
Esempio n. 5
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())