From eb2e42924a56638e761b0ea715d7d98056152df2 Mon Sep 17 00:00:00 2001 From: ArtemIsmagilov Date: Thu, 21 Nov 2024 13:14:04 +0400 Subject: [PATCH] use niquests --- caldav/davclient.py | 30 +++++++++++++++--------------- caldav/requests.py | 2 +- pyproject.toml | 2 +- tests/conf.py | 6 +++--- tests/test_caldav.py | 2 -- tests/test_caldav_unit.py | 10 +++++----- 6 files changed, 25 insertions(+), 27 deletions(-) diff --git a/caldav/davclient.py b/caldav/davclient.py index 7d6b475e..4da3e6b1 100644 --- a/caldav/davclient.py +++ b/caldav/davclient.py @@ -12,12 +12,12 @@ from typing import Union from urllib.parse import unquote -import requests +import niquests from lxml import etree from lxml.etree import _Element -from requests.auth import AuthBase -from requests.models import Response -from requests.structures import CaseInsensitiveDict +from niquests.auth import AuthBase +from niquests.models import Response +from niquests.structures import CaseInsensitiveDict from .elements.base import BaseElement from caldav import __version__ @@ -357,7 +357,7 @@ def expand_simple_props( class DAVClient: """ - Basic client for webdav, uses the requests lib; gives access to + Basic client for webdav, uses the niquests lib; gives access to low-level operations towards the caldav server. Unless you have special needs, you should probably care most about @@ -387,18 +387,18 @@ def __init__( * url: A fully qualified url: `scheme://user:pass@hostname:port` * proxy: A string defining a proxy server: `hostname:port` * username and password should be passed as arguments or in the URL - * auth, timeout and ssl_verify_cert are passed to requests.request. + * auth, timeout and ssl_verify_cert are passed to niquests.request. * ssl_verify_cert can be the path of a CA-bundle or False. * huge_tree: boolean, enable XMLParser huge_tree to handle big events, beware of security issues, see : https://lxml.de/api/lxml.etree.XMLParser-class.html - The requests library will honor a .netrc-file, if such a file exists + The niquests library will honor a .netrc-file, if such a file exists username and password may be omitted. Known bug: .netrc is honored even if a username/password is given, ref https://github.com/python-caldav/caldav/issues/206 """ headers = headers or {} - self.session = requests.Session() + self.session = niquests.Session(multiplexed=True) log.debug("url: " + str(url)) self.url = URL.objectify(url) @@ -406,7 +406,7 @@ def __init__( # Prepare proxy info if proxy is not None: _proxy = proxy - # requests library expects the proxy url to have a scheme + # niquests library expects the proxy url to have a scheme if "://" not in proxy: _proxy = self.url.scheme + "://" + proxy @@ -700,9 +700,9 @@ def request( auth_types = self.extract_auth_types(r.headers["WWW-Authenticate"]) if self.password and self.username and "digest" in auth_types: - self.auth = requests.auth.HTTPDigestAuth(self.username, self.password) + self.auth = niquests.auth.HTTPDigestAuth(self.username, self.password) elif self.password and self.username and "basic" in auth_types: - self.auth = requests.auth.HTTPBasicAuth(self.username, self.password) + self.auth = niquests.auth.HTTPBasicAuth(self.username, self.password) elif self.password and "bearer" in auth_types: self.auth = HTTPBearerAuth(self.password) else: @@ -732,11 +732,11 @@ def request( auth_types = self.extract_auth_types(r.headers["WWW-Authenticate"]) if self.password and self.username and "digest" in auth_types: - self.auth = requests.auth.HTTPDigestAuth( + self.auth = niquests.auth.HTTPDigestAuth( self.username, self.password.decode() ) elif self.password and self.username and "basic" in auth_types: - self.auth = requests.auth.HTTPBasicAuth( + self.auth = niquests.auth.HTTPBasicAuth( self.username, self.password.decode() ) elif self.password and "bearer" in auth_types: @@ -748,8 +748,8 @@ def request( # this is an error condition that should be raised to the application if ( - response.status == requests.codes.forbidden - or response.status == requests.codes.unauthorized + response.status == niquests.codes.forbidden + or response.status == niquests.codes.unauthorized ): try: reason = response.reason diff --git a/caldav/requests.py b/caldav/requests.py index 0d0c330c..265ec408 100644 --- a/caldav/requests.py +++ b/caldav/requests.py @@ -1,4 +1,4 @@ -from requests.auth import AuthBase +from niquests.auth import AuthBase class HTTPBearerAuth(AuthBase): diff --git a/pyproject.toml b/pyproject.toml index 6df936dc..2a933742 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,7 +30,7 @@ urls = { Homepage = "https://github.com/python-caldav/caldav" } dependencies = [ "vobject", "lxml", - "requests", + "niquests", "recurring-ical-events>=2.0.0", "typing_extensions;python_version<'3.11'", ## It's a mess - newer versions of xandikos, used for testing, does not support python 3.8 anymore. icalendar 6.0.0 is not compatible with elder versions of xandikos. It's no problem with python 3.7, as icalender 6.0.0 does not support python 3.7. diff --git a/tests/conf.py b/tests/conf.py index 14e49262..b777b33e 100644 --- a/tests/conf.py +++ b/tests/conf.py @@ -8,7 +8,7 @@ import threading import time -import requests +import niquests from . import compatibility_issues from caldav.davclient import DAVClient @@ -117,7 +117,7 @@ def setup_radicale(self): i = 0 while True: try: - requests.get(self.url) + niquests.get(self.url) break except: time.sleep(0.05) @@ -197,7 +197,7 @@ def teardown_xandikos(self): ## ... but the thread may be stuck waiting for a request ... def silly_request(): try: - requests.get(self.url) + niquests.get(self.url) except: pass diff --git a/tests/test_caldav.py b/tests/test_caldav.py index 829345f7..05314bf9 100644 --- a/tests/test_caldav.py +++ b/tests/test_caldav.py @@ -24,7 +24,6 @@ import icalendar import pytest import vobject -from requests.packages import urllib3 from . import compatibility_issues from .conf import caldav_servers @@ -60,7 +59,6 @@ log = logging.getLogger("caldav") -urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) ev1 = """BEGIN:VCALENDAR VERSION:2.0 diff --git a/tests/test_caldav_unit.py b/tests/test_caldav_unit.py index d7957566..cf3fbdd5 100644 --- a/tests/test_caldav_unit.py +++ b/tests/test_caldav_unit.py @@ -251,7 +251,7 @@ class TestCalDAV: dependencies, without accessing any caldav server) """ - @mock.patch("caldav.davclient.requests.Session.request") + @mock.patch("caldav.davclient.niquests.Session.request") def testRequestNonAscii(self, mocked): """ ref https://github.com/python-caldav/caldav/issues/83 @@ -272,7 +272,7 @@ def testRequestNonAscii(self, mocked): assert response.status == 200 assert response.tree is None - @mock.patch("caldav.davclient.requests.Session.request") + @mock.patch("caldav.davclient.niquests.Session.request") def testRequestCustomHeaders(self, mocked): """ ref https://github.com/python-caldav/caldav/issues/285 @@ -290,7 +290,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.requests.Session.request") + @mock.patch("caldav.davclient.niquests.Session.request") def testRequestUserAgent(self, mocked): """ ref https://github.com/python-caldav/caldav/issues/391 @@ -304,7 +304,7 @@ def testRequestUserAgent(self, mocked): assert client.headers["Content-Type"] == "text/xml" assert client.headers["User-Agent"].startswith("python-caldav/") - @mock.patch("caldav.davclient.requests.Session.request") + @mock.patch("caldav.davclient.niquests.Session.request") def testEmptyXMLNoContentLength(self, mocked): """ ref https://github.com/python-caldav/caldav/issues/213 @@ -314,7 +314,7 @@ def testEmptyXMLNoContentLength(self, mocked): mocked().content = "" client = DAVClient(url="AsdfasDF").request("/") - @mock.patch("caldav.davclient.requests.Session.request") + @mock.patch("caldav.davclient.niquests.Session.request") def testNonValidXMLNoContentLength(self, mocked): """ If XML is expected but nonvalid XML is given, an error should be raised