6
6
import logging
7
7
from typing import TYPE_CHECKING , Any
8
8
9
+ from zeep .cache import SqliteCache
9
10
from zeep .transports import Transport
10
11
from zeep .utils import get_version
11
12
from zeep .wsdl .utils import etree_to_string
12
13
13
14
import aiohttp
14
15
import httpx
15
- from aiohttp import ClientResponse , ClientSession , ClientTimeout , TCPConnector
16
+ from aiohttp import ClientResponse , ClientSession
16
17
from requests import Response
17
18
18
19
if TYPE_CHECKING :
@@ -26,61 +27,44 @@ class AIOHTTPTransport(Transport):
26
27
27
28
def __init__ (
28
29
self ,
29
- session : ClientSession | None = None ,
30
- timeout : float = 300 ,
31
- operation_timeout : float | None = None ,
30
+ session : ClientSession ,
32
31
verify_ssl : bool = True ,
33
32
proxy : str | None = None ,
33
+ cache : SqliteCache | None = None ,
34
34
) -> None :
35
35
"""
36
36
Initialize the transport.
37
37
38
38
Args:
39
- session: The aiohttp ClientSession to use
40
- timeout: The default timeout for requests in seconds
41
- operation_timeout: The default timeout for operations in seconds
39
+ session: The aiohttp ClientSession to use (required). The session's
40
+ timeout configuration will be used for all requests.
42
41
verify_ssl: Whether to verify SSL certificates
43
42
proxy: Proxy URL to use
44
43
45
44
"""
46
45
super ().__init__ (
47
- cache = None ,
48
- timeout = timeout ,
49
- operation_timeout = operation_timeout ,
46
+ cache = cache ,
47
+ timeout = session . timeout . total ,
48
+ operation_timeout = session . timeout . sock_read ,
50
49
)
51
50
52
51
# Override parent's session with aiohttp session
53
52
self .session = session
54
- self .timeout = timeout
55
- self .operation_timeout = operation_timeout
56
53
self .verify_ssl = verify_ssl
57
54
self .proxy = proxy
58
- self ._close_session = session is None
59
-
60
- # Pre-create timeout object to avoid recreating it on each request
61
- effective_timeout = self .operation_timeout or self .timeout
62
- self ._client_timeout = (
63
- ClientTimeout (total = effective_timeout ) if effective_timeout else None
64
- )
55
+ self ._close_session = False # Never close a provided session
56
+ # Extract timeout from session
57
+ self ._client_timeout = session ._timeout
65
58
66
59
async def __aenter__ (self ) -> AIOHTTPTransport :
67
60
"""Enter async context."""
68
- if self .session is None :
69
- connector = TCPConnector (ssl = self .verify_ssl )
70
- timeout = ClientTimeout (total = self .timeout )
71
- self .session = ClientSession (connector = connector , timeout = timeout )
72
61
return self
73
62
74
63
async def __aexit__ (self , exc_type : Any , exc_val : Any , exc_tb : Any ) -> None :
75
64
"""Exit async context."""
76
- if self ._close_session and self .session :
77
- await self .session .close ()
78
- self .session = None
79
65
80
66
async def aclose (self ) -> None :
81
67
"""Close the transport session."""
82
- if self .session :
83
- await self .session .close ()
84
68
85
69
def _aiohttp_to_httpx_response (
86
70
self , aiohttp_response : ClientResponse , content : bytes
@@ -149,9 +133,6 @@ async def post(
149
133
The httpx response object
150
134
151
135
"""
152
- if self .session is None :
153
- async with self :
154
- return await self ._post (address , message , headers )
155
136
return await self ._post (address , message , headers )
156
137
157
138
async def _post (
@@ -235,9 +216,6 @@ async def get(
235
216
A Response object compatible with zeep
236
217
237
218
"""
238
- if self .session is None :
239
- async with self :
240
- return await self ._get (address , params , headers )
241
219
return await self ._get (address , params , headers )
242
220
243
221
async def _get (
@@ -253,20 +231,13 @@ async def _get(
253
231
headers = headers or {}
254
232
headers .setdefault ("User-Agent" , f"Zeep/{ get_version ()} " )
255
233
256
- # Determine timeout
257
- timeout = self .operation_timeout or self .timeout
258
- if timeout :
259
- client_timeout = ClientTimeout (total = timeout )
260
- else :
261
- client_timeout = None
262
-
263
234
try :
264
235
response = await self .session .get (
265
236
address ,
266
237
params = params ,
267
238
headers = headers ,
268
239
proxy = self .proxy ,
269
- timeout = client_timeout ,
240
+ timeout = self . _client_timeout ,
270
241
)
271
242
response .raise_for_status ()
272
243
0 commit comments