Skip to content

Commit

Permalink
🔥 Remove niquests.session function alias (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ousret authored Sep 19, 2023
1 parent acd0081 commit fb071ee
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 35 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Release History
e.g. function `delete` no-longer accept `json`, or `files` arguments. as per RFCs specifications. You can still override this behavior through the `request` function.
- Mixin classes `RequestEncodingMixin`, and `RequestHooksMixin` due to OOP violations. Now deported directly into child classes.
- Function `unicode_is_ascii` as it is part of the stable `str` stdlib on Python 3 or greater.
- Alias function `session` for `Session` context manager that was kept for BC reasons since the v1.

**Changed**
- Calling the method `json` from `Response` when no encoding was provided no longer relies on internal encoding inference.
Expand Down
3 changes: 1 addition & 2 deletions src/niquests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def _check_cryptography(cryptography_version) -> None:
URLRequired,
)
from .models import PreparedRequest, Request, Response
from .sessions import Session, session
from .sessions import Session
from .status_codes import codes

logging.getLogger(__name__).addHandler(NullHandler())
Expand Down Expand Up @@ -194,6 +194,5 @@ def _check_cryptography(cryptography_version) -> None:
"Request",
"Response",
"Session",
"session",
"codes",
)
15 changes: 0 additions & 15 deletions src/niquests/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1091,18 +1091,3 @@ def rebuild_method(self, prepared_request: PreparedRequest, response: Response):
method = "GET"

prepared_request.method = method


def session():
"""
Returns a :class:`Session` for context-management.
.. deprecated:: 1.0.0
This method has been deprecated since version 1.0.0 and is only kept for
backwards compatibility. New code should use :class:`~requests.sessions.Session`
to create a session. This may be removed at a future date.
:rtype: Session
"""
return Session()
36 changes: 18 additions & 18 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ class TestRequests:
digest_auth_algo = ("MD5", "SHA-256", "SHA-512")

def test_entry_points(self):
niquests.session
niquests.session().get
niquests.session().head
niquests.Session
niquests.Session().get
niquests.Session().head
niquests.get
niquests.head
niquests.put
Expand Down Expand Up @@ -241,7 +241,7 @@ def test_HTTP_302_TOO_MANY_REDIRECTS(self, httpbin):
pytest.fail("Expected redirect to raise TooManyRedirects but it did not")

def test_HTTP_302_TOO_MANY_REDIRECTS_WITH_PARAMS(self, httpbin):
s = niquests.session()
s = niquests.Session()
s.max_redirects = 5
try:
s.get(httpbin("relative-redirect", "50"))
Expand Down Expand Up @@ -363,19 +363,19 @@ def test_HTTP_200_OK_GET_WITH_MIXED_PARAMS(self, httpbin):
assert r.status_code == 200

def test_set_cookie_on_301(self, httpbin):
s = niquests.session()
s = niquests.Session()
url = httpbin("cookies/set?foo=bar")
s.get(url)
assert s.cookies["foo"] == "bar"

def test_cookie_sent_on_redirect(self, httpbin):
s = niquests.session()
s = niquests.Session()
r = s.get(httpbin("cookies/set?foo=bar"))
r = s.get(httpbin("redirect/1")) # redirects to httpbin('get')
assert "Cookie" in r.json()["headers"]

def test_cookie_removed_on_expire(self, httpbin):
s = niquests.session()
s = niquests.Session()
s.get(httpbin("cookies/set?foo=bar"))
assert s.cookies["foo"] == "bar"
s.get(
Expand All @@ -385,34 +385,34 @@ def test_cookie_removed_on_expire(self, httpbin):
assert "foo" not in s.cookies

def test_cookie_quote_wrapped(self, httpbin):
s = niquests.session()
s = niquests.Session()
s.get(httpbin('cookies/set?foo="bar:baz"'))
assert s.cookies["foo"] == '"bar:baz"'

def test_cookie_persists_via_api(self, httpbin):
s = niquests.session()
s = niquests.Session()
r = s.get(httpbin("redirect/1"), cookies={"foo": "bar"})
assert "foo" in r.request.headers["Cookie"]
assert "foo" in r.history[0].request.headers["Cookie"]

def test_request_cookie_overrides_session_cookie(self, httpbin):
s = niquests.session()
s = niquests.Session()
s.cookies["foo"] = "bar"
r = s.get(httpbin("cookies"), cookies={"foo": "baz"})
assert r.json()["cookies"]["foo"] == "baz"
# Session cookie should not be modified
assert s.cookies["foo"] == "bar"

def test_request_cookies_not_persisted(self, httpbin):
s = niquests.session()
s = niquests.Session()
s.get(httpbin("cookies"), cookies={"foo": "baz"})
# Sending a request with cookies should not add cookies to the session
assert not s.cookies

def test_generic_cookiejar_works(self, httpbin):
cj = cookielib.CookieJar()
cookiejar_from_dict({"foo": "bar"}, cj)
s = niquests.session()
s = niquests.Session()
s.cookies = cj
r = s.get(httpbin("cookies"))
# Make sure the cookie was sent
Expand All @@ -423,7 +423,7 @@ def test_generic_cookiejar_works(self, httpbin):
def test_param_cookiejar_works(self, httpbin):
cj = cookielib.CookieJar()
cookiejar_from_dict({"foo": "bar"}, cj)
s = niquests.session()
s = niquests.Session()
r = s.get(httpbin("cookies"), cookies=cj)
# Make sure the cookie was sent
assert r.json()["cookies"]["foo"] == "bar"
Expand Down Expand Up @@ -527,7 +527,7 @@ def test_BASICAUTH_TUPLE_HTTP_200_OK_GET(self, httpbin):
r = niquests.get(url)
assert r.status_code == 401

s = niquests.session()
s = niquests.Session()
s.auth = auth
r = s.get(url)
assert r.status_code == 200
Expand Down Expand Up @@ -681,7 +681,7 @@ def get_netrc_auth_mock(url):
r = niquests.get(url, auth=wrong_auth)
assert r.status_code == 401

s = niquests.session()
s = niquests.Session()

# Should use netrc and work.
r = s.get(url)
Expand All @@ -706,7 +706,7 @@ def test_DIGEST_HTTP_200_OK_GET(self, httpbin):
assert r.status_code == 401
print(r.headers["WWW-Authenticate"])

s = niquests.session()
s = niquests.Session()
s.auth = HTTPDigestAuth("user", "pass")
r = s.get(url)
assert r.status_code == 200
Expand Down Expand Up @@ -751,7 +751,7 @@ def test_DIGESTAUTH_WRONG_HTTP_401_GET(self, httpbin):
r = niquests.get(url)
assert r.status_code == 401

s = niquests.session()
s = niquests.Session()
s.auth = auth
r = s.get(url)
assert r.status_code == 401
Expand Down Expand Up @@ -2067,7 +2067,7 @@ def test_response_context_manager(self, httpbin):
assert response.raw.closed

def test_unconsumed_session_response_closes_connection(self, httpbin):
s = niquests.session()
s = niquests.Session()

with contextlib.closing(s.get(httpbin("stream/4"), stream=True)) as response:
pass
Expand Down

0 comments on commit fb071ee

Please sign in to comment.