11# This file was auto-generated by Fern from our API Definition.
22
3+ import contextlib
34import typing
45from json .decoder import JSONDecodeError
56
@@ -16,9 +17,10 @@ class RawFileStashClient:
1617 def __init__ (self , * , client_wrapper : SyncClientWrapper ):
1718 self ._client_wrapper = client_wrapper
1819
20+ @contextlib .contextmanager
1921 def download_file (
2022 self , * , s_3_key : str , request_options : typing .Optional [RequestOptions ] = None
21- ) -> HttpResponse [None ]:
23+ ) -> typing . Iterator [ HttpResponse [typing . Iterator [ bytes ]] ]:
2224 """
2325 Download a file from File Stash
2426
@@ -27,47 +29,59 @@ def download_file(
2729 s_3_key : str
2830
2931 request_options : typing.Optional[RequestOptions]
30- Request-specific configuration.
32+ Request-specific configuration. You can pass in configuration such as `chunk_size`, and more to customize the request and response.
3133
3234 Returns
3335 -------
34- HttpResponse[None]
36+ typing.Iterator[HttpResponse[typing.Iterator[bytes]]]
37+ file contents
3538 """
36- _response = self ._client_wrapper .httpx_client .request (
39+ with self ._client_wrapper .httpx_client .stream (
3740 f"v1/connect/{ jsonable_encoder (self ._client_wrapper ._project_id )} /file_stash/download" ,
3841 method = "GET" ,
3942 params = {
4043 "s3_key" : s_3_key ,
4144 },
4245 request_options = request_options ,
43- )
44- try :
45- if 200 <= _response .status_code < 300 :
46- return HttpResponse (response = _response , data = None )
47- if _response .status_code == 429 :
48- raise TooManyRequestsError (
49- headers = dict (_response .headers ),
50- body = typing .cast (
51- typing .Optional [typing .Any ],
52- parse_obj_as (
53- type_ = typing .Optional [typing .Any ], # type: ignore
54- object_ = _response .json (),
55- ),
56- ),
57- )
58- _response_json = _response .json ()
59- except JSONDecodeError :
60- raise ApiError (status_code = _response .status_code , headers = dict (_response .headers ), body = _response .text )
61- raise ApiError (status_code = _response .status_code , headers = dict (_response .headers ), body = _response_json )
46+ ) as _response :
47+
48+ def _stream () -> HttpResponse [typing .Iterator [bytes ]]:
49+ try :
50+ if 200 <= _response .status_code < 300 :
51+ _chunk_size = request_options .get ("chunk_size" , None ) if request_options is not None else None
52+ return HttpResponse (
53+ response = _response , data = (_chunk for _chunk in _response .iter_bytes (chunk_size = _chunk_size ))
54+ )
55+ _response .read ()
56+ if _response .status_code == 429 :
57+ raise TooManyRequestsError (
58+ headers = dict (_response .headers ),
59+ body = typing .cast (
60+ typing .Optional [typing .Any ],
61+ parse_obj_as (
62+ type_ = typing .Optional [typing .Any ], # type: ignore
63+ object_ = _response .json (),
64+ ),
65+ ),
66+ )
67+ _response_json = _response .json ()
68+ except JSONDecodeError :
69+ raise ApiError (
70+ status_code = _response .status_code , headers = dict (_response .headers ), body = _response .text
71+ )
72+ raise ApiError (status_code = _response .status_code , headers = dict (_response .headers ), body = _response_json )
73+
74+ yield _stream ()
6275
6376
6477class AsyncRawFileStashClient :
6578 def __init__ (self , * , client_wrapper : AsyncClientWrapper ):
6679 self ._client_wrapper = client_wrapper
6780
81+ @contextlib .asynccontextmanager
6882 async def download_file (
6983 self , * , s_3_key : str , request_options : typing .Optional [RequestOptions ] = None
70- ) -> AsyncHttpResponse [None ]:
84+ ) -> typing . AsyncIterator [ AsyncHttpResponse [typing . AsyncIterator [ bytes ]] ]:
7185 """
7286 Download a file from File Stash
7387
@@ -76,35 +90,47 @@ async def download_file(
7690 s_3_key : str
7791
7892 request_options : typing.Optional[RequestOptions]
79- Request-specific configuration.
93+ Request-specific configuration. You can pass in configuration such as `chunk_size`, and more to customize the request and response.
8094
8195 Returns
8296 -------
83- AsyncHttpResponse[None]
97+ typing.AsyncIterator[AsyncHttpResponse[typing.AsyncIterator[bytes]]]
98+ file contents
8499 """
85- _response = await self ._client_wrapper .httpx_client .request (
100+ async with self ._client_wrapper .httpx_client .stream (
86101 f"v1/connect/{ jsonable_encoder (self ._client_wrapper ._project_id )} /file_stash/download" ,
87102 method = "GET" ,
88103 params = {
89104 "s3_key" : s_3_key ,
90105 },
91106 request_options = request_options ,
92- )
93- try :
94- if 200 <= _response .status_code < 300 :
95- return AsyncHttpResponse (response = _response , data = None )
96- if _response .status_code == 429 :
97- raise TooManyRequestsError (
98- headers = dict (_response .headers ),
99- body = typing .cast (
100- typing .Optional [typing .Any ],
101- parse_obj_as (
102- type_ = typing .Optional [typing .Any ], # type: ignore
103- object_ = _response .json (),
104- ),
105- ),
106- )
107- _response_json = _response .json ()
108- except JSONDecodeError :
109- raise ApiError (status_code = _response .status_code , headers = dict (_response .headers ), body = _response .text )
110- raise ApiError (status_code = _response .status_code , headers = dict (_response .headers ), body = _response_json )
107+ ) as _response :
108+
109+ async def _stream () -> AsyncHttpResponse [typing .AsyncIterator [bytes ]]:
110+ try :
111+ if 200 <= _response .status_code < 300 :
112+ _chunk_size = request_options .get ("chunk_size" , None ) if request_options is not None else None
113+ return AsyncHttpResponse (
114+ response = _response ,
115+ data = (_chunk async for _chunk in _response .aiter_bytes (chunk_size = _chunk_size )),
116+ )
117+ await _response .aread ()
118+ if _response .status_code == 429 :
119+ raise TooManyRequestsError (
120+ headers = dict (_response .headers ),
121+ body = typing .cast (
122+ typing .Optional [typing .Any ],
123+ parse_obj_as (
124+ type_ = typing .Optional [typing .Any ], # type: ignore
125+ object_ = _response .json (),
126+ ),
127+ ),
128+ )
129+ _response_json = _response .json ()
130+ except JSONDecodeError :
131+ raise ApiError (
132+ status_code = _response .status_code , headers = dict (_response .headers ), body = _response .text
133+ )
134+ raise ApiError (status_code = _response .status_code , headers = dict (_response .headers ), body = _response_json )
135+
136+ yield await _stream ()
0 commit comments