From 5b0bade5887bdad02fb440febf440441096ece5c Mon Sep 17 00:00:00 2001 From: Tobias Brox Date: Sat, 19 Jul 2025 13:51:20 +0200 Subject: [PATCH 1/2] niquests vs requeests - simplify the revert --- caldav/davclient.py | 26 +++++++++++++++++--------- caldav/requests.py | 5 ++++- tests/conf.py | 9 ++++++--- tests/test_caldav_unit.py | 10 +++++----- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/caldav/davclient.py b/caldav/davclient.py index a11cce3c..b14d2b97 100644 --- a/caldav/davclient.py +++ b/caldav/davclient.py @@ -14,12 +14,17 @@ from typing import Union from urllib.parse import unquote -import niquests + +try: + import niquests as requests + from niquests.auth import AuthBase + from niquests.models import Response + from niquests.structures import CaseInsensitiveDict +except ImportError: + import requests + from lxml import etree from lxml.etree import _Element -from niquests.auth import AuthBase -from niquests.models import Response -from niquests.structures import CaseInsensitiveDict from .elements.base import BaseElement from caldav import __version__ @@ -489,7 +494,10 @@ def __init__( ## Deprecation TODO: give a warning, user should use get_davclient or auto_calendar instead - self.session = niquests.Session(multiplexed=True) + try: + self.session = requests.Session(multiplexed=True) + except TypeError: + self.session = requests.Session() log.debug("url: " + str(url)) self.url = URL.objectify(url) @@ -839,9 +847,9 @@ def build_auth_object(self, auth_types: Optional[List[str]] = None): ) if auth_type == "digest": - self.auth = niquests.auth.HTTPDigestAuth(self.username, self.password) + self.auth = requests.auth.HTTPDigestAuth(self.username, self.password) elif auth_type == "basic": - self.auth = niquests.auth.HTTPBasicAuth(self.username, self.password) + self.auth = requests.auth.HTTPBasicAuth(self.username, self.password) elif auth_type == "bearer": self.auth = HTTPBearerAuth(self.password) @@ -977,8 +985,8 @@ def request( # this is an error condition that should be raised to the application if ( - response.status == niquests.codes.forbidden - or response.status == niquests.codes.unauthorized + response.status == requests.codes.forbidden + or response.status == requests.codes.unauthorized ): try: reason = response.reason diff --git a/caldav/requests.py b/caldav/requests.py index 265ec408..23b4adf6 100644 --- a/caldav/requests.py +++ b/caldav/requests.py @@ -1,4 +1,7 @@ -from niquests.auth import AuthBase +try: + from niquests.auth import AuthBase +except ImportError: + from requests.auth import AuthBase class HTTPBearerAuth(AuthBase): diff --git a/tests/conf.py b/tests/conf.py index 4f226e1a..637960fa 100644 --- a/tests/conf.py +++ b/tests/conf.py @@ -8,7 +8,10 @@ import threading import time -import niquests +try: + import niquests as requests +except ImportError: + import requests from caldav import compatibility_hints from caldav.compatibility_hints import FeatureSet @@ -125,7 +128,7 @@ def setup_radicale(self): i = 0 while True: try: - niquests.get(str(self.url)) + requests.get(str(self.url)) break except: time.sleep(0.05) @@ -205,7 +208,7 @@ def teardown_xandikos(self): ## ... but the thread may be stuck waiting for a request ... def silly_request(): try: - niquests.get(str(self.url)) + requests.get(str(self.url)) except: pass diff --git a/tests/test_caldav_unit.py b/tests/test_caldav_unit.py index 746ad471..dd636ed5 100755 --- a/tests/test_caldav_unit.py +++ b/tests/test_caldav_unit.py @@ -380,7 +380,7 @@ class TestCalDAV: dependencies, without accessing any caldav server) """ - @mock.patch("caldav.davclient.niquests.Session.request") + @mock.patch("caldav.davclient.requests.Session.request") def testRequestNonAscii(self, mocked): """ ref https://github.com/python-caldav/caldav/issues/83 @@ -437,7 +437,7 @@ def testLoadByMultiGet404(self): with pytest.raises(error.NotFoundError): object.load_by_multiget() - @mock.patch("caldav.davclient.niquests.Session.request") + @mock.patch("caldav.davclient.requests.Session.request") def testRequestCustomHeaders(self, mocked): """ ref https://github.com/python-caldav/caldav/issues/285 @@ -455,7 +455,7 @@ def testRequestCustomHeaders(self, mocked): ## User-Agent would be overwritten by some boring default in earlier versions assert client.headers["User-Agent"] == "MyCaldavApp" - @mock.patch("caldav.davclient.niquests.Session.request") + @mock.patch("caldav.davclient.requests.Session.request") def testRequestUserAgent(self, mocked): """ ref https://github.com/python-caldav/caldav/issues/391 @@ -469,7 +469,7 @@ def testRequestUserAgent(self, mocked): assert client.headers["Content-Type"] == "text/xml" assert client.headers["User-Agent"].startswith("python-caldav/") - @mock.patch("caldav.davclient.niquests.Session.request") + @mock.patch("caldav.davclient.requests.Session.request") def testEmptyXMLNoContentLength(self, mocked): """ ref https://github.com/python-caldav/caldav/issues/213 @@ -479,7 +479,7 @@ def testEmptyXMLNoContentLength(self, mocked): mocked().content = "" client = DAVClient(url="AsdfasDF").request("/") - @mock.patch("caldav.davclient.niquests.Session.request") + @mock.patch("caldav.davclient.requests.Session.request") def testNonValidXMLNoContentLength(self, mocked): """ If XML is expected but nonvalid XML is given, an error should be raised From f37f1e6a505b184b28e478c46048b606719d2f03 Mon Sep 17 00:00:00 2001 From: Tobias Brox Date: Sun, 20 Jul 2025 21:23:29 +0200 Subject: [PATCH 2/2] bugfix --- caldav/davclient.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/caldav/davclient.py b/caldav/davclient.py index b14d2b97..7224f2fa 100644 --- a/caldav/davclient.py +++ b/caldav/davclient.py @@ -22,6 +22,9 @@ from niquests.structures import CaseInsensitiveDict except ImportError: import requests + from requests.auth import AuthBase + from requests.models import Response + from requests.structures import CaseInsensitiveDict from lxml import etree from lxml.etree import _Element