ratethrottle 1.0.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.
- ratethrottle-1.0.0/LICENSE +21 -0
- ratethrottle-1.0.0/PKG-INFO +105 -0
- ratethrottle-1.0.0/README.md +66 -0
- ratethrottle-1.0.0/pyproject.toml +142 -0
- ratethrottle-1.0.0/ratethrottle/__init__.py +125 -0
- ratethrottle-1.0.0/ratethrottle/__main__.py +23 -0
- ratethrottle-1.0.0/ratethrottle/analytics.py +620 -0
- ratethrottle-1.0.0/ratethrottle/cli.py +587 -0
- ratethrottle-1.0.0/ratethrottle/config.py +493 -0
- ratethrottle-1.0.0/ratethrottle/core.py +659 -0
- ratethrottle-1.0.0/ratethrottle/ddos.py +648 -0
- ratethrottle-1.0.0/ratethrottle/exceptions.py +57 -0
- ratethrottle-1.0.0/ratethrottle/helpers.py +247 -0
- ratethrottle-1.0.0/ratethrottle/middleware.py +861 -0
- ratethrottle-1.0.0/ratethrottle/storage_backend.py +604 -0
- ratethrottle-1.0.0/ratethrottle/strategies.py +466 -0
- ratethrottle-1.0.0/ratethrottle.egg-info/PKG-INFO +105 -0
- ratethrottle-1.0.0/ratethrottle.egg-info/SOURCES.txt +29 -0
- ratethrottle-1.0.0/ratethrottle.egg-info/dependency_links.txt +1 -0
- ratethrottle-1.0.0/ratethrottle.egg-info/entry_points.txt +2 -0
- ratethrottle-1.0.0/ratethrottle.egg-info/requires.txt +34 -0
- ratethrottle-1.0.0/ratethrottle.egg-info/top_level.txt +1 -0
- ratethrottle-1.0.0/setup.cfg +4 -0
- ratethrottle-1.0.0/tests/test_cli.py +583 -0
- ratethrottle-1.0.0/tests/test_config.py +308 -0
- ratethrottle-1.0.0/tests/test_core.py +270 -0
- ratethrottle-1.0.0/tests/test_ddos.py +431 -0
- ratethrottle-1.0.0/tests/test_helpers.py +160 -0
- ratethrottle-1.0.0/tests/test_middleware.py +515 -0
- ratethrottle-1.0.0/tests/test_storage_backend.py +353 -0
- ratethrottle-1.0.0/tests/test_strategies.py +376 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MykeChidi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ratethrottle
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Advanced rate limiting and DDoS protection for Python web applications
|
|
5
|
+
Project-URL: Documentation, https://github.com/MykeChidi/ratethrottle
|
|
6
|
+
Project-URL: Repository, https://github.com/MykeChidi/ratethrottle
|
|
7
|
+
Project-URL: Bug Tracker, https://github.com/MykeChidi/ratethrottle/issues
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Provides-Extra: redis
|
|
11
|
+
Requires-Dist: redis>=7.0.0; extra == "redis"
|
|
12
|
+
Provides-Extra: flask
|
|
13
|
+
Requires-Dist: flask>=3.0.0; extra == "flask"
|
|
14
|
+
Provides-Extra: fastapi
|
|
15
|
+
Requires-Dist: fastapi>=0.129.0; extra == "fastapi"
|
|
16
|
+
Requires-Dist: starlette>=0.52.0; extra == "fastapi"
|
|
17
|
+
Provides-Extra: django
|
|
18
|
+
Requires-Dist: django>=5.2.0; extra == "django"
|
|
19
|
+
Provides-Extra: frameworks
|
|
20
|
+
Requires-Dist: redis>=7.0.0; extra == "frameworks"
|
|
21
|
+
Requires-Dist: flask>=3.0.0; extra == "frameworks"
|
|
22
|
+
Requires-Dist: fastapi>=0.100.0; extra == "frameworks"
|
|
23
|
+
Requires-Dist: starlette>=0.52.0; extra == "frameworks"
|
|
24
|
+
Requires-Dist: django>=5.2.0; extra == "frameworks"
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: pytest>=9.0.0; extra == "dev"
|
|
27
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
28
|
+
Requires-Dist: pytest-asyncio>=1.2.0; extra == "dev"
|
|
29
|
+
Requires-Dist: pytest-mock>=3.10.0; extra == "dev"
|
|
30
|
+
Requires-Dist: black>=26.0.0; extra == "dev"
|
|
31
|
+
Requires-Dist: flake8>=7.0.0; extra == "dev"
|
|
32
|
+
Requires-Dist: mypy>=1.0.0; extra == "dev"
|
|
33
|
+
Requires-Dist: isort>=7.0.0; extra == "dev"
|
|
34
|
+
Requires-Dist: bandit>=1.9.0; extra == "dev"
|
|
35
|
+
Requires-Dist: pre-commit>=4.0.0; extra == "dev"
|
|
36
|
+
Requires-Dist: types-PyYAML>=6.0.0; extra == "dev"
|
|
37
|
+
Requires-Dist: types-redis>=4.5.0; extra == "dev"
|
|
38
|
+
Dynamic: license-file
|
|
39
|
+
|
|
40
|
+
# `RateThrottle`
|
|
41
|
+
|
|
42
|
+
[](https://opensource.org/licenses/MIT)
|
|
43
|
+
|
|
44
|
+
**Advanced rate limiting and DDoS protection for Python web applications.**
|
|
45
|
+
|
|
46
|
+
RateThrottle is a comprehensive rate limiting library that provides enterprise-level features for protecting your APIs and web applications from abuse, with built-in DDoS protection, different storage backends, multiple strategies and seamless integration with popular Python web frameworks.
|
|
47
|
+
|
|
48
|
+
## ✨ Features
|
|
49
|
+
|
|
50
|
+
- 🚀 **Multiple Rate Limiting Strategies**
|
|
51
|
+
- Token Bucket
|
|
52
|
+
- Leaky Bucket
|
|
53
|
+
- Fixed Window
|
|
54
|
+
- Sliding Window Log
|
|
55
|
+
|
|
56
|
+
- 🛡️ **Advanced DDoS Protection**
|
|
57
|
+
- Traffic pattern analysis
|
|
58
|
+
- Automatic suspicious activity detection
|
|
59
|
+
- Auto-blocking capabilities
|
|
60
|
+
|
|
61
|
+
- 💾 **Flexible Storage Backends**
|
|
62
|
+
- In-memory (single instance)
|
|
63
|
+
- Redis (distributed/multi-server)
|
|
64
|
+
- Easy to extend with custom backends
|
|
65
|
+
|
|
66
|
+
- 🔧 **Framework Integration**
|
|
67
|
+
- Flask
|
|
68
|
+
- FastAPI
|
|
69
|
+
- Django
|
|
70
|
+
- Starlette
|
|
71
|
+
- Generic WSGI/ASGI support
|
|
72
|
+
|
|
73
|
+
- 📊 **Monitoring & Analytics**
|
|
74
|
+
- Real-time metrics
|
|
75
|
+
- Violation tracking
|
|
76
|
+
- CLI dashboard
|
|
77
|
+
|
|
78
|
+
- ⚙️ **Configuration Management**
|
|
79
|
+
- YAML configuration files
|
|
80
|
+
- Programmatic configuration
|
|
81
|
+
- Hot-reloading support
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
## 📊 Performance
|
|
85
|
+
|
|
86
|
+
RateThrottle is designed for high performance:
|
|
87
|
+
|
|
88
|
+
- **In-memory storage**: 100,000+ requests/second
|
|
89
|
+
- **Redis storage**: 50,000+ requests/second (network dependent)
|
|
90
|
+
- **Minimal overhead**: < 1ms per request check
|
|
91
|
+
- **Thread-safe**: Safe for concurrent use
|
|
92
|
+
- **Memory efficient**: Automatic cleanup of expired data
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
## 📝 License
|
|
96
|
+
|
|
97
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
## 📮 Support
|
|
101
|
+
|
|
102
|
+
- 🐛 Issues: [GitHub Issues](https://github.com/MykeChidi/ratethrottle/issues)
|
|
103
|
+
- 📖 Documentation: [Full Documentation](README)
|
|
104
|
+
|
|
105
|
+
---
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# `RateThrottle`
|
|
2
|
+
|
|
3
|
+
[](https://opensource.org/licenses/MIT)
|
|
4
|
+
|
|
5
|
+
**Advanced rate limiting and DDoS protection for Python web applications.**
|
|
6
|
+
|
|
7
|
+
RateThrottle is a comprehensive rate limiting library that provides enterprise-level features for protecting your APIs and web applications from abuse, with built-in DDoS protection, different storage backends, multiple strategies and seamless integration with popular Python web frameworks.
|
|
8
|
+
|
|
9
|
+
## ✨ Features
|
|
10
|
+
|
|
11
|
+
- 🚀 **Multiple Rate Limiting Strategies**
|
|
12
|
+
- Token Bucket
|
|
13
|
+
- Leaky Bucket
|
|
14
|
+
- Fixed Window
|
|
15
|
+
- Sliding Window Log
|
|
16
|
+
|
|
17
|
+
- 🛡️ **Advanced DDoS Protection**
|
|
18
|
+
- Traffic pattern analysis
|
|
19
|
+
- Automatic suspicious activity detection
|
|
20
|
+
- Auto-blocking capabilities
|
|
21
|
+
|
|
22
|
+
- 💾 **Flexible Storage Backends**
|
|
23
|
+
- In-memory (single instance)
|
|
24
|
+
- Redis (distributed/multi-server)
|
|
25
|
+
- Easy to extend with custom backends
|
|
26
|
+
|
|
27
|
+
- 🔧 **Framework Integration**
|
|
28
|
+
- Flask
|
|
29
|
+
- FastAPI
|
|
30
|
+
- Django
|
|
31
|
+
- Starlette
|
|
32
|
+
- Generic WSGI/ASGI support
|
|
33
|
+
|
|
34
|
+
- 📊 **Monitoring & Analytics**
|
|
35
|
+
- Real-time metrics
|
|
36
|
+
- Violation tracking
|
|
37
|
+
- CLI dashboard
|
|
38
|
+
|
|
39
|
+
- ⚙️ **Configuration Management**
|
|
40
|
+
- YAML configuration files
|
|
41
|
+
- Programmatic configuration
|
|
42
|
+
- Hot-reloading support
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
## 📊 Performance
|
|
46
|
+
|
|
47
|
+
RateThrottle is designed for high performance:
|
|
48
|
+
|
|
49
|
+
- **In-memory storage**: 100,000+ requests/second
|
|
50
|
+
- **Redis storage**: 50,000+ requests/second (network dependent)
|
|
51
|
+
- **Minimal overhead**: < 1ms per request check
|
|
52
|
+
- **Thread-safe**: Safe for concurrent use
|
|
53
|
+
- **Memory efficient**: Automatic cleanup of expired data
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
## 📝 License
|
|
57
|
+
|
|
58
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
## 📮 Support
|
|
62
|
+
|
|
63
|
+
- 🐛 Issues: [GitHub Issues](https://github.com/MykeChidi/ratethrottle/issues)
|
|
64
|
+
- 📖 Documentation: [Full Documentation](README)
|
|
65
|
+
|
|
66
|
+
---
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "ratethrottle"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "Advanced rate limiting and DDoS protection for Python web applications"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
|
|
11
|
+
[author]
|
|
12
|
+
name = "MykeChidi"
|
|
13
|
+
|
|
14
|
+
license = {text = "MIT"}
|
|
15
|
+
requires-python = ">=3.10"
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"License :: OSI Approved :: MIT License",
|
|
20
|
+
"Operating System :: OS Independent",
|
|
21
|
+
"Programming Language :: Python :: 3",
|
|
22
|
+
"Programming Language :: Python :: 3.10",
|
|
23
|
+
"Programming Language :: Python :: 3.11",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
"Topic :: Internet :: WWW/HTTP",
|
|
26
|
+
"Topic :: Security",
|
|
27
|
+
]
|
|
28
|
+
keywords = ["rate-limiting", "rate-limit", "throttlling", "api", "ddos", "security", "api-gateway"]
|
|
29
|
+
dependencies = [
|
|
30
|
+
"pyyaml>=6.0",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.optional-dependencies]
|
|
34
|
+
redis = ["redis>=7.0.0"]
|
|
35
|
+
flask = ["flask>=3.0.0"]
|
|
36
|
+
fastapi = ["fastapi>=0.129.0", "starlette>=0.52.0"]
|
|
37
|
+
django = ["django>=5.2.0"]
|
|
38
|
+
frameworks = [
|
|
39
|
+
"redis>=7.0.0",
|
|
40
|
+
"flask>=3.0.0",
|
|
41
|
+
"fastapi>=0.100.0",
|
|
42
|
+
"starlette>=0.52.0",
|
|
43
|
+
"django>=5.2.0"
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
dev = [
|
|
47
|
+
"pytest>=9.0.0",
|
|
48
|
+
"pytest-cov>=4.0.0",
|
|
49
|
+
"pytest-asyncio>=1.2.0",
|
|
50
|
+
"pytest-mock>=3.10.0",
|
|
51
|
+
"black>=26.0.0",
|
|
52
|
+
"flake8>=7.0.0",
|
|
53
|
+
"mypy>=1.0.0",
|
|
54
|
+
"isort>=7.0.0",
|
|
55
|
+
"bandit>=1.9.0",
|
|
56
|
+
"pre-commit>=4.0.0",
|
|
57
|
+
"types-PyYAML>=6.0.0",
|
|
58
|
+
"types-redis>=4.5.0",
|
|
59
|
+
]
|
|
60
|
+
|
|
61
|
+
[project.urls]
|
|
62
|
+
Documentation = "https://github.com/MykeChidi/ratethrottle"
|
|
63
|
+
Repository = "https://github.com/MykeChidi/ratethrottle"
|
|
64
|
+
"Bug Tracker" = "https://github.com/MykeChidi/ratethrottle/issues"
|
|
65
|
+
|
|
66
|
+
[project.scripts]
|
|
67
|
+
ratethrottle = "ratethrottle.cli:main"
|
|
68
|
+
|
|
69
|
+
[tool.setuptools.packages.find]
|
|
70
|
+
where = ["."]
|
|
71
|
+
include = ["ratethrottle*"]
|
|
72
|
+
exclude = ["tests*", "examples*", "docs*"]
|
|
73
|
+
|
|
74
|
+
[tool.setuptools.package-data]
|
|
75
|
+
ratethrottle = ["py.typed"]
|
|
76
|
+
|
|
77
|
+
[tool.black]
|
|
78
|
+
line-length = 100
|
|
79
|
+
target-version = ['py310', 'py311', 'py312']
|
|
80
|
+
include = '\.pyi?$'
|
|
81
|
+
extend-exclude = '''
|
|
82
|
+
/(
|
|
83
|
+
# directories
|
|
84
|
+
\.eggs
|
|
85
|
+
| \.git
|
|
86
|
+
| \.hg
|
|
87
|
+
| \.mypy_cache
|
|
88
|
+
| \.tox
|
|
89
|
+
| \.venv
|
|
90
|
+
| build
|
|
91
|
+
| dist
|
|
92
|
+
)/
|
|
93
|
+
'''
|
|
94
|
+
|
|
95
|
+
[tool.isort]
|
|
96
|
+
profile = "black"
|
|
97
|
+
line_length = 100
|
|
98
|
+
multi_line_output = 3
|
|
99
|
+
include_trailing_comma = true
|
|
100
|
+
force_grid_wrap = 0
|
|
101
|
+
use_parentheses = true
|
|
102
|
+
ensure_newline_before_comments = true
|
|
103
|
+
|
|
104
|
+
[tool.mypy]
|
|
105
|
+
python_version = "3.10"
|
|
106
|
+
warn_return_any = true
|
|
107
|
+
warn_unused_configs = true
|
|
108
|
+
disallow_untyped_defs = false
|
|
109
|
+
disallow_incomplete_defs = false
|
|
110
|
+
check_untyped_defs = true
|
|
111
|
+
no_implicit_optional = true
|
|
112
|
+
warn_redundant_casts = true
|
|
113
|
+
warn_unused_ignores = true
|
|
114
|
+
warn_no_return = true
|
|
115
|
+
follow_imports = "normal"
|
|
116
|
+
ignore_missing_imports = true
|
|
117
|
+
|
|
118
|
+
[tool.pytest.ini_options]
|
|
119
|
+
minversion = "7.0"
|
|
120
|
+
addopts = "-ra -q --strict-markers --cov=ratethrottle --cov-report=term-missing --cov-report=html"
|
|
121
|
+
testpaths = ["tests"]
|
|
122
|
+
python_files = ["test_*.py"]
|
|
123
|
+
python_classes = ["Test*"]
|
|
124
|
+
python_functions = ["test_*"]
|
|
125
|
+
|
|
126
|
+
[tool.coverage.run]
|
|
127
|
+
source = ["ratethrottle"]
|
|
128
|
+
omit = [
|
|
129
|
+
"*/tests/*",
|
|
130
|
+
"*/test_*.py",
|
|
131
|
+
]
|
|
132
|
+
|
|
133
|
+
[tool.coverage.report]
|
|
134
|
+
exclude_lines = [
|
|
135
|
+
"pragma: no cover",
|
|
136
|
+
"def __repr__",
|
|
137
|
+
"raise AssertionError",
|
|
138
|
+
"raise NotImplementedError",
|
|
139
|
+
"if __name__ == .__main__.:",
|
|
140
|
+
"if TYPE_CHECKING:",
|
|
141
|
+
"@abstractmethod",
|
|
142
|
+
]
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"""
|
|
2
|
+
RateThrottle - Production-grade rate limiting and DDoS protection
|
|
3
|
+
|
|
4
|
+
A comprehensive rate limiting library for Python web applications with
|
|
5
|
+
enterprise features including DDoS protection, analytics, and multi-framework support.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import logging
|
|
9
|
+
from typing import Optional
|
|
10
|
+
|
|
11
|
+
from .analytics import RateThrottleAnalytics
|
|
12
|
+
from .config import ConfigManager
|
|
13
|
+
from .core import RateThrottleCore, RateThrottleRule
|
|
14
|
+
from .ddos import DDoSProtection
|
|
15
|
+
from .helpers import create_limiter
|
|
16
|
+
from .storage_backend import InMemoryStorage, StorageBackend
|
|
17
|
+
|
|
18
|
+
__version__ = "1.0.0"
|
|
19
|
+
__author__ = "MykeChidi"
|
|
20
|
+
__license__ = "MIT"
|
|
21
|
+
__all__ = [
|
|
22
|
+
# Core
|
|
23
|
+
"RateThrottleCore",
|
|
24
|
+
"RateThrottleRule",
|
|
25
|
+
"RateThrottleStatus",
|
|
26
|
+
"RateThrottleViolation",
|
|
27
|
+
# Storage
|
|
28
|
+
"StorageBackend",
|
|
29
|
+
"InMemoryStorage",
|
|
30
|
+
"RedisStorage",
|
|
31
|
+
# Middleware
|
|
32
|
+
"FlaskRateLimiter",
|
|
33
|
+
"FastAPIRateLimiter",
|
|
34
|
+
"DjangoRateLimitMiddleware",
|
|
35
|
+
"django_ratelimit",
|
|
36
|
+
"StarletteRateLimitMiddleware",
|
|
37
|
+
"WSGIRateLimitMiddleware",
|
|
38
|
+
# Config & Protection
|
|
39
|
+
"ConfigManager",
|
|
40
|
+
"DDoSProtection",
|
|
41
|
+
"RateThrottleAnalytics",
|
|
42
|
+
# Helpers
|
|
43
|
+
"create_limiter",
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
# Lazy imports to avoid import errors when optional dependencies are missing
|
|
48
|
+
def __getattr__(name: str):
|
|
49
|
+
"""Lazy import for optional components"""
|
|
50
|
+
|
|
51
|
+
if name == "RedisStorage":
|
|
52
|
+
try:
|
|
53
|
+
from .storage_backend import RedisStorage
|
|
54
|
+
|
|
55
|
+
return RedisStorage
|
|
56
|
+
except ImportError as e:
|
|
57
|
+
raise ImportError(
|
|
58
|
+
"RedisStorage requires 'redis' package. "
|
|
59
|
+
"Install it with: pip install ratethrottle[redis]"
|
|
60
|
+
) from e
|
|
61
|
+
|
|
62
|
+
# Middleware imports
|
|
63
|
+
elif name == "FlaskRateLimiter":
|
|
64
|
+
try:
|
|
65
|
+
from .middleware import FlaskRateLimiter
|
|
66
|
+
|
|
67
|
+
return FlaskRateLimiter
|
|
68
|
+
except ImportError as e:
|
|
69
|
+
raise ImportError(
|
|
70
|
+
"FlaskRateLimiter requires 'flask' package. "
|
|
71
|
+
"Install it with: pip install ratethrottle[flask]"
|
|
72
|
+
) from e
|
|
73
|
+
|
|
74
|
+
elif name == "FastAPIRateLimiter":
|
|
75
|
+
try:
|
|
76
|
+
from .middleware import FastAPIRateLimiter
|
|
77
|
+
|
|
78
|
+
return FastAPIRateLimiter
|
|
79
|
+
except ImportError as e:
|
|
80
|
+
raise ImportError(
|
|
81
|
+
"FastAPIRateLimiter requires 'fastapi' package. "
|
|
82
|
+
"Install it with: pip install ratethrottle[fastapi]"
|
|
83
|
+
) from e
|
|
84
|
+
|
|
85
|
+
elif name in ["DjangoRateLimitMiddleware", "django_ratelimit"]:
|
|
86
|
+
try:
|
|
87
|
+
from .middleware import DjangoRateLimitMiddleware, django_ratelimit # noqa
|
|
88
|
+
|
|
89
|
+
return locals()[name]
|
|
90
|
+
except ImportError as e:
|
|
91
|
+
raise ImportError(
|
|
92
|
+
"Django components require 'django' package. "
|
|
93
|
+
"Install it with: pip install ratethrottle[django]"
|
|
94
|
+
) from e
|
|
95
|
+
|
|
96
|
+
elif name in ["StarletteRateLimitMiddleware", "WSGIRateLimitMiddleware"]:
|
|
97
|
+
from .middleware import StarletteRateLimitMiddleware, WSGIRateLimitMiddleware # noqa
|
|
98
|
+
|
|
99
|
+
return locals()[name]
|
|
100
|
+
|
|
101
|
+
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def get_version() -> str:
|
|
105
|
+
"""Return the current version"""
|
|
106
|
+
return __version__
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def configure_logging(level: int = logging.INFO, handler: Optional[logging.Handler] = None):
|
|
110
|
+
"""
|
|
111
|
+
Configure logging for RateThrottle
|
|
112
|
+
|
|
113
|
+
Args:
|
|
114
|
+
level: Logging level (logging.DEBUG, logging.INFO, etc.)
|
|
115
|
+
handler: Custom logging handler (default: StreamHandler)
|
|
116
|
+
"""
|
|
117
|
+
logger = logging.getLogger("ratethrottle")
|
|
118
|
+
logger.setLevel(level)
|
|
119
|
+
|
|
120
|
+
if handler is None:
|
|
121
|
+
handler = logging.StreamHandler()
|
|
122
|
+
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
|
123
|
+
handler.setFormatter(formatter)
|
|
124
|
+
|
|
125
|
+
logger.addHandler(handler)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""
|
|
2
|
+
RateThottle Module entry point.
|
|
3
|
+
|
|
4
|
+
Allows you to run the package as a module.
|
|
5
|
+
python -m ratethrottle
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import sys
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def main():
|
|
12
|
+
"""Main entry point for the package."""
|
|
13
|
+
try:
|
|
14
|
+
from .cli import main as cli_main
|
|
15
|
+
|
|
16
|
+
sys.exit(cli_main())
|
|
17
|
+
except ImportError as e:
|
|
18
|
+
print(f"Error importing CLI: {e}")
|
|
19
|
+
sys.exit(1)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
if __name__ == "__main__":
|
|
23
|
+
main()
|