axios-python 0.1.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.
- axios_python-0.1.0/.gitignore +209 -0
- axios_python-0.1.0/PKG-INFO +348 -0
- axios_python-0.1.0/README.md +321 -0
- axios_python-0.1.0/axios_python/__init__.py +148 -0
- axios_python-0.1.0/axios_python/cancel/__init__.py +1 -0
- axios_python-0.1.0/axios_python/cancel/exceptions.py +9 -0
- axios_python-0.1.0/axios_python/cancel/token.py +74 -0
- axios_python-0.1.0/axios_python/client.py +349 -0
- axios_python-0.1.0/axios_python/config.py +47 -0
- axios_python-0.1.0/axios_python/defaults.py +26 -0
- axios_python-0.1.0/axios_python/exceptions.py +47 -0
- axios_python-0.1.0/axios_python/interceptors/__init__.py +1 -0
- axios_python-0.1.0/axios_python/interceptors/chain.py +118 -0
- axios_python-0.1.0/axios_python/interceptors/manager.py +29 -0
- axios_python-0.1.0/axios_python/middleware/__init__.py +1 -0
- axios_python-0.1.0/axios_python/middleware/manager.py +54 -0
- axios_python-0.1.0/axios_python/middleware/pipeline.py +68 -0
- axios_python-0.1.0/axios_python/plugins/__init__.py +1 -0
- axios_python-0.1.0/axios_python/plugins/auth.py +49 -0
- axios_python-0.1.0/axios_python/plugins/base.py +27 -0
- axios_python-0.1.0/axios_python/plugins/cache.py +75 -0
- axios_python-0.1.0/axios_python/plugins/logger.py +57 -0
- axios_python-0.1.0/axios_python/request.py +23 -0
- axios_python-0.1.0/axios_python/response.py +137 -0
- axios_python-0.1.0/axios_python/retry/__init__.py +1 -0
- axios_python-0.1.0/axios_python/retry/engine.py +105 -0
- axios_python-0.1.0/axios_python/retry/strategy.py +116 -0
- axios_python-0.1.0/axios_python/transport/__init__.py +1 -0
- axios_python-0.1.0/axios_python/transport/base.py +50 -0
- axios_python-0.1.0/axios_python/transport/httpx_adapter.py +131 -0
- axios_python-0.1.0/axios_python/utils/__init__.py +1 -0
- axios_python-0.1.0/axios_python/utils/async_utils.py +56 -0
- axios_python-0.1.0/axios_python/utils/merge.py +33 -0
- axios_python-0.1.0/pyproject.toml +44 -0
- axios_python-0.1.0/rename2.py +45 -0
- axios_python-0.1.0/test_async.py +12 -0
- axios_python-0.1.0/test_stream.py +41 -0
- axios_python-0.1.0/test_sync.py +11 -0
- axios_python-0.1.0/test_v2.py +50 -0
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
#poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
#pdm.lock
|
|
116
|
+
#pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
#pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# SageMath parsed files
|
|
135
|
+
*.sage.py
|
|
136
|
+
|
|
137
|
+
# Environments
|
|
138
|
+
.env
|
|
139
|
+
.envrc
|
|
140
|
+
.venv
|
|
141
|
+
env/
|
|
142
|
+
venv/
|
|
143
|
+
ENV/
|
|
144
|
+
env.bak/
|
|
145
|
+
venv.bak/
|
|
146
|
+
|
|
147
|
+
# Spyder project settings
|
|
148
|
+
.spyderproject
|
|
149
|
+
.spyproject
|
|
150
|
+
|
|
151
|
+
# Rope project settings
|
|
152
|
+
.ropeproject
|
|
153
|
+
|
|
154
|
+
# mkdocs documentation
|
|
155
|
+
/site
|
|
156
|
+
|
|
157
|
+
# mypy
|
|
158
|
+
.mypy_cache/
|
|
159
|
+
.dmypy.json
|
|
160
|
+
dmypy.json
|
|
161
|
+
|
|
162
|
+
# Pyre type checker
|
|
163
|
+
.pyre/
|
|
164
|
+
|
|
165
|
+
# pytype static type analyzer
|
|
166
|
+
.pytype/
|
|
167
|
+
|
|
168
|
+
# Cython debug symbols
|
|
169
|
+
cython_debug/
|
|
170
|
+
|
|
171
|
+
# PyCharm
|
|
172
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
173
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
174
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
175
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
176
|
+
#.idea/
|
|
177
|
+
|
|
178
|
+
# Abstra
|
|
179
|
+
# Abstra is an AI-powered process automation framework.
|
|
180
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
181
|
+
# Learn more at https://abstra.io/docs
|
|
182
|
+
.abstra/
|
|
183
|
+
|
|
184
|
+
# Visual Studio Code
|
|
185
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
186
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
188
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
189
|
+
# .vscode/
|
|
190
|
+
|
|
191
|
+
# Ruff stuff:
|
|
192
|
+
.ruff_cache/
|
|
193
|
+
|
|
194
|
+
# PyPI configuration file
|
|
195
|
+
.pypirc
|
|
196
|
+
|
|
197
|
+
# Cursor
|
|
198
|
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
|
199
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
200
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
201
|
+
.cursorignore
|
|
202
|
+
.cursorindexingignore
|
|
203
|
+
|
|
204
|
+
# Marimo
|
|
205
|
+
marimo/_static/
|
|
206
|
+
marimo/_lsp/
|
|
207
|
+
__marimo__/
|
|
208
|
+
|
|
209
|
+
pre-commit-venv/
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: axios-python
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A developer-experience-first HTTP client for Python, inspired by Axios.
|
|
5
|
+
Project-URL: Homepage, https://github.com/ashavijit/axios_python
|
|
6
|
+
Project-URL: Repository, https://github.com/ashavijit/axios_python
|
|
7
|
+
Author: Avijit
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
Keywords: async,axios,client,http,interceptors,middleware,retry
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
18
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
19
|
+
Classifier: Typing :: Typed
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Requires-Dist: httpx>=0.27.0
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
|
|
24
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
25
|
+
Requires-Dist: respx>=0.21; extra == 'dev'
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# axios_python
|
|
29
|
+
|
|
30
|
+
[](https://pypi.org/project/axios_python/)
|
|
31
|
+
[](https://pypi.org/project/axios_python/)
|
|
32
|
+
[](https://opensource.org/licenses/MIT)
|
|
33
|
+
|
|
34
|
+
A developer-experience-first HTTP client for Python, heavily inspired by [Axios](https://axios-http.com/).
|
|
35
|
+
|
|
36
|
+
The Python ecosystem has amazing HTTP transport libraries (`requests`, `httpx`, `aiohttp`), but they are often focused purely on sending requests and getting responses. Modern applications need a **network orchestration layer**—features like request lifecycle hooks, middleware, interceptors, isolated client instances, request cancellation, and unified synchronous/asynchronous developer experience.
|
|
37
|
+
|
|
38
|
+
`axios_python` brings the elegant, feature-rich Axios model to Python, built natively on top of `httpx`.
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## Features
|
|
43
|
+
|
|
44
|
+
- 🌐 **Instance-based Client:** Completely isolated state for different APIs.
|
|
45
|
+
- 🔄 **Unified API:** Identical interface for both Sync (`api.get()`) and Async (`await api.async_get()`).
|
|
46
|
+
- 🔗 **Interceptors:** Hook into requests before they are sent, or responses before they are returned.
|
|
47
|
+
- 🚰 **Middleware Pipeline:** Express.js-style async middleware for complex request wrapping.
|
|
48
|
+
- 🔁 **Retry Engine:** Built-in strategies for linear, fixed, and exponential backoff.
|
|
49
|
+
- 🚫 **Cancellation Tokens:** Cleanly abort requests gracefully.
|
|
50
|
+
- 🔌 **Plugin System:** Easily extend clients with Cache, Auth, and Logging plugins (included out-of-the-box).
|
|
51
|
+
- 🧩 **Swappable Transport:** Backed by `httpx` by default, but completely abstracted for custom transports.
|
|
52
|
+
- 📝 **Fully Typed:** 100% strict typing support for modern Python 3.10+.
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## Installation
|
|
57
|
+
|
|
58
|
+
Install using `pip`:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pip install axios_python
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Requires Python 3.10+.
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## Quick Start
|
|
69
|
+
|
|
70
|
+
### Basic Synchronous Usage
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
import axios_python
|
|
74
|
+
|
|
75
|
+
# Zero-setup module level request
|
|
76
|
+
response = axios_python.get("https://httpbin.org/get", params={"query": "python"})
|
|
77
|
+
|
|
78
|
+
# Or create an isolated instance with default configuration
|
|
79
|
+
api = axios_python.create({
|
|
80
|
+
"base_url": "https://httpbin.org",
|
|
81
|
+
"timeout": 10,
|
|
82
|
+
"headers": {
|
|
83
|
+
"X-App-Client": "MyCLI/1.0"
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
# Make a request using the instance
|
|
88
|
+
response = api.get("/get", params={"query": "python"})
|
|
89
|
+
|
|
90
|
+
print(f"Status: {response.status_code}")
|
|
91
|
+
if response.ok:
|
|
92
|
+
print(response.json())
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Asynchronous Native
|
|
96
|
+
|
|
97
|
+
`axios_python` treats async as a first-class citizen. Just prefix method names with `async_`.
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
import asyncio
|
|
101
|
+
import axios_python
|
|
102
|
+
|
|
103
|
+
async def fetch_data():
|
|
104
|
+
# Non-blocking async call via instance
|
|
105
|
+
api = axios_python.create({"base_url": "https://httpbin.org"})
|
|
106
|
+
response = await api.async_get("/delay/2")
|
|
107
|
+
|
|
108
|
+
# Or module level
|
|
109
|
+
response = await axios_python.async_get("https://httpbin.org/delay/2")
|
|
110
|
+
print(response.data)
|
|
111
|
+
|
|
112
|
+
asyncio.run(fetch_data())
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### File Uploads
|
|
116
|
+
|
|
117
|
+
Multipart file uploads are supported out of the box matching the `requests` interface.
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
with open("report.csv", "rb") as f:
|
|
121
|
+
# Files can be passed as an open file handle or a tuple mapping
|
|
122
|
+
files = {"file": ("report.csv", f, "text/csv")}
|
|
123
|
+
response = axios_python.post("https://httpbin.org/post", files=files)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Streaming Responses
|
|
127
|
+
|
|
128
|
+
For large files or continuous data streams, use `stream=True`. The response is exposed as a context manager for both sync and async calls.
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
import axios_python
|
|
132
|
+
|
|
133
|
+
# Synchronous execution
|
|
134
|
+
with axios_python.get("https://httpbin.org/stream-bytes/100", stream=True) as response:
|
|
135
|
+
for chunk in response.iter_bytes(chunk_size=10):
|
|
136
|
+
print(len(chunk))
|
|
137
|
+
|
|
138
|
+
# Asynchronous execution in an async function
|
|
139
|
+
async with await axios_python.async_get("https://.../stream", stream=True) as response:
|
|
140
|
+
async for line in response.aiter_lines():
|
|
141
|
+
print(line)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
---
|
|
145
|
+
|
|
146
|
+
## Core Concepts
|
|
147
|
+
|
|
148
|
+
### Interceptors
|
|
149
|
+
|
|
150
|
+
Interceptors allow you to tap into the lifecycle of a request or response. They run sequentially.
|
|
151
|
+
|
|
152
|
+
```python
|
|
153
|
+
api = axios_python.create({"base_url": "https://api.myapp.com"})
|
|
154
|
+
|
|
155
|
+
# Add a request interceptor
|
|
156
|
+
def authorize_request(config):
|
|
157
|
+
config["headers"]["Authorization"] = "Bearer token123"
|
|
158
|
+
return config
|
|
159
|
+
|
|
160
|
+
api.interceptors.request.use(authorize_request)
|
|
161
|
+
|
|
162
|
+
# Add a response interceptor
|
|
163
|
+
def unwrap_data(response):
|
|
164
|
+
# Automatically unwrap the 'data' payload from the JSON
|
|
165
|
+
response.data = response.json().get("data", response.data)
|
|
166
|
+
return response
|
|
167
|
+
|
|
168
|
+
api.interceptors.response.use(unwrap_data)
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Middleware
|
|
172
|
+
|
|
173
|
+
For more complex logic that needs to "wrap" the entire request (like timing, distributed tracing, or custom caching), use the Express.js-style middleware pipeline.
|
|
174
|
+
|
|
175
|
+
```python
|
|
176
|
+
import time
|
|
177
|
+
|
|
178
|
+
async def logger_middleware(ctx, next_fn):
|
|
179
|
+
print(f"Starting {ctx.get('method')} to {ctx.get('url')}")
|
|
180
|
+
start = time.monotonic()
|
|
181
|
+
|
|
182
|
+
# Yield control to the next middleware / transport layer
|
|
183
|
+
result = await next_fn(ctx)
|
|
184
|
+
|
|
185
|
+
elapsed = time.monotonic() - start
|
|
186
|
+
print(f"Finished in {elapsed:.3f}s with status {result.status_code}")
|
|
187
|
+
|
|
188
|
+
return result
|
|
189
|
+
|
|
190
|
+
api.use(logger_middleware)
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Retry Engine
|
|
194
|
+
|
|
195
|
+
Temporary network issues shouldn't hard-crash your app. Provide a retry strategy when creating your client.
|
|
196
|
+
|
|
197
|
+
```python
|
|
198
|
+
from axios_python import ExponentialBackoff
|
|
199
|
+
|
|
200
|
+
api = axios_python.create({
|
|
201
|
+
"base_url": "https://httpbin.org",
|
|
202
|
+
"max_retries": 3,
|
|
203
|
+
"retry_strategy": ExponentialBackoff(base=1.0, multiplier=2.0, max_delay=10.0),
|
|
204
|
+
})
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
By default, this retries on Network Errors and Timeouts.
|
|
208
|
+
|
|
209
|
+
### Request Cancellation
|
|
210
|
+
|
|
211
|
+
Use a `CancelToken` to abort long-running requests or cancel requests when a user navigates away.
|
|
212
|
+
|
|
213
|
+
```python
|
|
214
|
+
from axios_python import CancelToken
|
|
215
|
+
import threading
|
|
216
|
+
import time
|
|
217
|
+
|
|
218
|
+
token = CancelToken()
|
|
219
|
+
|
|
220
|
+
def background_fetch():
|
|
221
|
+
try:
|
|
222
|
+
api.get("/delay/10", cancel_token=token)
|
|
223
|
+
except axios_python.CancelError as e:
|
|
224
|
+
print(f"Request aborted: {e}")
|
|
225
|
+
|
|
226
|
+
threading.Thread(target=background_fetch).start()
|
|
227
|
+
|
|
228
|
+
time.sleep(1)
|
|
229
|
+
token.cancel(reason="User clicked 'Stop'")
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
---
|
|
233
|
+
|
|
234
|
+
## Plugins
|
|
235
|
+
|
|
236
|
+
`axios_python` ships with first-party plugins for common use-cases.
|
|
237
|
+
|
|
238
|
+
### Authentication Plugin
|
|
239
|
+
|
|
240
|
+
Automatically injects `Authorization` headers. Supports static tokens or dynamic providers.
|
|
241
|
+
|
|
242
|
+
```python
|
|
243
|
+
from axios_python import AuthPlugin
|
|
244
|
+
|
|
245
|
+
api.plugin(AuthPlugin(scheme="Bearer", token="super-secret-key"))
|
|
246
|
+
|
|
247
|
+
# Or dynamically fetch it:
|
|
248
|
+
# api.plugin(AuthPlugin(token_provider=lambda: get_fresh_token()))
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### Cache Plugin
|
|
252
|
+
|
|
253
|
+
In-memory TTL cache for `GET` requests to reduce redundant network load.
|
|
254
|
+
|
|
255
|
+
```python
|
|
256
|
+
from axios_python import CachePlugin
|
|
257
|
+
|
|
258
|
+
# Cache GET responses for 120 seconds, max 256 items
|
|
259
|
+
api.plugin(CachePlugin(ttl=120, max_size=256))
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### Logger Plugin
|
|
263
|
+
|
|
264
|
+
Standardized `logging` for requests and responses out of the box.
|
|
265
|
+
|
|
266
|
+
```python
|
|
267
|
+
import logging
|
|
268
|
+
from axios_python import LoggerPlugin
|
|
269
|
+
|
|
270
|
+
logging.basicConfig(level=logging.INFO)
|
|
271
|
+
api.plugin(LoggerPlugin(level=logging.INFO))
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
---
|
|
275
|
+
|
|
276
|
+
## Configuration Reference
|
|
277
|
+
|
|
278
|
+
You can pass the following properties to `axios_python.create(config)` or as overrides to individual request methods (`api.get("/url", **kwargs)`):
|
|
279
|
+
|
|
280
|
+
| Property | Type | Description |
|
|
281
|
+
|----------|------|-------------|
|
|
282
|
+
| `base_url` | `str` | Base URL attached to relative paths. |
|
|
283
|
+
| `method` | `str` | HTTP Method (e.g., `"GET"`, `"POST"`). |
|
|
284
|
+
| `url` | `str` | The target path or absolute URL. |
|
|
285
|
+
| `headers` | `dict` | Dictionary of HTTP headers. |
|
|
286
|
+
| `params` | `dict` | URL Query parameters. |
|
|
287
|
+
| `data` | `Any` | Request body content (raw). |
|
|
288
|
+
| `json` | `Any` | Request body content (automatically serialized to JSON). |
|
|
289
|
+
| `files` | `Any` | Multipart-encoded files dictionary. |
|
|
290
|
+
| `stream` | `bool` | Stream the response (Default: False). |
|
|
291
|
+
| `timeout` | `float` | Max seconds to wait for a response (Default: 30). |
|
|
292
|
+
| `max_retries` | `int` | Maximum retry attempts on failure (Default: 0). |
|
|
293
|
+
| `retry_strategy` | `RetryStrategy` | Backoff class instance (e.g., `ExponentialBackoff`). |
|
|
294
|
+
| `cancel_token` | `CancelToken` | Token to cancel the request mid-flight. |
|
|
295
|
+
|
|
296
|
+
---
|
|
297
|
+
|
|
298
|
+
## Error Handling
|
|
299
|
+
|
|
300
|
+
`axios_python` provides strongly typed exceptions extending from `AxiosPythonError`. The `Response` object provides a `.raise_for_status()` method exactly like `requests`.
|
|
301
|
+
|
|
302
|
+
```python
|
|
303
|
+
import axios_python
|
|
304
|
+
|
|
305
|
+
try:
|
|
306
|
+
response = axios_python.get("https://httpbin.org/status/404")
|
|
307
|
+
response.raise_for_status()
|
|
308
|
+
except axios_python.HTTPStatusError as e:
|
|
309
|
+
print(f"Request failed with status code {e.response.status_code}")
|
|
310
|
+
except axios_python.TimeoutError:
|
|
311
|
+
print("Request timed out.")
|
|
312
|
+
except axios_python.NetworkError:
|
|
313
|
+
print("Unable to connect to the server.")
|
|
314
|
+
except axios_python.RetryError:
|
|
315
|
+
print("All retry attempts failed.")
|
|
316
|
+
except axios_python.CancelError:
|
|
317
|
+
print("Request was manually cancelled.")
|
|
318
|
+
except axios_python.AxiosPythonError as e:
|
|
319
|
+
print(f"A general axios_python error occurred: {e}")
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
---
|
|
323
|
+
|
|
324
|
+
## Advanced
|
|
325
|
+
|
|
326
|
+
### Custom Transports
|
|
327
|
+
|
|
328
|
+
You aren't locked into `httpx`. You can build a custom transport adapter by implementing the `BaseTransport` abstract class.
|
|
329
|
+
|
|
330
|
+
```python
|
|
331
|
+
from axios_python import AxiosPython, BaseTransport
|
|
332
|
+
|
|
333
|
+
class MockTransport(BaseTransport):
|
|
334
|
+
def send(self, request):
|
|
335
|
+
return axios_python.Response(200, {}, {"mock": "data"}, request)
|
|
336
|
+
|
|
337
|
+
async def send_async(self, request):
|
|
338
|
+
return self.send(request)
|
|
339
|
+
|
|
340
|
+
# Pass the custom transport directly to the AxiosPython constructor
|
|
341
|
+
api = AxiosPython(config={"base_url": "mock://"}, transport=MockTransport())
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
---
|
|
345
|
+
|
|
346
|
+
## License
|
|
347
|
+
|
|
348
|
+
This project is licensed under the [MIT License](LICENSE).
|