Skip to content

Commit

Permalink
use niquests
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemIsmagilov committed Nov 20, 2024
1 parent c5899b5 commit e9bcd89
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 56 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
60 changes: 30 additions & 30 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,44 @@ description = "CalDAV (RFC4791) client library"
keywords = []
readme = "README.md"
classifiers = [
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"License :: OSI Approved :: GNU General Public License (GPL)",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: Office/Business :: Scheduling",
"Topic :: Software Development :: Libraries :: Python Modules",
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"License :: OSI Approved :: GNU General Public License (GPL)",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: Office/Business :: Scheduling",
"Topic :: Software Development :: Libraries :: Python Modules",
]
urls = { Homepage = "https://github.com/python-caldav/caldav" }
dependencies = [
"vobject",
"lxml",
"requests",
"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.
"icalendar<6.0.0;python_version=='3.8'",
"icalendar;python_version!='3.8'",
"vobject",
"lxml",
"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.
"icalendar<6.0.0;python_version=='3.8'",
"icalendar;python_version!='3.8'",
]
dynamic = ["version"]

[project.optional-dependencies]
test = [
"pytest",
"coverage",
"sphinx",
"backports.zoneinfo;python_version<'3.9'",
"tzlocal",
"xandikos==0.2.7;python_version<'3.9'",
"dulwich==0.20.50;python_version<'3.9'",
"xandikos;python_version>='3.9'",
"pytest",
"coverage",
"sphinx",
"backports.zoneinfo;python_version<'3.9'",
"tzlocal",
"xandikos==0.2.7;python_version<'3.9'",
"dulwich==0.20.50;python_version<'3.9'",
"xandikos;python_version>='3.9'",
]

[tool.setuptools_scm]
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 e9bcd89

Please sign in to comment.