def publish_event( self, pubsub_name: str, topic_name: str, data: Union[bytes, str], metadata: Optional[MetadataTuple] = (), data_content_type: Optional[str] = None) -> DaprResponse: """Publish to a given topic. This publishes an event with bytes array or str data to a specified topic and specified pubsub component. The str data is encoded into bytes with default charset of utf-8. Custom metadata can be passed with the metadata field which will be passed on a gRPC metadata. The example publishes a byte array event to a topic: from dapr.clients import DaprClient with DaprClient() as d: resp = d.publish_event( pubsub_name='pubsub_1' topic_name='TOPIC_A' data=b'message', metadata=( ('header1', 'value1') ), ) # resp.headers includes the gRPC initial metadata. Args: pubsub_name (str): the name of the pubsub component topic_name (str): the topic name to publish to data (bytes or str): bytes or str for data metadata (tuple, optional): custom metadata data_content_type: (str, optional): content type of the data payload Returns: :class:`DaprResponse` gRPC metadata returned from callee """ if not isinstance(data, bytes) and not isinstance(data, str): raise ValueError(f'invalid type for data {type(data)}') req_data = data if isinstance(data, str): req_data = data.encode('utf-8') req = api_v1.PublishEventRequest( pubsub_name=pubsub_name, topic=topic_name, data=req_data, data_content_type=data_content_type) # response is google.protobuf.Empty _, call = self._stub.PublishEvent.with_call(req, metadata=metadata) return DaprResponse(call.initial_metadata())
def publish_event( self, topic: str, data: Union[bytes, str], metadata: Optional[MetadataTuple] = () ) -> DaprResponse: """Publish to a given topic. This publishes an event with bytes array or str data to a specified topic. The str data is encoded into bytes with default charset of utf-8. Custom metadata can be passed with the metadata field which will be passed on a gRPC metadata. The example publishes a byte array event to a topic: from dapr import DaprClient with DaprClient() as d: resp = d.publish_event( topic='TOPIC_A' data=b'message', metadata=( ('header1', 'value1') ), ) # resp.headers includes the gRPC initial metadata. # resp.trailers includes that gRPC trailing metadata. Args: topic (str): the topic name to publish to data (bytes or str): bytes or str for data metadata (tuple, optional): custom metadata Returns: :class:`DaprResponse` gRPC metadata returned from callee """ if not isinstance(data, bytes) and not isinstance(data, str): raise ValueError(f'invalid type for data {type(data)}') req_data = data if isinstance(data, str): req_data = data.encode('utf-8') req = api_v1.PublishEventRequest(topic=topic, data=req_data) # response is google.protobuf.Empty response, call = self._stub.PublishEvent.with_call(req, metadata=metadata) return DaprResponse(headers=call.initial_metadata(), trailers=call.trailing_metadata())
def publish_event(self, pubsub_name: str, topic_name: str, data: Union[bytes, str], publish_metadata: Dict[str, str] = {}, metadata: Optional[MetadataTuple] = (), data_content_type: Optional[str] = None) -> DaprResponse: """Publish to a given topic. This publishes an event with bytes array or str data to a specified topic and specified pubsub component. The str data is encoded into bytes with default charset of utf-8. Custom metadata can be passed with the metadata field which will be passed on a gRPC metadata. The example publishes a byte array event to a topic: from dapr.clients import DaprClient with DaprClient() as d: resp = d.publish_event( pubsub_name='pubsub_1', topic_name='TOPIC_A', data=b'message', publish_metadata={'ttlInSeconds': '100', 'rawPayload': 'false'}, ) # resp.headers includes the gRPC initial metadata. Args: pubsub_name (str): the name of the pubsub component topic_name (str): the topic name to publish to data (bytes or str): bytes or str for data publish_metadata (Dict[str, str], optional): Dapr metadata per Pub/Sub message metadata (tuple, optional, DEPRECATED): gRPC custom metadata data_content_type: (str, optional): content type of the data payload Returns: :class:`DaprResponse` gRPC metadata returned from callee """ 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 isinstance(data, bytes) and not isinstance(data, str): raise ValueError(f'invalid type for data {type(data)}') req_data: bytes if isinstance(data, bytes): req_data = data else: if isinstance(data, str): req_data = data.encode('utf-8') content_type = "" if data_content_type: content_type = data_content_type req = api_v1.PublishEventRequest(pubsub_name=pubsub_name, topic=topic_name, data=req_data, data_content_type=content_type, metadata=publish_metadata) # response is google.protobuf.Empty _, call = self._stub.PublishEvent.with_call(req, metadata=metadata) return DaprResponse(call.initial_metadata())
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) resp = client.DeleteState(api_v1.DeleteStateRequest(store_name=storeName, key=key))