Skip to content

Commit bfbf094

Browse files
committed
Migrate to using aiohttp
1 parent 04941e1 commit bfbf094

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

onvif/managers.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22

33
from __future__ import annotations
44

5-
from abc import abstractmethod
65
import asyncio
76
import datetime as dt
87
import logging
9-
from typing import TYPE_CHECKING, Any
8+
from abc import abstractmethod
109
from collections.abc import Callable
10+
from typing import TYPE_CHECKING, Any
1111

12-
import httpx
13-
from httpx import TransportError
1412
from zeep.exceptions import Fault, XMLParseError, XMLSyntaxError
1513
from zeep.loader import parse_xml
1614
from zeep.wsdl.bindings.soap import SoapOperation
1715

16+
import aiohttp
1817
from onvif.exceptions import ONVIFError
1918

2019
from .settings import DEFAULT_SETTINGS
@@ -27,8 +26,8 @@
2726

2827
_RENEWAL_PERCENTAGE = 0.8
2928

30-
SUBSCRIPTION_ERRORS = (Fault, asyncio.TimeoutError, TransportError)
31-
RENEW_ERRORS = (ONVIFError, httpx.RequestError, XMLParseError, *SUBSCRIPTION_ERRORS)
29+
SUBSCRIPTION_ERRORS = (Fault, asyncio.TimeoutError, aiohttp.ClientError)
30+
RENEW_ERRORS = (ONVIFError, aiohttp.ClientError, XMLParseError, *SUBSCRIPTION_ERRORS)
3231
SUBSCRIPTION_RESTART_INTERVAL_ON_ERROR = dt.timedelta(seconds=40)
3332

3433
# If the camera returns a subscription with a termination time that is less than
@@ -87,7 +86,8 @@ async def stop(self) -> None:
8786
await self._subscription.Unsubscribe()
8887

8988
async def shutdown(self) -> None:
90-
"""Shutdown the manager.
89+
"""
90+
Shutdown the manager.
9191
9292
This method is irreversible.
9393
"""
@@ -105,7 +105,7 @@ async def set_synchronization_point(self) -> float:
105105
"""Set the synchronization point."""
106106
try:
107107
await self._service.SetSynchronizationPoint()
108-
except (Fault, asyncio.TimeoutError, TransportError, TypeError):
108+
except (TimeoutError, Fault, aiohttp.ClientError, TypeError):
109109
logger.debug("%s: SetSynchronizationPoint failed", self._service.url)
110110

111111
def _cancel_renewals(self) -> None:
@@ -214,7 +214,8 @@ def __init__(
214214
super().__init__(device, interval, subscription_lost_callback)
215215

216216
async def _start(self) -> float:
217-
"""Start the notification processor.
217+
"""
218+
Start the notification processor.
218219
219220
Returns the next renewal call at time.
220221
"""
@@ -290,7 +291,8 @@ class PullPointManager(BaseManager):
290291
"""Manager for PullPoint."""
291292

292293
async def _start(self) -> float:
293-
"""Start the PullPoint manager.
294+
"""
295+
Start the PullPoint manager.
294296
295297
Returns the next renewal call at time.
296298
"""

onvif/wrappers.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
from __future__ import annotations
44

55
import asyncio
6-
from collections.abc import Awaitable
76
import logging
7+
from collections.abc import Awaitable, Callable
88
from typing import ParamSpec, TypeVar
9-
from collections.abc import Callable
109

11-
import httpx
10+
import aiohttp
1211

1312
from .const import BACKOFF_TIME, DEFAULT_ATTEMPTS
1413

@@ -19,14 +18,15 @@
1918

2019
def retry_connection_error(
2120
attempts: int = DEFAULT_ATTEMPTS,
22-
exception: httpx.HTTPError = httpx.RequestError,
21+
exception: type[Exception] = aiohttp.ClientError,
2322
) -> Callable[[Callable[P, Awaitable[T]]], Callable[P, Awaitable[T]]]:
2423
"""Define a wrapper to retry on connection error."""
2524

2625
def _decorator_retry_connection_error(
2726
func: Callable[P, Awaitable[T]],
2827
) -> Callable[P, Awaitable[T]]:
29-
"""Define a wrapper to retry on connection error.
28+
"""
29+
Define a wrapper to retry on connection error.
3030
3131
The remote server is allowed to disconnect us any time so
3232
we need to retry the operation.
@@ -40,11 +40,11 @@ async def _async_wrap_connection_error_retry( # type: ignore[return]
4040
return await func(*args, **kwargs)
4141
except exception as ex:
4242
#
43-
# We should only need to retry on RemoteProtocolError but some cameras
43+
# We should only need to retry on ServerDisconnectedError but some cameras
4444
# are flakey and sometimes do not respond to the Renew request so we
45-
# retry on RequestError as well.
45+
# retry on ClientError as well.
4646
#
47-
# For RemoteProtocolError:
47+
# For ServerDisconnectedError:
4848
# http://datatracker.ietf.org/doc/html/rfc2616#section-8.1.4 allows the server
4949
# to close the connection at any time, we treat this as a normal and try again
5050
# once since we do not want to declare the camera as not supporting PullPoint

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Package
2+
aiohttp==3.12.9
23
ciso8601==2.3.2
34
httpx==0.28.1
45
zeep[async]==4.3.1

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
version = open(version_path).read().strip()
1010

1111
requires = [
12+
"aiohttp>=3.12.9",
1213
"httpx>=0.19.0,<1.0.0",
1314
"zeep[async]>=4.2.1,<5.0.0",
1415
"ciso8601>=2.1.3",

0 commit comments

Comments
 (0)