Skip to content

Commit f08beec

Browse files
authored
niquests vs requeests - simplify the revert
The caldav library will now use niquest if it's installed, otherwise requests. Hence, the only change needed to remove the controversial niquest dependency is to edit `pyproject.toml`. As I've promised dual releases for the rest of the 2.x-series this also simplifies things for me. #542
1 parent 507b080 commit f08beec

4 files changed

Lines changed: 35 additions & 18 deletions

File tree

caldav/davclient.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@
1414
from typing import Union
1515
from urllib.parse import unquote
1616

17-
import niquests
17+
18+
try:
19+
import niquests as requests
20+
from niquests.auth import AuthBase
21+
from niquests.models import Response
22+
from niquests.structures import CaseInsensitiveDict
23+
except ImportError:
24+
import requests
25+
from requests.auth import AuthBase
26+
from requests.models import Response
27+
from requests.structures import CaseInsensitiveDict
28+
1829
from lxml import etree
1930
from lxml.etree import _Element
20-
from niquests.auth import AuthBase
21-
from niquests.models import Response
22-
from niquests.structures import CaseInsensitiveDict
2331

2432
from .elements.base import BaseElement
2533
from caldav import __version__
@@ -489,7 +497,10 @@ def __init__(
489497

490498
## Deprecation TODO: give a warning, user should use get_davclient or auto_calendar instead
491499

492-
self.session = niquests.Session(multiplexed=True)
500+
try:
501+
self.session = requests.Session(multiplexed=True)
502+
except TypeError:
503+
self.session = requests.Session()
493504

494505
log.debug("url: " + str(url))
495506
self.url = URL.objectify(url)
@@ -839,9 +850,9 @@ def build_auth_object(self, auth_types: Optional[List[str]] = None):
839850
)
840851

841852
if auth_type == "digest":
842-
self.auth = niquests.auth.HTTPDigestAuth(self.username, self.password)
853+
self.auth = requests.auth.HTTPDigestAuth(self.username, self.password)
843854
elif auth_type == "basic":
844-
self.auth = niquests.auth.HTTPBasicAuth(self.username, self.password)
855+
self.auth = requests.auth.HTTPBasicAuth(self.username, self.password)
845856
elif auth_type == "bearer":
846857
self.auth = HTTPBearerAuth(self.password)
847858

@@ -977,8 +988,8 @@ def request(
977988

978989
# this is an error condition that should be raised to the application
979990
if (
980-
response.status == niquests.codes.forbidden
981-
or response.status == niquests.codes.unauthorized
991+
response.status == requests.codes.forbidden
992+
or response.status == requests.codes.unauthorized
982993
):
983994
try:
984995
reason = response.reason

caldav/requests.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
from niquests.auth import AuthBase
1+
try:
2+
from niquests.auth import AuthBase
3+
except ImportError:
4+
from requests.auth import AuthBase
25

36

47
class HTTPBearerAuth(AuthBase):

tests/conf.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
import threading
99
import time
1010

11-
import niquests
11+
try:
12+
import niquests as requests
13+
except ImportError:
14+
import requests
1215

1316
from caldav import compatibility_hints
1417
from caldav.compatibility_hints import FeatureSet
@@ -125,7 +128,7 @@ def setup_radicale(self):
125128
i = 0
126129
while True:
127130
try:
128-
niquests.get(str(self.url))
131+
requests.get(str(self.url))
129132
break
130133
except:
131134
time.sleep(0.05)
@@ -205,7 +208,7 @@ def teardown_xandikos(self):
205208
## ... but the thread may be stuck waiting for a request ...
206209
def silly_request():
207210
try:
208-
niquests.get(str(self.url))
211+
requests.get(str(self.url))
209212
except:
210213
pass
211214

tests/test_caldav_unit.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ class TestCalDAV:
380380
dependencies, without accessing any caldav server)
381381
"""
382382

383-
@mock.patch("caldav.davclient.niquests.Session.request")
383+
@mock.patch("caldav.davclient.requests.Session.request")
384384
def testRequestNonAscii(self, mocked):
385385
"""
386386
ref https://github.com/python-caldav/caldav/issues/83
@@ -437,7 +437,7 @@ def testLoadByMultiGet404(self):
437437
with pytest.raises(error.NotFoundError):
438438
object.load_by_multiget()
439439

440-
@mock.patch("caldav.davclient.niquests.Session.request")
440+
@mock.patch("caldav.davclient.requests.Session.request")
441441
def testRequestCustomHeaders(self, mocked):
442442
"""
443443
ref https://github.com/python-caldav/caldav/issues/285
@@ -455,7 +455,7 @@ def testRequestCustomHeaders(self, mocked):
455455
## User-Agent would be overwritten by some boring default in earlier versions
456456
assert client.headers["User-Agent"] == "MyCaldavApp"
457457

458-
@mock.patch("caldav.davclient.niquests.Session.request")
458+
@mock.patch("caldav.davclient.requests.Session.request")
459459
def testRequestUserAgent(self, mocked):
460460
"""
461461
ref https://github.com/python-caldav/caldav/issues/391
@@ -469,7 +469,7 @@ def testRequestUserAgent(self, mocked):
469469
assert client.headers["Content-Type"] == "text/xml"
470470
assert client.headers["User-Agent"].startswith("python-caldav/")
471471

472-
@mock.patch("caldav.davclient.niquests.Session.request")
472+
@mock.patch("caldav.davclient.requests.Session.request")
473473
def testEmptyXMLNoContentLength(self, mocked):
474474
"""
475475
ref https://github.com/python-caldav/caldav/issues/213
@@ -479,7 +479,7 @@ def testEmptyXMLNoContentLength(self, mocked):
479479
mocked().content = ""
480480
client = DAVClient(url="AsdfasDF").request("/")
481481

482-
@mock.patch("caldav.davclient.niquests.Session.request")
482+
@mock.patch("caldav.davclient.requests.Session.request")
483483
def testNonValidXMLNoContentLength(self, mocked):
484484
"""
485485
If XML is expected but nonvalid XML is given, an error should be raised

0 commit comments

Comments
 (0)