impit 0.9.0__cp314-cp314-manylinux_2_28_aarch64.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.
impit/__init__.py ADDED
@@ -0,0 +1,96 @@
1
+ from importlib import metadata
2
+ from typing import Literal
3
+
4
+ from .cookies import Cookies
5
+ from .impit import (
6
+ AsyncClient,
7
+ Client,
8
+ CloseError,
9
+ ConnectError,
10
+ ConnectTimeout,
11
+ CookieConflict,
12
+ DecodingError,
13
+ HTTPError,
14
+ HTTPStatusError,
15
+ InvalidURL,
16
+ LocalProtocolError,
17
+ NetworkError,
18
+ PoolTimeout,
19
+ ProtocolError,
20
+ ProxyError,
21
+ ReadError,
22
+ ReadTimeout,
23
+ RemoteProtocolError,
24
+ RequestError,
25
+ RequestNotRead,
26
+ Response,
27
+ ResponseNotRead,
28
+ StreamClosed,
29
+ StreamConsumed,
30
+ StreamError,
31
+ TimeoutException,
32
+ TooManyRedirects,
33
+ TransportError,
34
+ UnsupportedProtocol,
35
+ WriteError,
36
+ WriteTimeout,
37
+ delete,
38
+ get,
39
+ head,
40
+ options,
41
+ patch,
42
+ post,
43
+ put,
44
+ stream,
45
+ trace,
46
+ )
47
+
48
+ __version__ = metadata.version('impit')
49
+
50
+ __all__ = [
51
+ 'AsyncClient',
52
+ 'Browser',
53
+ 'Client',
54
+ 'CloseError',
55
+ 'ConnectError',
56
+ 'ConnectTimeout',
57
+ 'CookieConflict',
58
+ 'Cookies',
59
+ 'DecodingError',
60
+ 'HTTPError',
61
+ 'HTTPStatusError',
62
+ 'InvalidURL',
63
+ 'LocalProtocolError',
64
+ 'NetworkError',
65
+ 'PoolTimeout',
66
+ 'ProtocolError',
67
+ 'ProxyError',
68
+ 'ReadError',
69
+ 'ReadTimeout',
70
+ 'RemoteProtocolError',
71
+ 'RequestError',
72
+ 'RequestNotRead',
73
+ 'Response',
74
+ 'ResponseNotRead',
75
+ 'StreamClosed',
76
+ 'StreamConsumed',
77
+ 'StreamError',
78
+ 'TimeoutException',
79
+ 'TooManyRedirects',
80
+ 'TransportError',
81
+ 'UnsupportedProtocol',
82
+ 'WriteError',
83
+ 'WriteTimeout',
84
+ 'delete',
85
+ 'get',
86
+ 'head',
87
+ 'options',
88
+ 'patch',
89
+ 'post',
90
+ 'put',
91
+ 'stream',
92
+ 'trace',
93
+ ]
94
+
95
+
96
+ Browser = Literal['chrome', 'firefox']
impit/cookies.py ADDED
@@ -0,0 +1,177 @@
1
+ """Copyright © 2019, [Encode OSS Ltd](https://www.encode.io/).
2
+
3
+ All rights reserved.
4
+
5
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8
+
9
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10
+
11
+ * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
+ """
15
+ # ruff: noqa: SIM102, E501
16
+
17
+ ## The Cookies class below is a modification of the `httpx.Cookies` class, licensed under the BSD 3-Clause "New" License.
18
+
19
+ from __future__ import annotations
20
+
21
+ import typing
22
+ from http.cookiejar import Cookie, CookieJar
23
+
24
+ from .impit import CookieConflict
25
+
26
+ CookieTypes = typing.Union['Cookies', CookieJar, dict[str, str], list[tuple[str, str]]]
27
+
28
+
29
+ class Cookies(typing.MutableMapping[str, str]):
30
+ """HTTP Cookies, as a mutable mapping."""
31
+
32
+ def __init__(self, cookies: CookieTypes | None = None) -> None:
33
+ if cookies is None or isinstance(cookies, dict):
34
+ self.jar = CookieJar()
35
+ if isinstance(cookies, dict):
36
+ for key, value in cookies.items():
37
+ self.set(key, value)
38
+ elif isinstance(cookies, list):
39
+ self.jar = CookieJar()
40
+ for key, value in cookies:
41
+ self.set(key, value)
42
+ elif isinstance(cookies, Cookies):
43
+ self.jar = CookieJar()
44
+ for cookie in cookies.jar:
45
+ self.jar.set_cookie(cookie)
46
+ else:
47
+ self.jar = cookies
48
+
49
+ def set(self, name: str, value: str, domain: str = '', path: str = '/') -> None:
50
+ """Set a cookie value by name. May optionally include domain and path."""
51
+ kwargs = {
52
+ 'version': 0,
53
+ 'name': name,
54
+ 'value': value,
55
+ 'port': None,
56
+ 'port_specified': False,
57
+ 'domain': domain,
58
+ 'domain_specified': bool(domain),
59
+ 'domain_initial_dot': domain.startswith('.'),
60
+ 'path': path,
61
+ 'path_specified': bool(path),
62
+ 'secure': False,
63
+ 'expires': None,
64
+ 'discard': True,
65
+ 'comment': None,
66
+ 'comment_url': None,
67
+ 'rest': {'HttpOnly': None},
68
+ 'rfc2109': False,
69
+ }
70
+ cookie = Cookie(**kwargs) # type: ignore[arg-type]
71
+ self.jar.set_cookie(cookie)
72
+
73
+ def get( # type: ignore[override]
74
+ self,
75
+ name: str,
76
+ default: str | None = None,
77
+ domain: str | None = None,
78
+ path: str | None = None,
79
+ ) -> str | None:
80
+ """Get a cookie by name.
81
+
82
+ May optionally include domain and path in order to specify exactly which cookie to retrieve.
83
+ """
84
+ value = None
85
+ for cookie in self.jar:
86
+ if cookie.name == name:
87
+ if domain is None or cookie.domain == domain:
88
+ if path is None or cookie.path == path:
89
+ if value is not None:
90
+ message = f'Multiple cookies exist with name={name}'
91
+ raise CookieConflict(message)
92
+ value = cookie.value
93
+
94
+ if value is None:
95
+ return default
96
+ return value
97
+
98
+ def delete(
99
+ self,
100
+ name: str,
101
+ domain: str | None = None,
102
+ path: str | None = None,
103
+ ) -> None:
104
+ """Delete a cookie by name.
105
+
106
+ May optionally include domain and path in order to specify exactly which cookie to delete.
107
+ """
108
+ if domain is not None and path is not None:
109
+ return self.jar.clear(domain, path, name)
110
+
111
+ remove = [
112
+ cookie
113
+ for cookie in self.jar
114
+ if cookie.name == name
115
+ and (domain is None or cookie.domain == domain)
116
+ and (path is None or cookie.path == path)
117
+ ]
118
+
119
+ for cookie in remove:
120
+ self.jar.clear(cookie.domain, cookie.path, cookie.name)
121
+ return None
122
+
123
+ def clear(self, domain: str | None = None, path: str | None = None) -> None:
124
+ """Delete all cookies.
125
+
126
+ Optionally include a domain and path in order to only delete a subset of all the cookies.
127
+ """
128
+ args = []
129
+ if domain is not None:
130
+ args.append(domain)
131
+ if path is not None:
132
+ assert domain is not None # noqa: S101
133
+ args.append(path)
134
+ self.jar.clear(*args)
135
+
136
+ def update(self, cookies: CookieTypes | None = None) -> None: # type: ignore[override]
137
+ """Update the cookie jar with new cookies. Accepts various types."""
138
+ cookies = Cookies(cookies)
139
+ for cookie in cookies.jar:
140
+ self.jar.set_cookie(cookie)
141
+
142
+ def __setitem__(self, name: str, value: str) -> None:
143
+ """Set a cookie by name."""
144
+ return self.set(name, value)
145
+
146
+ def __getitem__(self, name: str) -> str:
147
+ """Get a cookie by name."""
148
+ value = self.get(name)
149
+ if value is None:
150
+ raise KeyError(name)
151
+ return value
152
+
153
+ def __delitem__(self, name: str) -> None:
154
+ """Delete a cookie by name."""
155
+ return self.delete(name)
156
+
157
+ def __len__(self) -> int:
158
+ """Return the number of cookies in the jar."""
159
+ return len(self.jar)
160
+
161
+ def __iter__(self) -> typing.Iterator[str]:
162
+ """Return an iterator over the names of the cookies in the jar."""
163
+ return (cookie.name for cookie in self.jar)
164
+
165
+ def __bool__(self) -> bool:
166
+ """Return True if there are any cookies in the jar."""
167
+ for _ in self.jar:
168
+ return True
169
+ return False
170
+
171
+ def __repr__(self) -> str:
172
+ """Return a string representation of the Cookies object."""
173
+ cookies_repr = ', '.join(
174
+ [f'<Cookie {cookie.name}={cookie.value} for {cookie.domain} />' for cookie in self.jar]
175
+ )
176
+
177
+ return f'<Cookies[{cookies_repr}]>'