5353class DispatchApiClient (BaseApiClient [dispatch_pb2_grpc .MicrogridDispatchServiceStub ]):
5454 """Dispatch API client."""
5555
56+ # pylint: disable-next=too-many-arguments
5657 def __init__ (
5758 self ,
5859 * ,
5960 server_url : str ,
6061 key : str ,
6162 connect : bool = True ,
63+ call_timeout : timedelta = timedelta (seconds = 60 ),
64+ stream_timeout : timedelta = timedelta (minutes = 5 ),
6265 ) -> None :
6366 """Initialize the client.
6467
6568 Args:
6669 server_url: The URL of the server to connect to.
6770 key: API key to use for authentication.
6871 connect: Whether to connect to the service immediately.
72+ call_timeout: Timeout for gRPC calls, default is 60 seconds.
73+ stream_timeout: Timeout for gRPC streams, default is 5 minutes.
6974 """
7075 super ().__init__ (
7176 server_url ,
@@ -82,6 +87,9 @@ def __init__(
8287 ] = {}
8388 """A dictionary of streamers, keyed by microgrid_id."""
8489
90+ self ._call_timeout = call_timeout .total_seconds ()
91+ self ._stream_timeout = stream_timeout .total_seconds ()
92+
8593 @property
8694 def stub (self ) -> dispatch_pb2_grpc .MicrogridDispatchServiceAsyncStub :
8795 """The stub for the service."""
@@ -177,7 +185,9 @@ def to_interval(
177185 while True :
178186 response = await cast (
179187 Awaitable [ListMicrogridDispatchesResponse ],
180- self .stub .ListMicrogridDispatches (request , metadata = self ._metadata ),
188+ self .stub .ListMicrogridDispatches (
189+ request , metadata = self ._metadata , timeout = self ._call_timeout
190+ ),
181191 )
182192
183193 yield (Dispatch .from_protobuf (dispatch ) for dispatch in response .dispatches )
@@ -234,7 +244,9 @@ def _get_stream(
234244 stream_method = lambda : cast (
235245 AsyncIterator [StreamMicrogridDispatchesResponse ],
236246 self .stub .StreamMicrogridDispatches (
237- request , metadata = self ._metadata
247+ request ,
248+ metadata = self ._metadata ,
249+ timeout = self ._stream_timeout ,
238250 ),
239251 ),
240252 transform = DispatchEvent .from_protobuf ,
@@ -303,7 +315,9 @@ async def create( # pylint: disable=too-many-positional-arguments
303315 response = await cast (
304316 Awaitable [CreateMicrogridDispatchResponse ],
305317 self .stub .CreateMicrogridDispatch (
306- request .to_protobuf (), metadata = self ._metadata
318+ request .to_protobuf (),
319+ metadata = self ._metadata ,
320+ timeout = self ._call_timeout ,
307321 ),
308322 )
309323
@@ -394,7 +408,9 @@ async def update(
394408
395409 response = await cast (
396410 Awaitable [UpdateMicrogridDispatchResponse ],
397- self .stub .UpdateMicrogridDispatch (msg , metadata = self ._metadata ),
411+ self .stub .UpdateMicrogridDispatch (
412+ msg , metadata = self ._metadata , timeout = self ._call_timeout
413+ ),
398414 )
399415
400416 return Dispatch .from_protobuf (response .dispatch )
@@ -414,7 +430,9 @@ async def get(self, *, microgrid_id: int, dispatch_id: int) -> Dispatch:
414430 )
415431 response = await cast (
416432 Awaitable [GetMicrogridDispatchResponse ],
417- self .stub .GetMicrogridDispatch (request , metadata = self ._metadata ),
433+ self .stub .GetMicrogridDispatch (
434+ request , metadata = self ._metadata , timeout = self ._call_timeout
435+ ),
418436 )
419437 return Dispatch .from_protobuf (response .dispatch )
420438
@@ -430,5 +448,7 @@ async def delete(self, *, microgrid_id: int, dispatch_id: int) -> None:
430448 )
431449 await cast (
432450 Awaitable [None ],
433- self .stub .DeleteMicrogridDispatch (request , metadata = self ._metadata ),
451+ self .stub .DeleteMicrogridDispatch (
452+ request , metadata = self ._metadata , timeout = self ._call_timeout
453+ ),
434454 )
0 commit comments