Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HttpAuthenticationType to add callable #133

Merged
merged 2 commits into from
Jul 2, 2024
Merged

Conversation

MtkN1
Copy link

@MtkN1 MtkN1 commented Jun 26, 2024

Add a callable object to the HttpAuthenticationType type definition.

A type checking error occurs when a callable object is given as the auth argument.

image

Reproduction code
import niquests


def pizza_auth(request: niquests.PreparedRequest) -> niquests.PreparedRequest:
    if request.headers:
        request.headers["X-Pizza"] = "Token"
    return request


def test_callable_auth():
    r = niquests.get("https://httpbin.org/get", auth=pizza_auth)
    print(r.json()["headers"])


if __name__ == "__main__":
    test_callable_auth()

This is allowed at runtime.

def prepare_auth(self, auth: HttpAuthenticationType | None, url: str = "") -> None:
"""Prepares the given HTTP auth data."""
assert (
self.url is not None
), "Cannot invoke prepare_auth method with incomplete PreparedRequest"
# If no Auth is explicitly provided, extract it from the URL first.
if auth is None:
url_auth = get_auth_from_url(self.url)
auth = url_auth if any(url_auth) else None
if auth:
if isinstance(auth, tuple) and len(auth) == 2:
# special-case basic HTTP auth
auth = HTTPBasicAuth(*auth)
elif isinstance(auth, str):
auth = BearerTokenAuth(auth)
if not callable(auth):
raise ValueError(
"Unexpected non-callable authentication. Did you pass unsupported tuple to auth argument?"
)
if not asyncio.iscoroutinefunction(auth.__call__):
# Allow auth to make its changes.
r = auth(self)
# Update self to reflect the auth changes.
self.__dict__.update(r.__dict__)
# Recompute Content-Length
self.prepare_content_length(self.body)

@Ousret Ousret self-requested a review June 26, 2024 14:32
@Ousret Ousret added the typing static typing related (mypy or similar) label Jun 26, 2024
Copy link

@Ousret Ousret left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR itself bring a valid point. I had to make a change to avoid making a whole TypeAlias being a string. Some IDE / type checkers may not like it. It is most importantly a personal design choice.

Copy link
Author

@MtkN1 MtkN1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review and fix. I did not know that TypeAlias were allowed to be partial strings. I think it is better that way.

@Ousret Ousret merged commit 7cd3bcf into jawah:main Jul 2, 2024
31 checks passed
@MtkN1 MtkN1 deleted the fix-auth-type branch July 2, 2024 14:05
Ousret added a commit that referenced this pull request Jul 7, 2024
**Added**
- Official support for Python 3.13
- Support for asynchronous auth callables.
- Support for asynchronous bodies through `AsyncIterable` that yield either bytes or str.
- Support for purposely excluding a domain/port from connecting to QUIC/HTTP3 via the `quic_cache_layer` property of `Session`.
  In order to exclude `cloudflare.com` from HTTP3 auto-upgrade:
  ```python
  from niquests import Session

  s = Session()
  s.quic_cache_layer.exclude_domain("cloudflare.com")
  ```

**Fixed**
- auth argument not accepting a function according to static type checkers. (#133)
- RequestsCookieJar having a lock in `AsyncSession`. Its effect has been nullified to improve performances.

**Changed**
- urllib3-future lower bound version is raised to 2.8.902
@Ousret Ousret mentioned this pull request Jul 7, 2024
Ousret added a commit that referenced this pull request Jul 7, 2024
**Added**
- Official support for Python 3.13
- Support for asynchronous auth callables.
- Support for asynchronous bodies through `AsyncIterable` that yield either bytes or str.
- Support for purposely excluding a domain/port from connecting to QUIC/HTTP3 via the `quic_cache_layer` property of `Session`.
  In order to exclude `cloudflare.com` from HTTP3 auto-upgrade:
  ```python
  from niquests import Session

  s = Session()
  s.quic_cache_layer.exclude_domain("cloudflare.com")
  ```

**Fixed**
- auth argument not accepting a function according to static type checkers. (#133)
- RequestsCookieJar having a lock in `AsyncSession`. Its effect has been nullified to improve performances.

**Changed**
- urllib3-future lower bound version is raised to 2.8.902
Ousret added a commit that referenced this pull request Jul 7, 2024
**Added**
- Official support for Python 3.13
- Support for asynchronous auth callables.
- Support for asynchronous bodies through `AsyncIterable` that yield either bytes or str.
- Support for purposely excluding a domain/port from connecting to QUIC/HTTP3 via the `quic_cache_layer` property of `Session`.
  In order to exclude `cloudflare.com` from HTTP3 auto-upgrade:
  ```python
  from niquests import Session

  s = Session()
  s.quic_cache_layer.exclude_domain("cloudflare.com")
  ```

**Fixed**
- auth argument not accepting a function according to static type checkers. (#133)
- RequestsCookieJar having a lock in `AsyncSession`. Its effect has been nullified to improve performances.

**Changed**
- urllib3-future lower bound version is raised to 2.8.902
Ousret added a commit that referenced this pull request Jul 7, 2024
3.7.1 (2024-07-07)
------------------

**Added**
- Official support for Python 3.13
- Support for asynchronous auth callables.
- Support for asynchronous bodies through `AsyncIterable` that yield
either bytes or str.
- Support for purposely excluding a domain/port from connecting to
QUIC/HTTP3 via the `quic_cache_layer` property of `Session`.
  In order to exclude `cloudflare.com` from HTTP3 auto-upgrade:
  ```python
  from niquests import Session

  s = Session()
  s.quic_cache_layer.exclude_domain("cloudflare.com")
  ```

**Fixed**
- auth argument not accepting a function according to static type
checkers. (#133)
- RequestsCookieJar having a lock in `AsyncSession`. Its effect has been
nullified to improve performances.

**Changed**
- urllib3-future lower bound version is raised to 2.8.902

---------

Co-authored-by: MtkN1 <51289448+MtkN1@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typing static typing related (mypy or similar)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants