Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions caldav/davclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@
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 requests.auth import AuthBase
from requests.models import Response
from requests.structures import CaseInsensitiveDict

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__
Expand Down Expand Up @@ -489,7 +497,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)
Expand Down Expand Up @@ -839,9 +850,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)

Expand Down Expand Up @@ -977,8 +988,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
Expand Down
5 changes: 4 additions & 1 deletion caldav/requests.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
9 changes: 6 additions & 3 deletions tests/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

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 @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down