Skip to content

Commit 5b0bade

Browse files
committed
niquests vs requeests - simplify the revert
1 parent 507b080 commit 5b0bade

4 files changed

Lines changed: 32 additions & 18 deletions

File tree

caldav/davclient.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@
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+
1826
from lxml import etree
1927
from lxml.etree import _Element
20-
from niquests.auth import AuthBase
21-
from niquests.models import Response
22-
from niquests.structures import CaseInsensitiveDict
2328

2429
from .elements.base import BaseElement
2530
from caldav import __version__
@@ -489,7 +494,10 @@ def __init__(
489494

490495
## Deprecation TODO: give a warning, user should use get_davclient or auto_calendar instead
491496

492-
self.session = niquests.Session(multiplexed=True)
497+
try:
498+
self.session = requests.Session(multiplexed=True)
499+
except TypeError:
500+
self.session = requests.Session()
493501

494502
log.debug("url: " + str(url))
495503
self.url = URL.objectify(url)
@@ -839,9 +847,9 @@ def build_auth_object(self, auth_types: Optional[List[str]] = None):
839847
)
840848

841849
if auth_type == "digest":
842-
self.auth = niquests.auth.HTTPDigestAuth(self.username, self.password)
850+
self.auth = requests.auth.HTTPDigestAuth(self.username, self.password)
843851
elif auth_type == "basic":
844-
self.auth = niquests.auth.HTTPBasicAuth(self.username, self.password)
852+
self.auth = requests.auth.HTTPBasicAuth(self.username, self.password)
845853
elif auth_type == "bearer":
846854
self.auth = HTTPBearerAuth(self.password)
847855

@@ -977,8 +985,8 @@ def request(
977985

978986
# this is an error condition that should be raised to the application
979987
if (
980-
response.status == niquests.codes.forbidden
981-
or response.status == niquests.codes.unauthorized
988+
response.status == requests.codes.forbidden
989+
or response.status == requests.codes.unauthorized
982990
):
983991
try:
984992
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)