|
2 | 2 |
|
3 | 3 | import base64 |
4 | 4 | import typing |
| 5 | +from collections.abc import AsyncIterator, Iterator |
5 | 6 |
|
6 | 7 | from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper |
7 | 8 | from ..core.request_options import RequestOptions |
@@ -88,13 +89,26 @@ def get( |
88 | 89 | additional_headers=downstream_headers, |
89 | 90 | additional_query_parameters=params or {}, |
90 | 91 | ) |
91 | | - _response = self._raw_client.get( |
| 92 | + ctx = self._raw_client.get( |
92 | 93 | url_64, |
93 | 94 | external_user_id=external_user_id, |
94 | 95 | account_id=account_id, |
95 | 96 | request_options=request_options, |
96 | 97 | ) |
97 | | - return _response.data |
| 98 | + _response = ctx.__enter__() |
| 99 | + data = _response.data |
| 100 | + |
| 101 | + if not isinstance(data, Iterator): |
| 102 | + ctx.__exit__(None, None, None) |
| 103 | + return data |
| 104 | + |
| 105 | + def _stream() -> typing.Iterator[bytes]: |
| 106 | + try: |
| 107 | + for chunk in data: |
| 108 | + yield chunk |
| 109 | + finally: |
| 110 | + ctx.__exit__(None, None, None) |
| 111 | + return _stream() |
98 | 112 |
|
99 | 113 | def post( |
100 | 114 | self, |
@@ -162,14 +176,27 @@ def post( |
162 | 176 | additional_headers=downstream_headers, |
163 | 177 | additional_query_parameters=params or {}, |
164 | 178 | ) |
165 | | - _response = self._raw_client.post( |
| 179 | + ctx = self._raw_client.post( |
166 | 180 | url_64, |
167 | 181 | external_user_id=external_user_id, |
168 | 182 | account_id=account_id, |
169 | 183 | request=body or {}, |
170 | 184 | request_options=request_options, |
171 | 185 | ) |
172 | | - return _response.data |
| 186 | + _response = ctx.__enter__() |
| 187 | + data = _response.data |
| 188 | + |
| 189 | + if not isinstance(data, Iterator): |
| 190 | + ctx.__exit__(None, None, None) |
| 191 | + return data |
| 192 | + |
| 193 | + def _stream() -> typing.Iterator[bytes]: |
| 194 | + try: |
| 195 | + for chunk in data: |
| 196 | + yield chunk |
| 197 | + finally: |
| 198 | + ctx.__exit__(None, None, None) |
| 199 | + return _stream() |
173 | 200 |
|
174 | 201 | def put( |
175 | 202 | self, |
@@ -237,14 +264,27 @@ def put( |
237 | 264 | additional_headers=downstream_headers, |
238 | 265 | additional_query_parameters=params or {}, |
239 | 266 | ) |
240 | | - _response = self._raw_client.put( |
| 267 | + ctx = self._raw_client.put( |
241 | 268 | url_64, |
242 | 269 | external_user_id=external_user_id, |
243 | 270 | account_id=account_id, |
244 | 271 | request=body or {}, |
245 | 272 | request_options=request_options, |
246 | 273 | ) |
247 | | - return _response.data |
| 274 | + _response = ctx.__enter__() |
| 275 | + data = _response.data |
| 276 | + |
| 277 | + if not isinstance(data, Iterator): |
| 278 | + ctx.__exit__(None, None, None) |
| 279 | + return data |
| 280 | + |
| 281 | + def _stream() -> typing.Iterator[bytes]: |
| 282 | + try: |
| 283 | + for chunk in data: |
| 284 | + yield chunk |
| 285 | + finally: |
| 286 | + ctx.__exit__(None, None, None) |
| 287 | + return _stream() |
248 | 288 |
|
249 | 289 | def delete( |
250 | 290 | self, |
@@ -304,13 +344,26 @@ def delete( |
304 | 344 | additional_headers=downstream_headers, |
305 | 345 | additional_query_parameters=params or {}, |
306 | 346 | ) |
307 | | - _response = self._raw_client.delete( |
| 347 | + ctx = self._raw_client.delete( |
308 | 348 | url_64, |
309 | 349 | external_user_id=external_user_id, |
310 | 350 | account_id=account_id, |
311 | 351 | request_options=request_options, |
312 | 352 | ) |
313 | | - return _response.data |
| 353 | + _response = ctx.__enter__() |
| 354 | + data = _response.data |
| 355 | + |
| 356 | + if not isinstance(data, Iterator): |
| 357 | + ctx.__exit__(None, None, None) |
| 358 | + return data |
| 359 | + |
| 360 | + def _stream() -> typing.Iterator[bytes]: |
| 361 | + try: |
| 362 | + for chunk in data: |
| 363 | + yield chunk |
| 364 | + finally: |
| 365 | + ctx.__exit__(None, None, None) |
| 366 | + return _stream() |
314 | 367 |
|
315 | 368 | def patch( |
316 | 369 | self, |
@@ -378,14 +431,27 @@ def patch( |
378 | 431 | additional_headers=downstream_headers, |
379 | 432 | additional_query_parameters=params or {}, |
380 | 433 | ) |
381 | | - _response = self._raw_client.patch( |
| 434 | + ctx = self._raw_client.patch( |
382 | 435 | url_64, |
383 | 436 | external_user_id=external_user_id, |
384 | 437 | account_id=account_id, |
385 | 438 | request=body or {}, |
386 | 439 | request_options=request_options, |
387 | 440 | ) |
388 | | - return _response.data |
| 441 | + _response = ctx.__enter__() |
| 442 | + data = _response.data |
| 443 | + |
| 444 | + if not isinstance(data, Iterator): |
| 445 | + ctx.__exit__(None, None, None) |
| 446 | + return data |
| 447 | + |
| 448 | + def _stream() -> typing.Iterator[bytes]: |
| 449 | + try: |
| 450 | + for chunk in data: |
| 451 | + yield chunk |
| 452 | + finally: |
| 453 | + ctx.__exit__(None, None, None) |
| 454 | + return _stream() |
389 | 455 |
|
390 | 456 |
|
391 | 457 | class AsyncProxyClient: |
@@ -472,13 +538,26 @@ async def main() -> None: |
472 | 538 | additional_headers=downstream_headers, |
473 | 539 | additional_query_parameters=params or {}, |
474 | 540 | ) |
475 | | - _response = await self._raw_client.get( |
| 541 | + ctx = self._raw_client.get( |
476 | 542 | url_64, |
477 | 543 | external_user_id=external_user_id, |
478 | 544 | account_id=account_id, |
479 | 545 | request_options=request_options, |
480 | 546 | ) |
481 | | - return _response.data |
| 547 | + _response = await ctx.__aenter__() |
| 548 | + data = _response.data |
| 549 | + |
| 550 | + if not isinstance(data, AsyncIterator): |
| 551 | + await ctx.__aexit__(None, None, None) |
| 552 | + return data |
| 553 | + |
| 554 | + async def _stream() -> typing.AsyncIterator[bytes]: |
| 555 | + try: |
| 556 | + async for chunk in data: |
| 557 | + yield chunk |
| 558 | + finally: |
| 559 | + await ctx.__aexit__(None, None, None) |
| 560 | + return _stream() |
482 | 561 |
|
483 | 562 | async def post( |
484 | 563 | self, |
@@ -554,14 +633,27 @@ async def main() -> None: |
554 | 633 | additional_headers=downstream_headers, |
555 | 634 | additional_query_parameters=params or {}, |
556 | 635 | ) |
557 | | - _response = await self._raw_client.post( |
| 636 | + ctx = self._raw_client.post( |
558 | 637 | url_64, |
559 | 638 | external_user_id=external_user_id, |
560 | 639 | account_id=account_id, |
561 | 640 | request=body or {}, |
562 | 641 | request_options=request_options, |
563 | 642 | ) |
564 | | - return _response.data |
| 643 | + _response = await ctx.__aenter__() |
| 644 | + data = _response.data |
| 645 | + |
| 646 | + if not isinstance(data, AsyncIterator): |
| 647 | + await ctx.__aexit__(None, None, None) |
| 648 | + return data |
| 649 | + |
| 650 | + async def _stream() -> typing.AsyncIterator[bytes]: |
| 651 | + try: |
| 652 | + async for chunk in data: |
| 653 | + yield chunk |
| 654 | + finally: |
| 655 | + await ctx.__aexit__(None, None, None) |
| 656 | + return _stream() |
565 | 657 |
|
566 | 658 | async def put( |
567 | 659 | self, |
@@ -637,14 +729,27 @@ async def main() -> None: |
637 | 729 | additional_headers=downstream_headers, |
638 | 730 | additional_query_parameters=params or {}, |
639 | 731 | ) |
640 | | - _response = await self._raw_client.put( |
| 732 | + ctx = self._raw_client.put( |
641 | 733 | url_64, |
642 | 734 | external_user_id=external_user_id, |
643 | 735 | account_id=account_id, |
644 | 736 | request=body or {}, |
645 | 737 | request_options=request_options, |
646 | 738 | ) |
647 | | - return _response.data |
| 739 | + _response = await ctx.__aenter__() |
| 740 | + data = _response.data |
| 741 | + |
| 742 | + if not isinstance(data, AsyncIterator): |
| 743 | + await ctx.__aexit__(None, None, None) |
| 744 | + return data |
| 745 | + |
| 746 | + async def _stream() -> typing.AsyncIterator[bytes]: |
| 747 | + try: |
| 748 | + async for chunk in data: |
| 749 | + yield chunk |
| 750 | + finally: |
| 751 | + await ctx.__aexit__(None, None, None) |
| 752 | + return _stream() |
648 | 753 |
|
649 | 754 | async def delete( |
650 | 755 | self, |
@@ -712,13 +817,26 @@ async def main() -> None: |
712 | 817 | additional_headers=downstream_headers, |
713 | 818 | additional_query_parameters=params or {}, |
714 | 819 | ) |
715 | | - _response = await self._raw_client.delete( |
| 820 | + ctx = self._raw_client.delete( |
716 | 821 | url_64, |
717 | 822 | external_user_id=external_user_id, |
718 | 823 | account_id=account_id, |
719 | 824 | request_options=request_options, |
720 | 825 | ) |
721 | | - return _response.data |
| 826 | + _response = await ctx.__aenter__() |
| 827 | + data = _response.data |
| 828 | + |
| 829 | + if not isinstance(data, AsyncIterator): |
| 830 | + await ctx.__aexit__(None, None, None) |
| 831 | + return data |
| 832 | + |
| 833 | + async def _stream() -> typing.AsyncIterator[bytes]: |
| 834 | + try: |
| 835 | + async for chunk in data: |
| 836 | + yield chunk |
| 837 | + finally: |
| 838 | + await ctx.__aexit__(None, None, None) |
| 839 | + return _stream() |
722 | 840 |
|
723 | 841 | async def patch( |
724 | 842 | self, |
@@ -794,11 +912,24 @@ async def main() -> None: |
794 | 912 | additional_headers=downstream_headers, |
795 | 913 | additional_query_parameters=params or {}, |
796 | 914 | ) |
797 | | - _response = await self._raw_client.patch( |
| 915 | + ctx = self._raw_client.patch( |
798 | 916 | url_64, |
799 | 917 | external_user_id=external_user_id, |
800 | 918 | account_id=account_id, |
801 | 919 | request=body or {}, |
802 | 920 | request_options=request_options, |
803 | 921 | ) |
804 | | - return _response.data |
| 922 | + _response = await ctx.__aenter__() |
| 923 | + data = _response.data |
| 924 | + |
| 925 | + if not isinstance(data, AsyncIterator): |
| 926 | + await ctx.__aexit__(None, None, None) |
| 927 | + return data |
| 928 | + |
| 929 | + async def _stream() -> typing.AsyncIterator[bytes]: |
| 930 | + try: |
| 931 | + async for chunk in data: |
| 932 | + yield chunk |
| 933 | + finally: |
| 934 | + await ctx.__aexit__(None, None, None) |
| 935 | + return _stream() |
0 commit comments