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