httpx-limiter 0.5.0__tar.gz → 0.6.0__tar.gz

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.
Files changed (18) hide show
  1. {httpx_limiter-0.5.0 → httpx_limiter-0.6.0}/PKG-INFO +25 -16
  2. {httpx_limiter-0.5.0 → httpx_limiter-0.6.0}/README.md +21 -12
  3. {httpx_limiter-0.5.0 → httpx_limiter-0.6.0}/pyproject.toml +61 -56
  4. {httpx_limiter-0.5.0 → httpx_limiter-0.6.0}/src/httpx_limiter/_version.py +2 -2
  5. {httpx_limiter-0.5.0 → httpx_limiter-0.6.0}/src/httpx_limiter/abstract_async_limiter.py +1 -2
  6. {httpx_limiter-0.5.0 → httpx_limiter-0.6.0}/src/httpx_limiter/async_multi_rate_limited_transport.py +1 -1
  7. {httpx_limiter-0.5.0 → httpx_limiter-0.6.0}/src/httpx_limiter/pyrate/pyrate_async_limiter.py +2 -27
  8. {httpx_limiter-0.5.0 → httpx_limiter-0.6.0}/.gitignore +0 -0
  9. {httpx_limiter-0.5.0 → httpx_limiter-0.6.0}/LICENSE +0 -0
  10. {httpx_limiter-0.5.0 → httpx_limiter-0.6.0}/src/httpx_limiter/__init__.py +0 -0
  11. {httpx_limiter-0.5.0 → httpx_limiter-0.6.0}/src/httpx_limiter/abstract_rate_limiter_repository.py +0 -0
  12. {httpx_limiter-0.5.0 → httpx_limiter-0.6.0}/src/httpx_limiter/aiolimiter/__init__.py +0 -0
  13. {httpx_limiter-0.5.0 → httpx_limiter-0.6.0}/src/httpx_limiter/aiolimiter/aiolimiter_async_limiter.py +0 -0
  14. {httpx_limiter-0.5.0 → httpx_limiter-0.6.0}/src/httpx_limiter/async_rate_limited_transport.py +0 -0
  15. {httpx_limiter-0.5.0 → httpx_limiter-0.6.0}/src/httpx_limiter/py.typed +0 -0
  16. {httpx_limiter-0.5.0 → httpx_limiter-0.6.0}/src/httpx_limiter/pyrate/__init__.py +0 -0
  17. {httpx_limiter-0.5.0 → httpx_limiter-0.6.0}/src/httpx_limiter/rate.py +0 -0
  18. {httpx_limiter-0.5.0 → httpx_limiter-0.6.0}/src/httpx_limiter/types.py +0 -0
@@ -1,9 +1,9 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: httpx-limiter
3
- Version: 0.5.0
3
+ Version: 0.6.0
4
4
  Summary: A lightweight package that provides rate-limited httpx transports.
5
5
  Project-URL: Homepage, https://github.com/Midnighter/httpx-limiter
6
- Project-URL: Documentation, https://httpx-limiter.readthedocs.io
6
+ Project-URL: Documentation, https://midnighter.github.io/httpx-limiter/
7
7
  Project-URL: Source Code, https://github.com/Midnighter/httpx-limiter
8
8
  Project-URL: Bug Tracker, https://github.com/Midnighter/httpx-limiter/issues
9
9
  Project-URL: Download, https://pypi.org/project/httpx-limiter/#files
@@ -13,7 +13,6 @@ License-File: LICENSE
13
13
  Keywords: httpx,leaky bucket,limiter,rate-limit
14
14
  Classifier: Development Status :: 2 - Pre-Alpha
15
15
  Classifier: Environment :: Web Environment
16
- Classifier: Framework :: AsyncIO
17
16
  Classifier: Intended Audience :: Developers
18
17
  Classifier: License :: OSI Approved :: Apache Software License
19
18
  Classifier: Natural Language :: English
@@ -23,6 +22,7 @@ Classifier: Programming Language :: Python :: 3.10
23
22
  Classifier: Programming Language :: Python :: 3.11
24
23
  Classifier: Programming Language :: Python :: 3.12
25
24
  Classifier: Programming Language :: Python :: 3.13
25
+ Classifier: Programming Language :: Python :: 3.14
26
26
  Classifier: Topic :: Internet :: WWW/HTTP
27
27
  Classifier: Typing :: Typed
28
28
  Requires-Python: >=3.10
@@ -31,7 +31,7 @@ Requires-Dist: typing-extensions~=4.6; python_version < '3.11'
31
31
  Provides-Extra: aiolimiter
32
32
  Requires-Dist: aiolimiter~=1.2; extra == 'aiolimiter'
33
33
  Provides-Extra: pyrate
34
- Requires-Dist: pyrate-limiter~=3.9; extra == 'pyrate'
34
+ Requires-Dist: pyrate-limiter~=4.0; extra == 'pyrate'
35
35
  Description-Content-Type: text/markdown
36
36
 
37
37
  # HTTPX Limiter
@@ -80,6 +80,7 @@ from:
80
80
  roughly one token will be added every 500 milliseconds
81
81
 
82
82
  ```python
83
+ from httpx_limiter import Rate
83
84
  from httpx_limiter.aiolimiter import AiolimiterAsyncLimiter
84
85
 
85
86
  limiter = AiolimiterAsyncLimiter.create(Rate.create(magnitude=20))
@@ -96,16 +97,18 @@ limiter = AiolimiterAsyncLimiter.create(Rate.create(magnitude=20))
96
97
  two token will be added every second
97
98
 
98
99
  ```python
100
+ from httpx_limiter import Rate
99
101
  from httpx_limiter.pyrate import PyrateAsyncLimiter
100
102
 
101
- # Single rate limit
102
- limiter = PyrateAsyncLimiter.create(Rate.create(magnitude=20))
103
+ async def main():
104
+ # Single rate limit
105
+ limiter = PyrateAsyncLimiter.create(Rate.create(magnitude=20))
103
106
 
104
- # Multiple rate limits
105
- limiter = PyrateAsyncLimiter.create(
106
- Rate.create(magnitude=10), # 10 per second
107
- Rate.create(magnitude=100, duration=60), # 100 per minute
108
- )
107
+ # Multiple rate limits
108
+ limiter = PyrateAsyncLimiter.create(
109
+ Rate.create(magnitude=10), # 10 per second
110
+ Rate.create(magnitude=100, duration=60), # 100 per minute
111
+ )
109
112
  ```
110
113
 
111
114
  ## Tutorial
@@ -164,6 +167,7 @@ from httpx_limiter import (
164
167
  )
165
168
  from httpx_limiter.aiolimiter import AiolimiterAsyncLimiter
166
169
 
170
+
167
171
  class DomainBasedRateLimiterRepository(AbstractRateLimiterRepository):
168
172
  """Apply different rate limits based on the domain being requested."""
169
173
 
@@ -175,11 +179,15 @@ class DomainBasedRateLimiterRepository(AbstractRateLimiterRepository):
175
179
  """Create a rate limiter for the domain."""
176
180
  return AiolimiterAsyncLimiter.create(Rate.create(magnitude=25))
177
181
 
178
- client = httpx.AsyncClient(
179
- transport=AsyncMultiRateLimitedTransport.create(
180
- repository=DomainBasedRateLimiterRepository(),
181
- ),
182
- )
182
+
183
+ async def main():
184
+ async with httpx.AsyncClient(
185
+ transport=AsyncMultiRateLimitedTransport.create(
186
+ repository=DomainBasedRateLimiterRepository(),
187
+ ),
188
+ ) as client:
189
+ # Use the client instance as you normally would.
190
+ pass
183
191
  ```
184
192
 
185
193
  > [!TIP]
@@ -193,6 +201,7 @@ import httpx
193
201
  from httpx_limiter import AbstractRateLimiterRepository, Rate
194
202
  from httpx_limiter.pyrate import PyrateAsyncLimiter
195
203
 
204
+
196
205
  class DayNightRateLimiterRepository(AbstractRateLimiterRepository):
197
206
  """Apply different rate limits based on the time of day."""
198
207
 
@@ -44,6 +44,7 @@ from:
44
44
  roughly one token will be added every 500 milliseconds
45
45
 
46
46
  ```python
47
+ from httpx_limiter import Rate
47
48
  from httpx_limiter.aiolimiter import AiolimiterAsyncLimiter
48
49
 
49
50
  limiter = AiolimiterAsyncLimiter.create(Rate.create(magnitude=20))
@@ -60,16 +61,18 @@ limiter = AiolimiterAsyncLimiter.create(Rate.create(magnitude=20))
60
61
  two token will be added every second
61
62
 
62
63
  ```python
64
+ from httpx_limiter import Rate
63
65
  from httpx_limiter.pyrate import PyrateAsyncLimiter
64
66
 
65
- # Single rate limit
66
- limiter = PyrateAsyncLimiter.create(Rate.create(magnitude=20))
67
+ async def main():
68
+ # Single rate limit
69
+ limiter = PyrateAsyncLimiter.create(Rate.create(magnitude=20))
67
70
 
68
- # Multiple rate limits
69
- limiter = PyrateAsyncLimiter.create(
70
- Rate.create(magnitude=10), # 10 per second
71
- Rate.create(magnitude=100, duration=60), # 100 per minute
72
- )
71
+ # Multiple rate limits
72
+ limiter = PyrateAsyncLimiter.create(
73
+ Rate.create(magnitude=10), # 10 per second
74
+ Rate.create(magnitude=100, duration=60), # 100 per minute
75
+ )
73
76
  ```
74
77
 
75
78
  ## Tutorial
@@ -128,6 +131,7 @@ from httpx_limiter import (
128
131
  )
129
132
  from httpx_limiter.aiolimiter import AiolimiterAsyncLimiter
130
133
 
134
+
131
135
  class DomainBasedRateLimiterRepository(AbstractRateLimiterRepository):
132
136
  """Apply different rate limits based on the domain being requested."""
133
137
 
@@ -139,11 +143,15 @@ class DomainBasedRateLimiterRepository(AbstractRateLimiterRepository):
139
143
  """Create a rate limiter for the domain."""
140
144
  return AiolimiterAsyncLimiter.create(Rate.create(magnitude=25))
141
145
 
142
- client = httpx.AsyncClient(
143
- transport=AsyncMultiRateLimitedTransport.create(
144
- repository=DomainBasedRateLimiterRepository(),
145
- ),
146
- )
146
+
147
+ async def main():
148
+ async with httpx.AsyncClient(
149
+ transport=AsyncMultiRateLimitedTransport.create(
150
+ repository=DomainBasedRateLimiterRepository(),
151
+ ),
152
+ ) as client:
153
+ # Use the client instance as you normally would.
154
+ pass
147
155
  ```
148
156
 
149
157
  > [!TIP]
@@ -157,6 +165,7 @@ import httpx
157
165
  from httpx_limiter import AbstractRateLimiterRepository, Rate
158
166
  from httpx_limiter.pyrate import PyrateAsyncLimiter
159
167
 
168
+
160
169
  class DayNightRateLimiterRepository(AbstractRateLimiterRepository):
161
170
  """Apply different rate limits based on the time of day."""
162
171
 
@@ -22,7 +22,6 @@ requires-python = ">=3.10"
22
22
  classifiers = [
23
23
  "Development Status :: 2 - Pre-Alpha",
24
24
  "Environment :: Web Environment",
25
- "Framework :: AsyncIO",
26
25
  "Intended Audience :: Developers",
27
26
  "License :: OSI Approved :: Apache Software License",
28
27
  "Natural Language :: English",
@@ -32,6 +31,7 @@ classifiers = [
32
31
  "Programming Language :: Python :: 3.11",
33
32
  "Programming Language :: Python :: 3.12",
34
33
  "Programming Language :: Python :: 3.13",
34
+ "Programming Language :: Python :: 3.14",
35
35
  "Topic :: Internet :: WWW/HTTP",
36
36
  "Typing :: Typed",
37
37
  ]
@@ -43,14 +43,25 @@ dependencies = [
43
43
 
44
44
  [project.urls]
45
45
  Homepage = "https://github.com/Midnighter/httpx-limiter"
46
- Documentation = "https://httpx-limiter.readthedocs.io"
46
+ Documentation = "https://midnighter.github.io/httpx-limiter/"
47
47
  "Source Code" = "https://github.com/Midnighter/httpx-limiter"
48
48
  "Bug Tracker" = "https://github.com/Midnighter/httpx-limiter/issues"
49
49
  Download = "https://pypi.org/project/httpx-limiter/#files"
50
50
 
51
51
  [project.optional-dependencies]
52
52
  aiolimiter = ["aiolimiter ~=1.2"]
53
- pyrate = ["pyrate-limiter ~=3.9"]
53
+ pyrate = ["pyrate-limiter ~=4.0"]
54
+
55
+ [dependency-groups]
56
+ style = ["pydoclint", "ruff"]
57
+ types = ["mypy", "ty"]
58
+ docs = [
59
+ "mkdocs-material ~=9.5",
60
+ "mkdocstrings[python] ~=1.0",
61
+ "mkdocs-awesome-pages-plugin ~=2.9",
62
+ "mike ~=2.1",
63
+ ]
64
+ tests = ["anyio", "pytest", "pytest-cov", "pytest-httpx", "pytest-raises"]
54
65
 
55
66
  ################################################################################
56
67
  # Tool Configuration
@@ -129,39 +140,36 @@ check-return-types = false
129
140
  check-yield-types = false
130
141
  exclude = "_version.py"
131
142
 
132
- # TODO: Adjust mypy configuration.
133
- #[tool.mypy]
134
- #plugins = [
135
- # "pydantic.mypy",
136
- #]
137
-
138
- # Stop mypy from complaining about missing types from imports.
139
- [[tool.mypy.overrides]]
140
- module = ["limiter"]
141
- ignore_missing_imports = true
142
-
143
- #[tool.pydantic-mypy]
144
- #init_forbid_extra = true
145
- #init_typed = true
146
- #warn_required_dynamic_aliases = true
147
-
148
143
  ################################################################################
149
144
  # Hatch Environments
150
145
  ################################################################################
151
146
 
147
+ [tool.hatch.env]
148
+ requires = ["hatch-pip-compile"]
149
+
152
150
  [tool.hatch.envs.default]
153
- installer = "uv"
151
+ description = """
152
+ Base environment with default configuration values.
153
+
154
+ All environments inheriting from the default also inherit the configuration values set
155
+ here.
156
+ """
157
+ type = "pip-compile"
158
+ lock-filename = "requirements/requirements-{env_name}.txt"
159
+ pip-compile-constraint = "dev"
160
+ pip-compile-hashes = true
161
+ pip-compile-resolver = "uv"
162
+ pip-compile-installer = "uv"
154
163
  features = ["aiolimiter", "pyrate"]
155
164
 
165
+ [tool.hatch.envs.no-install]
166
+ description = """A base environment for use cases that do not install the project."""
167
+ skip-install = true
168
+ features = []
169
+
156
170
  [tool.hatch.envs.dev]
157
171
  description = """Development environment."""
158
- extra-dependencies = [
159
- "anyio",
160
- "pytest",
161
- "pytest-httpx",
162
- "pytest-watcher",
163
- "ruff",
164
- ]
172
+ dependency-groups = ["style", "types", "docs", "tests"]
165
173
 
166
174
  [tool.hatch.envs.dev.env-vars]
167
175
  PYTHONDEVMODE = "1"
@@ -169,14 +177,14 @@ PYTHONASYNCIODEBUG = "1"
169
177
 
170
178
  [tool.hatch.envs.style]
171
179
  description = """Check the style of the codebase."""
172
- dependencies = ["pydoclint", "ruff"]
173
- detached = true
174
- installer = "uv"
180
+ template = "no-install"
181
+ dependency-groups = ["style"]
175
182
 
176
183
  [tool.hatch.envs.style.scripts]
177
184
  docstrings = ["pydoclint src", "pydoclint tests"]
178
185
  code = "ruff check {args}"
179
186
  format = "ruff format {args}"
187
+ fix = "ruff check --fix {args}"
180
188
  check = ["docstrings", "code"]
181
189
 
182
190
  [tool.hatch.envs.audit]
@@ -188,19 +196,14 @@ check = ["pip-audit"]
188
196
 
189
197
  [tool.hatch.envs.types]
190
198
  description = """Check the static types of the codebase."""
191
- extra-dependencies = ["mypy"]
199
+ dependency-groups = ["types"]
192
200
 
193
201
  [tool.hatch.envs.types.scripts]
194
- check = "mypy src/httpx_limiter"
202
+ check = ["mypy src/httpx_limiter", "ty check src/httpx_limiter"]
195
203
 
196
204
  [tool.hatch.envs.docs]
197
205
  description = """Build or serve the documentation."""
198
- extra-dependencies = [
199
- "mkdocs-material ~=9.5",
200
- "mkdocstrings[python] ~=0.24",
201
- "mkdocs-awesome-pages-plugin ~=2.9",
202
- "mike ~=2.1",
203
- ]
206
+ dependency-groups = ["docs"]
204
207
 
205
208
  [tool.hatch.envs.docs.scripts]
206
209
  build = "mkdocs build {args:--clean --strict}"
@@ -209,9 +212,8 @@ deploy = "mike deploy {args}"
209
212
 
210
213
  [tool.hatch.envs.distribution]
211
214
  description = """Test the source and wheel distribution of the package."""
215
+ template = "no-install"
212
216
  dependencies = ["pip", "twine"]
213
- detached = true
214
- installer = "uv"
215
217
  builder = true
216
218
 
217
219
  [tool.hatch.envs.distribution.scripts]
@@ -219,40 +221,43 @@ check = ["pip check", "hatch build {args:--clean}", "twine check dist/*"]
219
221
 
220
222
  [tool.hatch.envs.unit-tests]
221
223
  description = """Run the unit tests."""
222
- extra-dependencies = [
223
- "anyio",
224
- "pytest",
225
- "pytest-cov",
226
- "pytest-httpx",
227
- "pytest-raises",
228
- ]
224
+ dependency-groups = ["tests"]
229
225
 
230
226
  [tool.hatch.envs.unit-tests.env-vars]
231
227
  PYTHONDEVMODE = "1"
232
228
  PYTHONASYNCIODEBUG = "1"
233
229
 
234
230
  [[tool.hatch.envs.unit-tests.matrix]]
235
- python = ["3.10", "3.11", "3.12", "3.13"]
231
+ python = ["3.10", "3.11", "3.12", "3.13", "3.14"]
236
232
 
237
233
  [tool.hatch.envs.unit-tests.scripts]
238
234
  run = "pytest {args:--cov=httpx_limiter --cov-report=term-missing} {root}/tests/unit"
239
235
 
240
236
  [tool.hatch.envs.acceptance-tests]
241
237
  description = """Run the acceptance tests."""
242
- extra-dependencies = [
243
- "anyio",
244
- "pytest",
245
- "pytest-cov",
246
- "pytest-httpx",
247
- "pytest-raises",
248
- ]
238
+ dependency-groups = ["tests"]
249
239
 
250
240
  [tool.hatch.envs.acceptance-tests.env-vars]
251
241
  PYTHONDEVMODE = "1"
252
242
  PYTHONASYNCIODEBUG = "1"
253
243
 
254
244
  [[tool.hatch.envs.acceptance-tests.matrix]]
255
- python = ["3.10", "3.11", "3.12", "3.13"]
245
+ python = ["3.10", "3.11", "3.12", "3.13", "3.14"]
256
246
 
257
247
  [tool.hatch.envs.acceptance-tests.scripts]
258
248
  run = "pytest {args:--cov=httpx_limiter --cov-report=term-missing} {root}/tests/acceptance"
249
+
250
+ [tool.hatch.envs.documentation-tests]
251
+ description = """Run the documentation tests."""
252
+ dependency-groups = ["tests"]
253
+ extra-dependencies = ["mktestdocs", "httpx-tenacity ~=0.1"]
254
+
255
+ [tool.hatch.envs.documentation-tests.env-vars]
256
+ PYTHONDEVMODE = "1"
257
+ PYTHONASYNCIODEBUG = "1"
258
+
259
+ [[tool.hatch.envs.documentation-tests.matrix]]
260
+ python = ["3.10", "3.11", "3.12", "3.13", "3.14"]
261
+
262
+ [tool.hatch.envs.documentation-tests.scripts]
263
+ run = "pytest {args:--cov=httpx_limiter --cov-report=term-missing} {root}/tests/documentation"
@@ -28,7 +28,7 @@ version_tuple: VERSION_TUPLE
28
28
  commit_id: COMMIT_ID
29
29
  __commit_id__: COMMIT_ID
30
30
 
31
- __version__ = version = '0.5.0'
32
- __version_tuple__ = version_tuple = (0, 5, 0)
31
+ __version__ = version = '0.6.0'
32
+ __version_tuple__ = version_tuple = (0, 6, 0)
33
33
 
34
34
  __commit_id__ = commit_id = None
@@ -38,8 +38,7 @@ class AbstractAsyncLimiter(ABC):
38
38
  async def __aenter__(self) -> AbstractAsyncLimiter:
39
39
  """Acquire a rate limit token upon entering the asynchronous context."""
40
40
 
41
- @abstractmethod
42
- async def __aexit__(
41
+ async def __aexit__( # noqa: B027
43
42
  self,
44
43
  exc_type: type[BaseException] | None,
45
44
  exc_val: BaseException | None,
@@ -79,7 +79,7 @@ class AsyncMultiRateLimitedTransport(httpx.AsyncBaseTransport):
79
79
  """
80
80
  return cls(
81
81
  repository=repository,
82
- transport=httpx.AsyncHTTPTransport(**kwargs), # type: ignore[arg-type]
82
+ transport=httpx.AsyncHTTPTransport(**kwargs),
83
83
  )
84
84
 
85
85
  async def handle_async_request(
@@ -27,12 +27,9 @@ else:
27
27
  from typing import Unpack
28
28
 
29
29
  from pyrate_limiter import (
30
- AbstractClock,
31
30
  BucketAsyncWrapper,
32
- Duration,
33
31
  InMemoryBucket,
34
32
  Limiter,
35
- TimeAsyncClock,
36
33
  validate_rate_list,
37
34
  )
38
35
  from pyrate_limiter import Rate as PyRate
@@ -41,18 +38,12 @@ from httpx_limiter import AbstractAsyncLimiter
41
38
 
42
39
 
43
40
  if TYPE_CHECKING: # pragma: no cover
44
- from types import TracebackType
45
-
46
41
  from httpx_limiter.rate import Rate
47
42
 
48
43
 
49
44
  class PyRateLimiterKeywordArguments(TypedDict, total=False):
50
45
  """Keyword arguments for the pyrate limiter."""
51
46
 
52
- clock: AbstractClock
53
- raise_when_fail: bool
54
- max_delay: int | Duration | None
55
- retry_until_max_delay: bool
56
47
  buffer_ms: int
57
48
 
58
49
 
@@ -96,29 +87,13 @@ class PyrateAsyncLimiter(AbstractAsyncLimiter):
96
87
  )
97
88
  raise ValueError(msg)
98
89
 
99
- # Cast kwargs to proper types for internal use
100
90
  limiter = Limiter(
101
- argument=BucketAsyncWrapper(InMemoryBucket(rate_limits)),
102
- clock=kwargs.get("clock", TimeAsyncClock()),
103
- raise_when_fail=kwargs.get("raise_when_fail", False),
104
- max_delay=kwargs.get("max_delay", Duration.HOUR),
105
- retry_until_max_delay=kwargs.get("retry_until_max_delay", True),
91
+ BucketAsyncWrapper(InMemoryBucket(rate_limits)),
106
92
  buffer_ms=kwargs.get("buffer_ms", 50),
107
93
  )
108
94
  return cls(limiter=limiter)
109
95
 
110
96
  async def __aenter__(self) -> PyrateAsyncLimiter: # noqa: PYI034
111
97
  """Acquire a token upon entering the asynchronous context."""
112
- # Keep trying to acquire, let timeouts be handled externally.
113
- while not (await self._limiter.try_acquire_async("httpx-limiter")):
114
- pass # pragma: no cover
115
-
98
+ await self._limiter.try_acquire_async(__name__)
116
99
  return self
117
-
118
- async def __aexit__(
119
- self,
120
- _exc_type: type[BaseException] | None,
121
- _exc_val: BaseException | None,
122
- _exc_tb: TracebackType | None,
123
- ) -> None:
124
- """Exit the asynchronous context."""
File without changes
File without changes