@@ -128,7 +128,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
128128 async def __aenter__ (self ):
129129 """异步上下文管理器入口"""
130130 if not self ._context_entered :
131- self ._response = self ._stream_context .__enter__ () # 检查HTTP状态码(同步版本逻辑 )
131+ self ._response = await self ._stream_context .__aenter__ () # 检查HTTP状态码(异步版本逻辑 )
132132 if self ._response .status_code != 200 :
133133 try :
134134 # 先读取完整响应内容
@@ -139,10 +139,10 @@ async def __aenter__(self):
139139 log_id = self ._response .headers .get ("x-tt-logid" , "" )
140140 error_code = error_data .get ('code' , 0 )
141141 error_msg = error_data .get ('msg' , 'Unknown error' )
142- self ._stream_context .__exit__ (None , None , None )
142+ await self ._stream_context .__aexit__ (None , None , None )
143143 raise RemoteServiceError (self ._response .status_code , error_code , error_msg , log_id )
144144 except Exception as e :
145- self ._stream_context .__exit__ (None , None , None )
145+ await self ._stream_context .__aexit__ (None , None , None )
146146 if isinstance (e , RemoteServiceError ):
147147 raise
148148 from cozeloop .internal .consts .error import InternalError
@@ -158,7 +158,7 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
158158 """异步上下文管理器出口"""
159159 await self .aclose ()
160160 if self ._context_entered :
161- return self ._stream_context .__exit__ (exc_type , exc_val , exc_tb )
161+ return await self ._stream_context .__aexit__ (exc_type , exc_val , exc_tb )
162162
163163 def __iter__ (self ):
164164 """支持for循环直接读取"""
@@ -206,9 +206,9 @@ async def aclose(self) -> None:
206206 self ._closed = True
207207 # 如果还没有进入上下文,直接关闭stream_context
208208 if not self ._context_entered :
209- if hasattr (self ._stream_context , '__exit__ ' ):
209+ if hasattr (self ._stream_context , '__aexit__ ' ):
210210 try :
211- self ._stream_context .__exit__ (None , None , None )
211+ await self ._stream_context .__aexit__ (None , None , None )
212212 except Exception :
213213 pass
214214 return
@@ -218,8 +218,8 @@ async def aclose(self) -> None:
218218 await super ().aclose ()
219219 else :
220220 # 如果response属性不存在,只关闭stream_context
221- if hasattr (self ._stream_context , '__exit__ ' ):
221+ if hasattr (self ._stream_context , '__aexit__ ' ):
222222 try :
223- self ._stream_context .__exit__ (None , None , None )
223+ await self ._stream_context .__aexit__ (None , None , None )
224224 except Exception :
225225 pass
0 commit comments