@@ -139,7 +139,7 @@ def post_stream(
139139 json : Union [BaseModel , Dict ] = None ,
140140 timeout : Optional [int ] = None ,
141141 ):
142- """发起流式POST请求,返回stream_context """
142+ """Initiate streaming POST request, return stream_context """
143143 url = self ._build_url (path )
144144 headers = self ._set_headers ({"Content-Type" : "application/json" })
145145
@@ -149,7 +149,7 @@ def post_stream(
149149 _timeout = timeout if timeout is not None else self .timeout
150150
151151 try :
152- # 返回stream_context,让StreamReader管理上下文
152+ # Return stream_context, let StreamReader manage context
153153 stream_context = self .http_client .stream (
154154 "POST" ,
155155 url ,
@@ -162,13 +162,54 @@ def post_stream(
162162 logger .error (f"Http client stream request failed, path: { path } , err: { e } ." )
163163 raise consts .NetworkError from e
164164
165+ async def arequest (
166+ self ,
167+ path : str ,
168+ method : str ,
169+ response_model : Type [T ],
170+ * ,
171+ params : Optional [Dict [str , str ]] = None ,
172+ form : Optional [Dict [str , str ]] = None ,
173+ json : Optional [Union [BaseModel , Dict ]] = None ,
174+ files : Optional [Dict [str , FileType ]] = None ,
175+ headers : Optional [Dict [str , str ]] = None ,
176+ timeout : Optional [int ] = None ,
177+ ) -> T :
178+ url = self ._build_url (path )
179+ _headers = self ._set_headers (headers )
180+
181+ _timeout = timeout if timeout is not None else self .timeout
182+
183+ if isinstance (json , BaseModel ):
184+ if pydantic .VERSION .startswith ('1' ):
185+ json = json .dict (by_alias = True )
186+ else :
187+ json = json .model_dump (by_alias = True )
188+
189+ try :
190+ response = await self .http_client .arequest (
191+ method ,
192+ url ,
193+ params = params ,
194+ data = form ,
195+ json = json ,
196+ files = files ,
197+ headers = _headers ,
198+ timeout = _timeout
199+ )
200+ except httpx .HTTPError as e :
201+ logger .error (f"Http client request failed, path: { path } , err: { e } ." )
202+ raise consts .NetworkError from e
203+
204+ return parse_response (url , response , response_model )
205+
165206 async def apost_stream (
166207 self ,
167208 path : str ,
168209 json : Union [BaseModel , Dict ] = None ,
169210 timeout : Optional [int ] = None ,
170211 ):
171- """发起异步流式POST请求,返回stream_context """
212+ """Initiate asynchronous streaming POST request, return stream_context """
172213 url = self ._build_url (path )
173214 headers = self ._set_headers ({"Content-Type" : "application/json" })
174215
@@ -178,8 +219,8 @@ async def apost_stream(
178219 _timeout = timeout if timeout is not None else self .timeout
179220
180221 try :
181- # 返回stream_context,让StreamReader管理上下文
182- stream_context = self .http_client .stream (
222+ # Return stream_context, let StreamReader manage context
223+ stream_context = self .http_client .astream (
183224 "POST" ,
184225 url ,
185226 json = json ,
0 commit comments