Skip to content

Commit 3343339

Browse files
committed
remove unused code
1 parent 4ae57f1 commit 3343339

File tree

3 files changed

+110
-104
lines changed

3 files changed

+110
-104
lines changed

onvif/client.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,18 @@ def __init__(
260260
sock_read=read_timeout or _READ_TIMEOUT,
261261
),
262262
)
263-
self.transport = AsyncTransportProtocolErrorHandler(
264-
session=session,
265-
timeout=_DEFAULT_TIMEOUT,
266-
operation_timeout=read_timeout or _READ_TIMEOUT,
267-
verify_ssl=False,
263+
self.transport = (
264+
AsyncTransportProtocolErrorHandler(
265+
session=session,
266+
verify_ssl=False,
267+
)
268+
if no_cache
269+
else AIOHTTPTransport(
270+
session=session,
271+
verify_ssl=False,
272+
cache=SqliteCache(),
273+
)
268274
)
269-
if not no_cache:
270-
self.transport.cache = SqliteCache()
271275
self.document: Document | None = None
272276
self.zeep_client_authless: ZeepAsyncClient | None = None
273277
self.ws_client_authless: AsyncServiceProxy | None = None

onvif/zeep_aiohttp.py

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
import logging
77
from typing import TYPE_CHECKING, Any
88

9+
from zeep.cache import SqliteCache
910
from zeep.transports import Transport
1011
from zeep.utils import get_version
1112
from zeep.wsdl.utils import etree_to_string
1213

1314
import aiohttp
1415
import httpx
15-
from aiohttp import ClientResponse, ClientSession, ClientTimeout, TCPConnector
16+
from aiohttp import ClientResponse, ClientSession
1617
from requests import Response
1718

1819
if TYPE_CHECKING:
@@ -26,61 +27,44 @@ class AIOHTTPTransport(Transport):
2627

2728
def __init__(
2829
self,
29-
session: ClientSession | None = None,
30-
timeout: float = 300,
31-
operation_timeout: float | None = None,
30+
session: ClientSession,
3231
verify_ssl: bool = True,
3332
proxy: str | None = None,
33+
cache: SqliteCache | None = None,
3434
) -> None:
3535
"""
3636
Initialize the transport.
3737
3838
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.
4241
verify_ssl: Whether to verify SSL certificates
4342
proxy: Proxy URL to use
4443
4544
"""
4645
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,
5049
)
5150

5251
# Override parent's session with aiohttp session
5352
self.session = session
54-
self.timeout = timeout
55-
self.operation_timeout = operation_timeout
5653
self.verify_ssl = verify_ssl
5754
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
6558

6659
async def __aenter__(self) -> AIOHTTPTransport:
6760
"""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)
7261
return self
7362

7463
async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
7564
"""Exit async context."""
76-
if self._close_session and self.session:
77-
await self.session.close()
78-
self.session = None
7965

8066
async def aclose(self) -> None:
8167
"""Close the transport session."""
82-
if self.session:
83-
await self.session.close()
8468

8569
def _aiohttp_to_httpx_response(
8670
self, aiohttp_response: ClientResponse, content: bytes
@@ -149,9 +133,6 @@ async def post(
149133
The httpx response object
150134
151135
"""
152-
if self.session is None:
153-
async with self:
154-
return await self._post(address, message, headers)
155136
return await self._post(address, message, headers)
156137

157138
async def _post(
@@ -235,9 +216,6 @@ async def get(
235216
A Response object compatible with zeep
236217
237218
"""
238-
if self.session is None:
239-
async with self:
240-
return await self._get(address, params, headers)
241219
return await self._get(address, params, headers)
242220

243221
async def _get(
@@ -253,20 +231,13 @@ async def _get(
253231
headers = headers or {}
254232
headers.setdefault("User-Agent", f"Zeep/{get_version()}")
255233

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-
263234
try:
264235
response = await self.session.get(
265236
address,
266237
params=params,
267238
headers=headers,
268239
proxy=self.proxy,
269-
timeout=client_timeout,
240+
timeout=self._client_timeout,
270241
)
271242
response.raise_for_status()
272243

0 commit comments

Comments
 (0)