|
31 | 31 | from google.protobuf.struct_pb2 import Struct as GrpcStruct |
32 | 32 | from grpc import ( # type: ignore |
33 | 33 | RpcError, |
| 34 | + StatusCode, |
34 | 35 | StreamStreamClientInterceptor, |
35 | 36 | StreamUnaryClientInterceptor, |
36 | 37 | UnaryStreamClientInterceptor, |
|
60 | 61 | ) |
61 | 62 | from dapr.clients.grpc._response import ( |
62 | 63 | BindingResponse, |
| 64 | + BulkPublishResponse, |
| 65 | + BulkPublishResponseFailedEntry, |
63 | 66 | BulkStateItem, |
64 | 67 | BulkStatesResponse, |
65 | 68 | ConfigurationResponse, |
@@ -487,6 +490,96 @@ def publish_event( |
487 | 490 |
|
488 | 491 | return DaprResponse(call.initial_metadata()) |
489 | 492 |
|
| 493 | + def publish_events( |
| 494 | + self, |
| 495 | + pubsub_name: str, |
| 496 | + topic_name: str, |
| 497 | + data: Sequence[Union[bytes, str]], |
| 498 | + publish_metadata: Dict[str, str] = {}, |
| 499 | + data_content_type: Optional[str] = None, |
| 500 | + ) -> BulkPublishResponse: |
| 501 | + """Bulk publish multiple events to a given topic. |
| 502 | + This publishes multiple events to a specified topic and pubsub component. |
| 503 | + Each event can be bytes or str. The str data is encoded into bytes with |
| 504 | + default charset of utf-8. |
| 505 | +
|
| 506 | + The example publishes multiple string events to a topic: |
| 507 | +
|
| 508 | + from dapr.clients import DaprClient |
| 509 | + with DaprClient() as d: |
| 510 | + resp = d.publish_events( |
| 511 | + pubsub_name='pubsub_1', |
| 512 | + topic_name='TOPIC_A', |
| 513 | + data=['message1', 'message2', 'message3'], |
| 514 | + data_content_type='text/plain', |
| 515 | + ) |
| 516 | + # resp.failed_entries includes any entries that failed to publish. |
| 517 | +
|
| 518 | + Args: |
| 519 | + pubsub_name (str): the name of the pubsub component |
| 520 | + topic_name (str): the topic name to publish to |
| 521 | + data (Sequence[Union[bytes, str]]): sequence of events to publish; |
| 522 | + each event must be bytes or str |
| 523 | + publish_metadata (Dict[str, str], optional): Dapr metadata for the |
| 524 | + bulk publish request |
| 525 | + data_content_type (str, optional): content type of the event data |
| 526 | +
|
| 527 | + Returns: |
| 528 | + :class:`BulkPublishResponse` with any failed entries |
| 529 | + """ |
| 530 | + entries = [] |
| 531 | + for event in data: |
| 532 | + entry_id = str(uuid.uuid4()) |
| 533 | + if isinstance(event, bytes): |
| 534 | + event_data = event |
| 535 | + content_type = data_content_type or 'application/octet-stream' |
| 536 | + elif isinstance(event, str): |
| 537 | + event_data = event.encode('utf-8') |
| 538 | + content_type = data_content_type or 'text/plain' |
| 539 | + else: |
| 540 | + raise ValueError(f'invalid type for event {type(event)}') |
| 541 | + |
| 542 | + entries.append( |
| 543 | + api_v1.BulkPublishRequestEntry( |
| 544 | + entry_id=entry_id, |
| 545 | + event=event_data, |
| 546 | + content_type=content_type, |
| 547 | + ) |
| 548 | + ) |
| 549 | + |
| 550 | + req = api_v1.BulkPublishRequest( |
| 551 | + pubsub_name=pubsub_name, |
| 552 | + topic=topic_name, |
| 553 | + entries=entries, |
| 554 | + metadata=publish_metadata, |
| 555 | + ) |
| 556 | + |
| 557 | + try: |
| 558 | + response, call = self.retry_policy.run_rpc(self._stub.BulkPublishEvent.with_call, req) |
| 559 | + except RpcError as err: |
| 560 | + if err.code() == StatusCode.UNIMPLEMENTED: |
| 561 | + try: |
| 562 | + response, call = self.retry_policy.run_rpc( |
| 563 | + self._stub.BulkPublishEventAlpha1.with_call, req |
| 564 | + ) |
| 565 | + except RpcError as err2: |
| 566 | + raise DaprGrpcError(err2) from err2 |
| 567 | + else: |
| 568 | + raise DaprGrpcError(err) from err |
| 569 | + |
| 570 | + failed_entries = [ |
| 571 | + BulkPublishResponseFailedEntry( |
| 572 | + entry_id=entry.entry_id, |
| 573 | + error=entry.error, |
| 574 | + ) |
| 575 | + for entry in response.failedEntries |
| 576 | + ] |
| 577 | + |
| 578 | + return BulkPublishResponse( |
| 579 | + failed_entries=failed_entries, |
| 580 | + headers=call.initial_metadata(), |
| 581 | + ) |
| 582 | + |
490 | 583 | def subscribe( |
491 | 584 | self, |
492 | 585 | pubsub_name: str, |
|
0 commit comments