@@ -44,15 +44,21 @@ <h1 class="title">Module <code>nitric.api.events</code></h1>
4444# See the License for the specific language governing permissions and
4545# limitations under the License.
4646#
47+ from __future__ import annotations
48+
4749from typing import List, Union
50+
51+ from grpclib import GRPCError
52+
53+ from nitric.api.exception import exception_from_grpc_error
4854from nitric.utils import new_default_channel, _struct_from_dict
49- from nitric.proto. nitric.event.v1 import EventStub , NitricEvent, TopicStub
55+ from nitricapi. nitric.event.v1 import EventServiceStub , NitricEvent, TopicServiceStub
5056from dataclasses import dataclass, field
5157
5258
5359@dataclass(frozen=True, order=True)
5460class Event(object):
55- """Represents a NitricEvent ."""
61+ """Eventing client, providing access to Topic and Event references and operations on those entities ."""
5662
5763 payload: dict = field(default_factory=dict)
5864 id: str = field(default=None)
@@ -71,7 +77,7 @@ <h1 class="title">Module <code>nitric.api.events</code></h1>
7177class Topic(object):
7278 """A reference to a topic on an event service, used to perform operations on that topic."""
7379
74- _stub: EventStub
80+ _events: Events
7581 name: str
7682
7783 async def publish(
@@ -91,11 +97,14 @@ <h1 class="title">Module <code>nitric.api.events</code></h1>
9197 # TODO: handle events that are just a payload
9298 event = Event(**event)
9399
94- response = await self._stub.publish(topic=self.name, event=_event_to_wire(event))
95- return Event(**{**event.__dict__.copy(), **{"id": response.id}})
100+ try:
101+ response = await self._events._stub.publish(topic=self.name, event=_event_to_wire(event))
102+ return Event(**{**event.__dict__.copy(), **{"id": response.id}})
103+ except GRPCError as grpc_err:
104+ raise exception_from_grpc_error(grpc_err)
96105
97106
98- class EventClient (object):
107+ class Events (object):
99108 """
100109 Nitric generic publish/subscribe event client.
101110
@@ -104,18 +113,26 @@ <h1 class="title">Module <code>nitric.api.events</code></h1>
104113
105114 def __init__(self):
106115 """Construct a Nitric Event Client."""
107- channel = new_default_channel()
108- self._stub = EventStub(channel=channel)
109- self._topic_stub = TopicStub(channel=channel)
116+ self.channel = new_default_channel()
117+ self._stub = EventServiceStub(channel=self.channel)
118+ self._topic_stub = TopicServiceStub(channel=self.channel)
119+
120+ def __del__(self):
121+ # close the channel when this client is destroyed
122+ if self.channel is not None:
123+ self.channel.close()
110124
111125 async def topics(self) -> List[Topic]:
112126 """Get a list of topics available for publishing or subscription."""
113- response = await self._topic_stub.list()
114- return [self.topic(topic.name) for topic in response.topics]
127+ try:
128+ response = await self._topic_stub.list()
129+ return [self.topic(topic.name) for topic in response.topics]
130+ except GRPCError as grpc_err:
131+ raise exception_from_grpc_error(grpc_err)
115132
116133 def topic(self, name: str) -> Topic:
117- """Return a reference a topic from the connected event service ."""
118- return Topic(_stub =self._stub , name=name)</ code > </ pre >
134+ """Return a reference to a topic."""
135+ return Topic(_events =self, name=name)</ code > </ pre >
119136</ details >
120137</ section >
121138< section >
@@ -132,13 +149,13 @@ <h2 class="section-title" id="header-classes">Classes</h2>
132149< span > (</ span > < span > payload: dict = <factory>, id: str = None, payload_type: str = None)</ span >
133150</ code > </ dt >
134151< dd >
135- < div class ="desc "> < p > Represents a NitricEvent .</ p > </ div >
152+ < div class ="desc "> < p > Eventing client, providing access to Topic and Event references and operations on those entities .</ p > </ div >
136153< details class ="source ">
137154< summary >
138155< span > Expand source code</ span >
139156</ summary >
140157< pre > < code class ="python "> class Event(object):
141- """Represents a NitricEvent ."""
158+ """Eventing client, providing access to Topic and Event references and operations on those entities ."""
142159
143160 payload: dict = field(default_factory=dict)
144161 id: str = field(default=None)
@@ -160,8 +177,8 @@ <h3>Class variables</h3>
160177</ dd >
161178</ dl >
162179</ dd >
163- < dt id ="nitric.api.events.EventClient "> < code class ="flex name class ">
164- < span > class < span class ="ident "> EventClient </ span > </ span >
180+ < dt id ="nitric.api.events.Events "> < code class ="flex name class ">
181+ < span > class < span class ="ident "> Events </ span > </ span >
165182</ code > </ dt >
166183< dd >
167184< div class ="desc "> < p > Nitric generic publish/subscribe event client.</ p >
@@ -171,11 +188,7 @@ <h3>Class variables</h3>
171188< summary >
172189< span > Expand source code</ span >
173190</ summary >
174- < < < < < < < refs /remotes/origin/main:docs/nitric/api/models.html
175- < pre > < code class ="python "> class FailedTask(Task):
176- """Represents a failed queue publish for an event."""
177- =======
178- < pre > < code class ="python "> class EventClient(object):
191+ < pre > < code class ="python "> class Events(object):
179192 """
180193 Nitric generic publish/subscribe event client.
181194
@@ -184,37 +197,44 @@ <h3>Class variables</h3>
184197
185198 def __init__(self):
186199 """Construct a Nitric Event Client."""
187- channel = new_default_channel()
188- self._stub = EventStub(channel=channel)
189- self._topic_stub = TopicStub(channel=channel)
190- > > > > > > > feat: port faas.start to bi-di streaming with membrane:docs/nitric/api/events.html
200+ self.channel = new_default_channel()
201+ self._stub = EventServiceStub(channel=self.channel)
202+ self._topic_stub = TopicServiceStub(channel=self.channel)
203+
204+ def __del__(self):
205+ # close the channel when this client is destroyed
206+ if self.channel is not None:
207+ self.channel.close()
191208
192209 async def topics(self) -> List[Topic]:
193210 """Get a list of topics available for publishing or subscription."""
194- response = await self._topic_stub.list()
195- return [self.topic(topic.name) for topic in response.topics]
211+ try:
212+ response = await self._topic_stub.list()
213+ return [self.topic(topic.name) for topic in response.topics]
214+ except GRPCError as grpc_err:
215+ raise exception_from_grpc_error(grpc_err)
196216
197217 def topic(self, name: str) -> Topic:
198- """Return a reference a topic from the connected event service ."""
199- return Topic(_stub =self._stub , name=name)</ code > </ pre >
218+ """Return a reference to a topic."""
219+ return Topic(_events =self, name=name)</ code > </ pre >
200220</ details >
201221< h3 > Methods</ h3 >
202222< dl >
203- < dt id ="nitric.api.events.EventClient .topic "> < code class ="name flex ">
223+ < dt id ="nitric.api.events.Events .topic "> < code class ="name flex ">
204224< span > def < span class ="ident "> topic</ span > </ span > (< span > self, name: str) ‑> < a title ="nitric.api.events.Topic " href ="#nitric.api.events.Topic "> Topic</ a > </ span >
205225</ code > </ dt >
206226< dd >
207- < div class ="desc "> < p > Return a reference a topic from the connected event service .</ p > </ div >
227+ < div class ="desc "> < p > Return a reference to a topic.</ p > </ div >
208228< details class ="source ">
209229< summary >
210230< span > Expand source code</ span >
211231</ summary >
212232< pre > < code class ="python "> def topic(self, name: str) -> Topic:
213- """Return a reference a topic from the connected event service ."""
214- return Topic(_stub =self._stub , name=name)</ code > </ pre >
233+ """Return a reference to a topic."""
234+ return Topic(_events =self, name=name)</ code > </ pre >
215235</ details >
216236</ dd >
217- < dt id ="nitric.api.events.EventClient .topics "> < code class ="name flex ">
237+ < dt id ="nitric.api.events.Events .topics "> < code class ="name flex ">
218238< span > async def < span class ="ident "> topics</ span > </ span > (< span > self) ‑> List[< a title ="nitric.api.events.Topic " href ="#nitric.api.events.Topic "> Topic</ a > ]</ span >
219239</ code > </ dt >
220240< dd >
@@ -225,30 +245,29 @@ <h3>Methods</h3>
225245</ summary >
226246< pre > < code class ="python "> async def topics(self) -> List[Topic]:
227247 """Get a list of topics available for publishing or subscription."""
228- response = await self._topic_stub.list()
229- return [self.topic(topic.name) for topic in response.topics]</ code > </ pre >
248+ try:
249+ response = await self._topic_stub.list()
250+ return [self.topic(topic.name) for topic in response.topics]
251+ except GRPCError as grpc_err:
252+ raise exception_from_grpc_error(grpc_err)</ code > </ pre >
230253</ details >
231254</ dd >
232255</ dl >
233256</ dd >
234257< dt id ="nitric.api.events.Topic "> < code class ="flex name class ">
235258< span > class < span class ="ident "> Topic</ span > </ span >
236- < span > (</ span > < span > _stub : < a title ="nitric.proto.nitric.event.v1.EventStub " href ="../proto/nitric/event/v1/index.html #nitric.proto.nitric.event.v1.EventStub " > EventStub </ a > , name: str)</ span >
259+ < span > (</ span > < span > _events : < a title ="nitric.api.events.Events " href ="#nitric.api.events.Events " > Events </ a > , name: str)</ span >
237260</ code > </ dt >
238261< dd >
239262< div class ="desc "> < p > A reference to a topic on an event service, used to perform operations on that topic.</ p > </ div >
240263< details class ="source ">
241264< summary >
242265< span > Expand source code</ span >
243266</ summary >
244- < < < < < < < refs /remotes/origin/main:docs/nitric/api/models.html
245- < pre > < code class ="python "> class Task(object):
246- """Represents a NitricTask."""
247- =======
248267< pre > < code class ="python "> class Topic(object):
249268 """A reference to a topic on an event service, used to perform operations on that topic."""
250269
251- _stub: EventStub
270+ _events: Events
252271 name: str
253272
254273 async def publish(
@@ -267,10 +286,12 @@ <h3>Methods</h3>
267286 if isinstance(event, dict):
268287 # TODO: handle events that are just a payload
269288 event = Event(**event)
270- > > > > > > > feat: port faas.start to bi-di streaming with membrane:docs/nitric/api/events.html
271289
272- response = await self._stub.publish(topic=self.name, event=_event_to_wire(event))
273- return Event(**{**event.__dict__.copy(), **{"id": response.id}})</ code > </ pre >
290+ try:
291+ response = await self._events._stub.publish(topic=self.name, event=_event_to_wire(event))
292+ return Event(**{**event.__dict__.copy(), **{"id": response.id}})
293+ except GRPCError as grpc_err:
294+ raise exception_from_grpc_error(grpc_err)</ code > </ pre >
274295</ details >
275296< h3 > Class variables</ h3 >
276297< dl >
@@ -292,10 +313,6 @@ <h3>Methods</h3>
292313< summary >
293314< span > Expand source code</ span >
294315</ summary >
295- < < < < < < < refs /remotes/origin/main:docs/nitric/api/models.html
296- < pre > < code class ="python "> class Topic(object):
297- """Represents event topic metadata."""
298- =======
299316< pre > < code class ="python "> async def publish(
300317 self,
301318 event: Union[Event, dict] = None,
@@ -312,10 +329,12 @@ <h3>Methods</h3>
312329 if isinstance(event, dict):
313330 # TODO: handle events that are just a payload
314331 event = Event(**event)
315- > > > > > > > feat: port faas.start to bi-di streaming with membrane:docs/nitric/api/events.html
316332
317- response = await self._stub.publish(topic=self.name, event=_event_to_wire(event))
318- return Event(**{**event.__dict__.copy(), **{"id": response.id}})</ code > </ pre >
333+ try:
334+ response = await self._events._stub.publish(topic=self.name, event=_event_to_wire(event))
335+ return Event(**{**event.__dict__.copy(), **{"id": response.id}})
336+ except GRPCError as grpc_err:
337+ raise exception_from_grpc_error(grpc_err)</ code > </ pre >
319338</ details >
320339</ dd >
321340</ dl >
@@ -345,10 +364,10 @@ <h4><code><a title="nitric.api.events.Event" href="#nitric.api.events.Event">Eve
345364</ ul >
346365</ li >
347366< li >
348- < h4 > < code > < a title ="nitric.api.events.EventClient " href ="#nitric.api.events.EventClient " > EventClient </ a > </ code > </ h4 >
367+ < h4 > < code > < a title ="nitric.api.events.Events " href ="#nitric.api.events.Events " > Events </ a > </ code > </ h4 >
349368< ul class ="">
350- < li > < code > < a title ="nitric.api.events.EventClient .topic " href ="#nitric.api.events.EventClient .topic "> topic</ a > </ code > </ li >
351- < li > < code > < a title ="nitric.api.events.EventClient .topics " href ="#nitric.api.events.EventClient .topics "> topics</ a > </ code > </ li >
369+ < li > < code > < a title ="nitric.api.events.Events .topic " href ="#nitric.api.events.Events .topic "> topic</ a > </ code > </ li >
370+ < li > < code > < a title ="nitric.api.events.Events .topics " href ="#nitric.api.events.Events .topics "> topics</ a > </ code > </ li >
352371</ ul >
353372</ li >
354373< li >
0 commit comments