punkreq 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
punkreq/__init__.py ADDED
@@ -0,0 +1,84 @@
1
+ from ._auth import Auth, BasicAuth, BearerAuth
2
+ from ._client import USE_CLIENT_DEFAULT, ResponseHandle, UseClientDefault
3
+ from ._config import Limits, Proxy, Timeout
4
+ from ._content import AsyncByteStream, ByteStream
5
+ from ._cookies import Cookies
6
+ from ._exceptions import (
7
+ CloseError,
8
+ ConnectError,
9
+ ConnectTimeout,
10
+ CookieConflict,
11
+ DecodingError,
12
+ HTTPError,
13
+ HTTPStatusError,
14
+ InvalidURL,
15
+ LocalProtocolError,
16
+ NetworkError,
17
+ PoolTimeout,
18
+ ProtocolError,
19
+ ProxyError,
20
+ ReadError,
21
+ ReadTimeout,
22
+ RemoteProtocolError,
23
+ RequestError,
24
+ RequestNotRead,
25
+ StreamClosed,
26
+ StreamConsumed,
27
+ StreamError,
28
+ TimeoutException,
29
+ TooManyRedirects,
30
+ TransportError,
31
+ UnsupportedProtocol,
32
+ WriteError,
33
+ )
34
+ from ._headers import Headers
35
+ from ._models import Request, Response
36
+ from ._urls import URL, QueryParams
37
+ from ._version import __version__ as __version__
38
+
39
+
40
+ __all__ = [
41
+ "URL",
42
+ "USE_CLIENT_DEFAULT",
43
+ "AsyncByteStream",
44
+ "Auth",
45
+ "BasicAuth",
46
+ "BearerAuth",
47
+ "ByteStream",
48
+ "CloseError",
49
+ "ConnectError",
50
+ "ConnectTimeout",
51
+ "CookieConflict",
52
+ "Cookies",
53
+ "DecodingError",
54
+ "HTTPError",
55
+ "HTTPStatusError",
56
+ "Headers",
57
+ "InvalidURL",
58
+ "Limits",
59
+ "LocalProtocolError",
60
+ "NetworkError",
61
+ "PoolTimeout",
62
+ "ProtocolError",
63
+ "Proxy",
64
+ "ProxyError",
65
+ "QueryParams",
66
+ "ReadError",
67
+ "ReadTimeout",
68
+ "RemoteProtocolError",
69
+ "Request",
70
+ "RequestError",
71
+ "RequestNotRead",
72
+ "Response",
73
+ "ResponseHandle",
74
+ "StreamClosed",
75
+ "StreamConsumed",
76
+ "StreamError",
77
+ "Timeout",
78
+ "TimeoutException",
79
+ "TooManyRedirects",
80
+ "TransportError",
81
+ "UnsupportedProtocol",
82
+ "UseClientDefault",
83
+ "WriteError",
84
+ ]
punkreq/_auth.py ADDED
@@ -0,0 +1,43 @@
1
+ from __future__ import annotations
2
+
3
+ import base64
4
+ import typing
5
+
6
+ from ._models import Request
7
+
8
+
9
+ __all__ = ["Auth", "BasicAuth", "BearerAuth"]
10
+
11
+ AuthTypes = typing.Union["Auth", typing.Tuple[str, str], None]
12
+
13
+
14
+ class Auth:
15
+ """Base class: override `apply` to mutate the outgoing request."""
16
+
17
+ def apply(self, request: Request) -> None:
18
+ raise NotImplementedError()
19
+
20
+
21
+ class BasicAuth(Auth):
22
+ def __init__(self, username: str, password: str = "") -> None:
23
+ token = base64.b64encode(f"{username}:{password}".encode()).decode("ascii")
24
+ self._header = f"Basic {token}"
25
+
26
+ def apply(self, request: Request) -> None:
27
+ request.headers["authorization"] = self._header
28
+
29
+
30
+ class BearerAuth(Auth):
31
+ def __init__(self, token: str) -> None:
32
+ self._header = f"Bearer {token}"
33
+
34
+ def apply(self, request: Request) -> None:
35
+ request.headers["authorization"] = self._header
36
+
37
+
38
+ def coerce_auth(auth: AuthTypes) -> Auth | None:
39
+ if auth is None or isinstance(auth, Auth):
40
+ return auth
41
+ if isinstance(auth, tuple) and len(auth) == 2:
42
+ return BasicAuth(auth[0], auth[1])
43
+ raise TypeError(f"Invalid 'auth' argument: {auth!r}")