@@ -72,7 +72,7 @@ class Producer:
7272 The Pulsar message producer, used to publish messages on a topic.
7373 """
7474
75- def __init__ (self , producer : _pulsar .Producer ) -> None :
75+ def __init__ (self , producer : _pulsar .Producer , schema : pulsar . schema . Schema ) -> None :
7676 """
7777 Create the producer.
7878 Users should not call this constructor directly. Instead, create the
@@ -82,8 +82,11 @@ def __init__(self, producer: _pulsar.Producer) -> None:
8282 ----------
8383 producer: _pulsar.Producer
8484 The underlying Producer object from the C extension.
85+ schema: pulsar.schema.Schema
86+ The schema of the data that will be sent by this producer.
8587 """
86- self ._producer : _pulsar .Producer = producer
88+ self ._producer = producer
89+ self ._schema = schema
8790
8891 async def send (self , content : Any ) -> pulsar .MessageId :
8992 """
@@ -105,7 +108,7 @@ async def send(self, content: Any) -> pulsar.MessageId:
105108 PulsarException
106109 """
107110 builder = _pulsar .MessageBuilder ()
108- builder .content (content )
111+ builder .content (self . _schema . encode ( content ) )
109112 future = asyncio .get_running_loop ().create_future ()
110113 self ._producer .send_async (builder .build (), functools .partial (_set_future , future ))
111114 msg_id = await future
@@ -454,7 +457,8 @@ def underlying_router(msg, num_partitions):
454457 self ._client .create_producer_async (
455458 topic , conf , functools .partial (_set_future , future )
456459 )
457- return Producer (await future )
460+ schema .attach_client (self ._client )
461+ return Producer (await future , schema )
458462
459463 # pylint: disable=too-many-arguments,too-many-locals,too-many-branches,too-many-positional-arguments
460464 async def subscribe (self , topic : Union [str , List [str ]],
@@ -632,11 +636,9 @@ async def subscribe(self, topic: Union[str, List[str]],
632636 functools .partial (_set_future , future )
633637 )
634638 else :
635- raise ValueError (
636- "Argument 'topic' is expected to be of a type between "
637- "(str, list)"
638- )
639+ raise ValueError ( "Argument 'topic' is expected to be of type 'str' or 'list'" )
639640
641+ schema .attach_client (self ._client )
640642 return Consumer (await future , schema )
641643
642644 async def close (self ) -> None :
0 commit comments