Skip to content

Commit

Permalink
use niquests
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemIsmagilov authored and tobixen committed Nov 21, 2024
1 parent 2809c1a commit eb2e429
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 27 deletions.
30 changes: 15 additions & 15 deletions caldav/davclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -387,26 +387,26 @@ 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)
self.huge_tree = huge_tree
# 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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion caldav/requests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from requests.auth import AuthBase
from niquests.auth import AuthBase


class HTTPBearerAuth(AuthBase):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions tests/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import threading
import time

import requests
import niquests

from . import compatibility_issues
from caldav.davclient import DAVClient
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions tests/test_caldav.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import icalendar
import pytest
import vobject
from requests.packages import urllib3

from . import compatibility_issues
from .conf import caldav_servers
Expand Down Expand Up @@ -60,7 +59,6 @@

log = logging.getLogger("caldav")

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

ev1 = """BEGIN:VCALENDAR
VERSION:2.0
Expand Down
10 changes: 5 additions & 5 deletions tests/test_caldav_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit eb2e429

Please sign in to comment.