|
| 1 | +from abc import ABC |
| 2 | +from typing import ( |
| 3 | + TYPE_CHECKING, |
| 4 | + AsyncIterable, |
| 5 | + AsyncIterator, |
| 6 | + Iterable, |
| 7 | + Mapping, |
| 8 | + Optional, |
| 9 | + Union, |
| 10 | +) |
| 11 | + |
| 12 | +import grpc |
| 13 | + |
| 14 | + |
| 15 | +if TYPE_CHECKING: |
| 16 | + from .._types import ( |
| 17 | + T, |
| 18 | + IProtoMessage, |
| 19 | + ) |
| 20 | + |
| 21 | + |
| 22 | +Value = Union[str, bytes] |
| 23 | +MetadataLike = Union[Mapping[str, Value], Iterable[tuple[str, Value]]] |
| 24 | +MessageSource = Union[Iterable["IProtoMessage"], AsyncIterable["IProtoMessage"]] |
| 25 | + |
| 26 | + |
| 27 | +class ServiceStub(ABC): |
| 28 | + |
| 29 | + def __init__( |
| 30 | + self, |
| 31 | + channel: grpc.aio.Channel, |
| 32 | + *, |
| 33 | + timeout: Optional[float] = None, |
| 34 | + metadata: Optional[MetadataLike] = None, |
| 35 | + ) -> None: |
| 36 | + self.channel = channel |
| 37 | + self.timeout = timeout |
| 38 | + self.metadata = metadata |
| 39 | + |
| 40 | + def _resolve_request_kwargs( |
| 41 | + self, |
| 42 | + timeout: Optional[float], |
| 43 | + metadata: Optional[MetadataLike], |
| 44 | + ): |
| 45 | + # Avoid creating dict if no overrides needed |
| 46 | + if timeout is None and metadata is None: |
| 47 | + # Return cached default kwargs if both timeout and metadata are None |
| 48 | + if not hasattr(self, '_default_kwargs'): |
| 49 | + self._default_kwargs = { |
| 50 | + "timeout": self.timeout, |
| 51 | + "metadata": self.metadata, |
| 52 | + } |
| 53 | + return self._default_kwargs |
| 54 | + |
| 55 | + return { |
| 56 | + "timeout": self.timeout if timeout is None else timeout, |
| 57 | + "metadata": self.metadata if metadata is None else metadata, |
| 58 | + } |
| 59 | + |
| 60 | + async def _unary_unary( |
| 61 | + self, |
| 62 | + stub_method: grpc.aio.UnaryUnaryMultiCallable, |
| 63 | + request: "IProtoMessage", |
| 64 | + *, |
| 65 | + timeout: Optional[float] = None, |
| 66 | + metadata: Optional[MetadataLike] = None, |
| 67 | + ) -> "T": |
| 68 | + return await stub_method( |
| 69 | + request, |
| 70 | + **self._resolve_request_kwargs(timeout, metadata), |
| 71 | + ) |
| 72 | + |
| 73 | + async def _unary_stream( |
| 74 | + self, |
| 75 | + stub_method: grpc.aio.UnaryStreamMultiCallable, |
| 76 | + request: "IProtoMessage", |
| 77 | + *, |
| 78 | + timeout: Optional[float] = None, |
| 79 | + metadata: Optional[MetadataLike] = None, |
| 80 | + ) -> AsyncIterator["T"]: |
| 81 | + call = stub_method( |
| 82 | + request, |
| 83 | + **self._resolve_request_kwargs(timeout, metadata), |
| 84 | + ) |
| 85 | + async for response in call: |
| 86 | + yield response |
| 87 | + |
| 88 | + async def _stream_unary( |
| 89 | + self, |
| 90 | + stub_method: grpc.aio.StreamUnaryMultiCallable, |
| 91 | + request_iterator: MessageSource, |
| 92 | + *, |
| 93 | + timeout: Optional[float] = None, |
| 94 | + metadata: Optional[MetadataLike] = None, |
| 95 | + ) -> "T": |
| 96 | + call = stub_method( |
| 97 | + self._wrap_message_iterator(request_iterator), |
| 98 | + **self._resolve_request_kwargs(timeout, metadata), |
| 99 | + ) |
| 100 | + return await call |
| 101 | + |
| 102 | + async def _stream_stream( |
| 103 | + self, |
| 104 | + stub_method: grpc.aio.StreamStreamMultiCallable, |
| 105 | + request_iterator: MessageSource, |
| 106 | + *, |
| 107 | + timeout: Optional[float] = None, |
| 108 | + metadata: Optional[MetadataLike] = None, |
| 109 | + ) -> AsyncIterator["T"]: |
| 110 | + call = stub_method( |
| 111 | + self._wrap_message_iterator(request_iterator), |
| 112 | + **self._resolve_request_kwargs(timeout, metadata), |
| 113 | + ) |
| 114 | + async for response in call: |
| 115 | + yield response |
| 116 | + |
| 117 | + @staticmethod |
| 118 | + def _wrap_message_iterator( |
| 119 | + messages: MessageSource, |
| 120 | + ) -> AsyncIterator["IProtoMessage"]: |
| 121 | + if hasattr(messages, '__aiter__'): |
| 122 | + async def async_wrapper(): |
| 123 | + async for message in messages: |
| 124 | + yield message |
| 125 | + return async_wrapper() |
| 126 | + else: |
| 127 | + async def sync_wrapper(): |
| 128 | + for message in messages: |
| 129 | + yield message |
| 130 | + return sync_wrapper() |
0 commit comments