|
14 | 14 | """Wrapper classes used to modify the behavior of response objects.""" |
15 | 15 |
|
16 | 16 | import grpc |
17 | | -import time |
| 17 | +from types import SimpleNamespace, TracebackType |
| 18 | +from typing import Any, Callable, Iterator, Optional, Union |
18 | 19 |
|
19 | 20 | from google.ads.googleads import util |
20 | | -from types import SimpleNamespace |
| 21 | +from google.ads.googleads.interceptors import MetadataType |
| 22 | +from google.protobuf.message import Message as ProtobufMessageType |
21 | 23 |
|
22 | 24 |
|
23 | 25 | class _UnaryStreamWrapper(grpc.Call, grpc.Future): |
24 | | - def __init__(self, underlay_call, failure_handler, use_proto_plus=False): |
| 26 | + def __init__( |
| 27 | + self, |
| 28 | + underlay_call: Union[grpc.Call, grpc.Future], |
| 29 | + failure_handler: Callable[[Union[grpc.Call, grpc.Future]], grpc.Call], |
| 30 | + use_proto_plus: bool = False, |
| 31 | + ): |
25 | 32 | super().__init__() |
26 | | - self._underlay_call = underlay_call |
27 | | - self._failure_handler = failure_handler |
28 | | - self._exception = None |
29 | | - self._use_proto_plus = use_proto_plus |
30 | | - self._cache = SimpleNamespace(**{"initial_response_object": None}) |
31 | | - |
32 | | - def initial_metadata(self): |
| 33 | + self._underlay_call: Union[grpc.Call, grpc.Future] = underlay_call |
| 34 | + self._failure_handler: Callable[[Union[grpc.Call, grpc.Future]], grpc.Call] = ( |
| 35 | + failure_handler |
| 36 | + ) |
| 37 | + self._exception: Optional[grpc.RpcError] = None |
| 38 | + self._use_proto_plus: bool = use_proto_plus |
| 39 | + self._cache: SimpleNamespace = SimpleNamespace( |
| 40 | + **{"initial_response_object": None} |
| 41 | + ) |
| 42 | + |
| 43 | + def initial_metadata(self) -> MetadataType: |
33 | 44 | return self._underlay_call.initial_metadata() |
34 | 45 |
|
35 | | - def trailing_metadata(self): |
| 46 | + def trailing_metadata(self) -> MetadataType: |
36 | 47 | return self._underlay_call.initial_metadata() |
37 | 48 |
|
38 | | - def code(self): |
| 49 | + def code(self) -> grpc.StatusCode: |
39 | 50 | return self._underlay_call.code() |
40 | 51 |
|
41 | | - def details(self): |
| 52 | + def details(self) -> str: |
42 | 53 | return self._underlay_call.details() |
43 | 54 |
|
44 | | - def debug_error_string(self): |
| 55 | + def debug_error_string(self) -> str: |
45 | 56 | return self._underlay_call.debug_error_string() |
46 | 57 |
|
47 | | - def cancelled(self): |
| 58 | + def cancelled(self) -> bool: |
48 | 59 | return self._underlay_call.cancelled() |
49 | 60 |
|
50 | | - def running(self): |
| 61 | + def running(self) -> bool: |
51 | 62 | return self._underlay_call.running() |
52 | 63 |
|
53 | | - def done(self): |
| 64 | + def done(self) -> bool: |
54 | 65 | return self._underlay_call.done() |
55 | 66 |
|
56 | | - def result(self, timeout=None): |
| 67 | + def result(self, timeout: Optional[float] = None) -> ProtobufMessageType: |
57 | 68 | return self._underlay_call.result(timeout=timeout) |
58 | 69 |
|
59 | | - def exception(self, timeout=None): |
| 70 | + def exception(self, timeout: Optional[float] = None) -> Optional[grpc.RpcError]: |
60 | 71 | if self._exception: |
61 | 72 | return self._exception |
62 | 73 | else: |
63 | 74 | return self._underlay_call.exception(timeout=timeout) |
64 | 75 |
|
65 | | - def traceback(self, timeout=None): |
| 76 | + def traceback(self, timeout: Optional[float] = None) -> Optional[TracebackType]: |
66 | 77 | return self._underlay_call.traceback(timeout=timeout) |
67 | 78 |
|
68 | | - def add_done_callback(self, fn): |
| 79 | + def add_done_callback(self, fn: Callable[[grpc.Future], Any]) -> None: |
69 | 80 | return self._underlay_call.add_done_callback(fn) |
70 | 81 |
|
71 | | - def add_callback(self, callback): |
| 82 | + def add_callback(self, callback: Callable[[grpc.Future], Any]) -> None: |
72 | 83 | return self._underlay_call.add_callback(callback) |
73 | 84 |
|
74 | | - def is_active(self): |
| 85 | + def is_active(self) -> bool: |
75 | 86 | return self._underlay_call.is_active() |
76 | 87 |
|
77 | | - def time_remaining(self): |
| 88 | + def time_remaining(self) -> Optional[float]: |
78 | 89 | return self._underlay_call.time_remaining() |
79 | 90 |
|
80 | | - def cancel(self): |
| 91 | + def cancel(self) -> bool: |
81 | 92 | return self._underlay_call.cancel() |
82 | 93 |
|
83 | | - def __iter__(self): |
| 94 | + def __iter__(self) -> Iterator[ProtobufMessageType]: |
84 | 95 | return self |
85 | 96 |
|
86 | | - def __next__(self): |
| 97 | + def __next__(self) -> ProtobufMessageType: |
87 | 98 | try: |
88 | | - message = next(self._underlay_call) |
| 99 | + message: ProtobufMessageType = next(self._underlay_call) |
89 | 100 | # Store only the first streaming response object in _cache.initial_response_object. |
90 | 101 | # Each streaming response object captures 10,000 rows. |
91 | 102 | # The default log character limit is 5,000, so caching multiple |
92 | 103 | # streaming response objects does not make sense in most cases, |
93 | 104 | # as only [part of] 1 will get logged. |
94 | | - if self._cache.initial_response_object == None: |
| 105 | + if self._cache.initial_response_object is None: |
95 | 106 | self._cache.initial_response_object = message |
96 | | - if self._use_proto_plus == True: |
| 107 | + if self._use_proto_plus is True: |
97 | 108 | # By default this message is wrapped by proto-plus |
98 | 109 | return message |
99 | 110 | else: |
100 | 111 | return util.convert_proto_plus_to_protobuf(message) |
101 | 112 | except StopIteration: |
102 | 113 | raise |
103 | | - except Exception: |
| 114 | + except Exception as e: |
104 | 115 | try: |
105 | 116 | self._failure_handler(self._underlay_call) |
106 | | - except Exception as e: |
| 117 | + except Exception as e_inner: |
107 | 118 | self._exception = e |
108 | | - raise e |
| 119 | + raise e_inner |
109 | 120 |
|
110 | | - def get_cache(self): |
| 121 | + def get_cache(self) -> SimpleNamespace: |
111 | 122 | return self._cache |
112 | 123 |
|
113 | 124 |
|
114 | 125 | class _UnaryUnaryWrapper(grpc.Call, grpc.Future): |
115 | | - def __init__(self, underlay_call, use_proto_plus=False): |
| 126 | + def __init__( |
| 127 | + self, |
| 128 | + underlay_call: Union[grpc.Call, grpc.Future], |
| 129 | + use_proto_plus: bool = False, |
| 130 | + ): |
116 | 131 | super().__init__() |
117 | | - self._underlay_call = underlay_call |
118 | | - self._use_proto_plus = use_proto_plus |
| 132 | + self._underlay_call: Union[grpc.Call, grpc.Future] = underlay_call |
| 133 | + self._use_proto_plus: bool = use_proto_plus |
| 134 | + self._exception: Optional[grpc.RpcError] = None |
119 | 135 |
|
120 | | - def initial_metadata(self): |
| 136 | + def initial_metadata(self) -> MetadataType: |
121 | 137 | return self._underlay_call.initial_metadata() |
122 | 138 |
|
123 | | - def trailing_metadata(self): |
| 139 | + def trailing_metadata(self) -> MetadataType: |
124 | 140 | return self._underlay_call.initial_metadata() |
125 | 141 |
|
126 | | - def code(self): |
| 142 | + def code(self) -> grpc.StatusCode: |
127 | 143 | return self._underlay_call.code() |
128 | 144 |
|
129 | | - def details(self): |
| 145 | + def details(self) -> str: |
130 | 146 | return self._underlay_call.details() |
131 | 147 |
|
132 | | - def debug_error_string(self): |
| 148 | + def debug_error_string(self) -> str: |
133 | 149 | return self._underlay_call.debug_error_string() |
134 | 150 |
|
135 | | - def cancelled(self): |
| 151 | + def cancelled(self) -> bool: |
136 | 152 | return self._underlay_call.cancelled() |
137 | 153 |
|
138 | | - def running(self): |
| 154 | + def running(self) -> bool: |
139 | 155 | return self._underlay_call.running() |
140 | 156 |
|
141 | | - def done(self): |
| 157 | + def done(self) -> bool: |
142 | 158 | return self._underlay_call.done() |
143 | 159 |
|
144 | | - def result(self, timeout=None): |
145 | | - message = self._underlay_call.result() |
146 | | - if self._use_proto_plus == True: |
| 160 | + def result(self, timeout: Optional[float] = None) -> ProtobufMessageType: |
| 161 | + message: ProtobufMessageType = self._underlay_call.result() |
| 162 | + if self._use_proto_plus is True: |
147 | 163 | return message |
148 | 164 | else: |
149 | 165 | return util.convert_proto_plus_to_protobuf(message) |
150 | 166 |
|
151 | | - def exception(self, timeout=None): |
| 167 | + def exception(self, timeout: Optional[float] = None) -> Optional[grpc.RpcError]: |
152 | 168 | if self._exception: |
153 | 169 | return self._exception |
154 | 170 | else: |
155 | 171 | return self._underlay_call.exception(timeout=timeout) |
156 | 172 |
|
157 | | - def traceback(self, timeout=None): |
| 173 | + def traceback(self, timeout: Optional[float] = None) -> Optional[TracebackType]: |
158 | 174 | return self._underlay_call.traceback(timeout=timeout) |
159 | 175 |
|
160 | | - def add_done_callback(self, fn): |
| 176 | + def add_done_callback(self, fn: Callable[[grpc.Future], Any]) -> None: |
161 | 177 | return self._underlay_call.add_done_callback(fn) |
162 | 178 |
|
163 | | - def add_callback(self, callback): |
| 179 | + def add_callback(self, callback: Callable[[grpc.Future], Any]) -> None: |
164 | 180 | return self._underlay_call.add_callback(callback) |
165 | 181 |
|
166 | | - def is_active(self): |
| 182 | + def is_active(self) -> bool: |
167 | 183 | return self._underlay_call.is_active() |
168 | 184 |
|
169 | | - def time_remaining(self): |
| 185 | + def time_remaining(self) -> Optional[float]: |
170 | 186 | return self._underlay_call.time_remaining() |
171 | 187 |
|
172 | | - def cancel(self): |
| 188 | + def cancel(self) -> bool: |
173 | 189 | return self._underlay_call.cancel() |
174 | 190 |
|
175 | | - def __iter__(self): |
176 | | - if self._use_proto_plus == True: |
| 191 | + def __iter__(self) -> Iterator[ProtobufMessageType]: |
| 192 | + if self._use_proto_plus is True: |
177 | 193 | return self |
178 | 194 | else: |
179 | 195 | return util.convert_proto_plus_to_protobuf(self) |
180 | 196 |
|
181 | | - def __next__(self): |
| 197 | + def __next__(self) -> ProtobufMessageType: |
182 | 198 | return next(self._underlay_call) |
0 commit comments