pfmsoft-api-request 0.1.3__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.
- pfmsoft_api_request-0.1.3/LICENSE +21 -0
- pfmsoft_api_request-0.1.3/PKG-INFO +171 -0
- pfmsoft_api_request-0.1.3/README.md +150 -0
- pfmsoft_api_request-0.1.3/pyproject.toml +80 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/__init__.py +37 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/cache/__init__.py +36 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/cache/memory_cache.py +177 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/cache/metadata_helpers.py +55 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/cache/models.py +83 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/cache/protocols.py +166 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/cache/sqlite_cache/__init__.py +7 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/cache/sqlite_cache/connection_helpers.py +126 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/cache/sqlite_cache/query_helpers.py +107 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/cache/sqlite_cache/sqlite_cache.py +233 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/cache/sqlite_cache/table_definitions.sql +23 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/cli/__init__.py +17 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/cli/cache/__init__.py +12 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/cli/cache/info.py +11 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/cli/helpers.py +41 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/cli/main_typer.py +52 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/cli/request/__init__.py +11 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/cli/request/request.py +222 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/cli/request/validate.py +11 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/helpers/http_session_factory.py +80 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/helpers/json_io.py +256 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/helpers/save_text_file.py +43 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/helpers/whenever/__init__.py +4 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/helpers/whenever/instant_helpers.py +41 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/logging_config.py +202 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/py.typed +0 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/rate_limit/__init__.py +24 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/rate_limit/aio_limiter.py +89 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/rate_limit/protocols.py +101 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/request/__init__.py +11 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/request/api_requester.py +681 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/request/intermediate_models.py +143 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/request/models.py +355 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/request/protocols.py +54 -0
- pfmsoft_api_request-0.1.3/src/pfmsoft/api_request/settings.py +65 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Chad Lowe
|
|
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,171 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pfmsoft-api-request
|
|
3
|
+
Version: 0.1.3
|
|
4
|
+
Summary: A command line api request tool with caching and rate limiting.
|
|
5
|
+
Author: Chad Lowe
|
|
6
|
+
Author-email: Chad Lowe <pfmsoft.dev@gmail.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: aiolimiter>=1.2.1
|
|
10
|
+
Requires-Dist: httpx2>=2.5.0
|
|
11
|
+
Requires-Dist: pfmsoft-eve-snippets>=0.1.4
|
|
12
|
+
Requires-Dist: pydantic>=2.13.4
|
|
13
|
+
Requires-Dist: pydantic-settings>=2.14.2
|
|
14
|
+
Requires-Dist: typer>=0.26.8
|
|
15
|
+
Requires-Dist: whenever>=0.10.2
|
|
16
|
+
Requires-Python: >=3.14
|
|
17
|
+
Project-URL: Documentation, https://github.com/DonalChilde/pfmsoft-api-request#readme
|
|
18
|
+
Project-URL: Issues, https://github.com/DonalChilde/pfmsoft-api-request/issues
|
|
19
|
+
Project-URL: Source, https://github.com/DonalChilde/pfmsoft-api-request
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# api-request
|
|
23
|
+
|
|
24
|
+
`api-request` is an API-first Python project for executing batched HTTP requests with:
|
|
25
|
+
|
|
26
|
+
- async request orchestration
|
|
27
|
+
- optional SQLite-backed caching
|
|
28
|
+
- configurable rate limiting
|
|
29
|
+
- JSON in / JSON out workflows
|
|
30
|
+
|
|
31
|
+
It includes a simple CLI for running request batches from stdin or files, and a Python API for integrating the same behavior in application code.
|
|
32
|
+
|
|
33
|
+
## Project Status
|
|
34
|
+
|
|
35
|
+
- Package manager: `uv`
|
|
36
|
+
- Distribution: source-only for now (no PyPI release yet)
|
|
37
|
+
- Python: `>=3.14`
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
Clone the repository and sync dependencies with `uv`:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
uv sync
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
This creates/updates the local `.venv` and installs app + dev dependencies.
|
|
48
|
+
|
|
49
|
+
## Quick Start (CLI)
|
|
50
|
+
|
|
51
|
+
Show CLI help:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
uv run api-request --help
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Run a batch from stdin and print plain JSON to stdout:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
cat requests.json | uv run api-request --from - --to - --plain --indent 2
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Run from file and write to file:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
uv run api-request --from requests.json --to responses.json --overwrite --indent 2
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Show version and resolved runtime directories:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
uv run api-request --version
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Input JSON Shape
|
|
76
|
+
|
|
77
|
+
The CLI expects a JSON object keyed by request UUID. Each value is a request definition.
|
|
78
|
+
|
|
79
|
+
Minimal example:
|
|
80
|
+
|
|
81
|
+
```json
|
|
82
|
+
{
|
|
83
|
+
"00000000-0000-0000-0000-000000000001": {
|
|
84
|
+
"url": "https://esi.evetech.net/status/",
|
|
85
|
+
"method": "GET"
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Supported request fields include:
|
|
91
|
+
|
|
92
|
+
- `url` (required)
|
|
93
|
+
- `method` (required)
|
|
94
|
+
- `headers` (optional map)
|
|
95
|
+
- `body` (optional)
|
|
96
|
+
- `parameters` (optional query param map)
|
|
97
|
+
- `cache_key` (optional UUID)
|
|
98
|
+
- `rate_key` (optional string)
|
|
99
|
+
|
|
100
|
+
### Output JSON Shape
|
|
101
|
+
|
|
102
|
+
The CLI returns a JSON object with:
|
|
103
|
+
|
|
104
|
+
- `successful`: map of request UUID -> response
|
|
105
|
+
- `failed`: map of request UUID -> failed response
|
|
106
|
+
|
|
107
|
+
## Python API Usage
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
import asyncio
|
|
111
|
+
from uuid import uuid4
|
|
112
|
+
|
|
113
|
+
from api_request import ApiRequester, Request
|
|
114
|
+
from api_request.cache import InMemoryCache
|
|
115
|
+
from api_request.rate_limit import AiolimiterRateLimiterFactory
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
async def main() -> None:
|
|
119
|
+
request = Request(
|
|
120
|
+
request_key=uuid4(),
|
|
121
|
+
method="GET",
|
|
122
|
+
url="https://esi.evetech.net/status/",
|
|
123
|
+
cache_key=uuid4(),
|
|
124
|
+
rate_key="esi-status",
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
async with ApiRequester(
|
|
128
|
+
cache_factory=InMemoryCache,
|
|
129
|
+
rate_limiter_factory=AiolimiterRateLimiterFactory(
|
|
130
|
+
max_rate=50.0,
|
|
131
|
+
time_period=60.0,
|
|
132
|
+
),
|
|
133
|
+
) as requester:
|
|
134
|
+
responses = await requester.process_requests({request.request_key: request})
|
|
135
|
+
print(responses)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
if __name__ == "__main__":
|
|
139
|
+
asyncio.run(main())
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Configuration
|
|
143
|
+
|
|
144
|
+
Runtime settings use environment variables with the `API_REQUEST_` prefix.
|
|
145
|
+
|
|
146
|
+
- `API_REQUEST_APPLICATION_DIRECTORY`: overrides the app directory used for cache and logs.
|
|
147
|
+
|
|
148
|
+
You can also set this from the CLI with `--application-directory`.
|
|
149
|
+
|
|
150
|
+
## Development
|
|
151
|
+
|
|
152
|
+
Common commands:
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
uv sync
|
|
156
|
+
uv run ruff format
|
|
157
|
+
uv run ruff check
|
|
158
|
+
uv run pytest
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Repository Layout
|
|
162
|
+
|
|
163
|
+
- `src/api_request/`: package source
|
|
164
|
+
- `src/api_request/cli/`: Typer CLI entrypoints
|
|
165
|
+
- `src/api_request/request/`: request orchestration and models
|
|
166
|
+
- `src/api_request/cache/`: cache implementations and protocols
|
|
167
|
+
- `tests/`: test suite
|
|
168
|
+
|
|
169
|
+
## License
|
|
170
|
+
|
|
171
|
+
MIT. See `LICENSE`.
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# api-request
|
|
2
|
+
|
|
3
|
+
`api-request` is an API-first Python project for executing batched HTTP requests with:
|
|
4
|
+
|
|
5
|
+
- async request orchestration
|
|
6
|
+
- optional SQLite-backed caching
|
|
7
|
+
- configurable rate limiting
|
|
8
|
+
- JSON in / JSON out workflows
|
|
9
|
+
|
|
10
|
+
It includes a simple CLI for running request batches from stdin or files, and a Python API for integrating the same behavior in application code.
|
|
11
|
+
|
|
12
|
+
## Project Status
|
|
13
|
+
|
|
14
|
+
- Package manager: `uv`
|
|
15
|
+
- Distribution: source-only for now (no PyPI release yet)
|
|
16
|
+
- Python: `>=3.14`
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
Clone the repository and sync dependencies with `uv`:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
uv sync
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
This creates/updates the local `.venv` and installs app + dev dependencies.
|
|
27
|
+
|
|
28
|
+
## Quick Start (CLI)
|
|
29
|
+
|
|
30
|
+
Show CLI help:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
uv run api-request --help
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Run a batch from stdin and print plain JSON to stdout:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
cat requests.json | uv run api-request --from - --to - --plain --indent 2
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Run from file and write to file:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
uv run api-request --from requests.json --to responses.json --overwrite --indent 2
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Show version and resolved runtime directories:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
uv run api-request --version
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Input JSON Shape
|
|
55
|
+
|
|
56
|
+
The CLI expects a JSON object keyed by request UUID. Each value is a request definition.
|
|
57
|
+
|
|
58
|
+
Minimal example:
|
|
59
|
+
|
|
60
|
+
```json
|
|
61
|
+
{
|
|
62
|
+
"00000000-0000-0000-0000-000000000001": {
|
|
63
|
+
"url": "https://esi.evetech.net/status/",
|
|
64
|
+
"method": "GET"
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Supported request fields include:
|
|
70
|
+
|
|
71
|
+
- `url` (required)
|
|
72
|
+
- `method` (required)
|
|
73
|
+
- `headers` (optional map)
|
|
74
|
+
- `body` (optional)
|
|
75
|
+
- `parameters` (optional query param map)
|
|
76
|
+
- `cache_key` (optional UUID)
|
|
77
|
+
- `rate_key` (optional string)
|
|
78
|
+
|
|
79
|
+
### Output JSON Shape
|
|
80
|
+
|
|
81
|
+
The CLI returns a JSON object with:
|
|
82
|
+
|
|
83
|
+
- `successful`: map of request UUID -> response
|
|
84
|
+
- `failed`: map of request UUID -> failed response
|
|
85
|
+
|
|
86
|
+
## Python API Usage
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
import asyncio
|
|
90
|
+
from uuid import uuid4
|
|
91
|
+
|
|
92
|
+
from api_request import ApiRequester, Request
|
|
93
|
+
from api_request.cache import InMemoryCache
|
|
94
|
+
from api_request.rate_limit import AiolimiterRateLimiterFactory
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
async def main() -> None:
|
|
98
|
+
request = Request(
|
|
99
|
+
request_key=uuid4(),
|
|
100
|
+
method="GET",
|
|
101
|
+
url="https://esi.evetech.net/status/",
|
|
102
|
+
cache_key=uuid4(),
|
|
103
|
+
rate_key="esi-status",
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
async with ApiRequester(
|
|
107
|
+
cache_factory=InMemoryCache,
|
|
108
|
+
rate_limiter_factory=AiolimiterRateLimiterFactory(
|
|
109
|
+
max_rate=50.0,
|
|
110
|
+
time_period=60.0,
|
|
111
|
+
),
|
|
112
|
+
) as requester:
|
|
113
|
+
responses = await requester.process_requests({request.request_key: request})
|
|
114
|
+
print(responses)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
if __name__ == "__main__":
|
|
118
|
+
asyncio.run(main())
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Configuration
|
|
122
|
+
|
|
123
|
+
Runtime settings use environment variables with the `API_REQUEST_` prefix.
|
|
124
|
+
|
|
125
|
+
- `API_REQUEST_APPLICATION_DIRECTORY`: overrides the app directory used for cache and logs.
|
|
126
|
+
|
|
127
|
+
You can also set this from the CLI with `--application-directory`.
|
|
128
|
+
|
|
129
|
+
## Development
|
|
130
|
+
|
|
131
|
+
Common commands:
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
uv sync
|
|
135
|
+
uv run ruff format
|
|
136
|
+
uv run ruff check
|
|
137
|
+
uv run pytest
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Repository Layout
|
|
141
|
+
|
|
142
|
+
- `src/api_request/`: package source
|
|
143
|
+
- `src/api_request/cli/`: Typer CLI entrypoints
|
|
144
|
+
- `src/api_request/request/`: request orchestration and models
|
|
145
|
+
- `src/api_request/cache/`: cache implementations and protocols
|
|
146
|
+
- `tests/`: test suite
|
|
147
|
+
|
|
148
|
+
## License
|
|
149
|
+
|
|
150
|
+
MIT. See `LICENSE`.
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "pfmsoft-api-request"
|
|
3
|
+
version = "0.1.3"
|
|
4
|
+
description = "A command line api request tool with caching and rate limiting."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [{ name = "Chad Lowe", email = "pfmsoft.dev@gmail.com" }]
|
|
7
|
+
requires-python = ">=3.14"
|
|
8
|
+
dependencies = [
|
|
9
|
+
"aiolimiter>=1.2.1",
|
|
10
|
+
"httpx2>=2.5.0",
|
|
11
|
+
"pfmsoft-eve-snippets>=0.1.4",
|
|
12
|
+
"pydantic>=2.13.4",
|
|
13
|
+
"pydantic-settings>=2.14.2",
|
|
14
|
+
"typer>=0.26.8",
|
|
15
|
+
"whenever>=0.10.2",
|
|
16
|
+
]
|
|
17
|
+
# https://packaging.python.org/en/latest/specifications/license-expression/#specification
|
|
18
|
+
license = "MIT"
|
|
19
|
+
# https://packaging.python.org/en/latest/specifications/pyproject-toml/#license-files
|
|
20
|
+
license-files = ["LICENSE"]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
[project.scripts]
|
|
24
|
+
# script entry-points
|
|
25
|
+
# COMMAND_NAME = "PATH.TO.ENTRY.MODULE:ENTRY_FUNCTION"
|
|
26
|
+
|
|
27
|
+
api-request = "pfmsoft.api_request.cli.main_typer:app"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
[build-system]
|
|
31
|
+
requires = ["uv_build>=0.11.21,<0.12.0"]
|
|
32
|
+
build-backend = "uv_build"
|
|
33
|
+
|
|
34
|
+
# For a namespaced project
|
|
35
|
+
[tool.uv.build-backend]
|
|
36
|
+
module-name = "pfmsoft.api_request"
|
|
37
|
+
|
|
38
|
+
# Don't wait for internal package updates.
|
|
39
|
+
[tool.uv.exclude-newer-package]
|
|
40
|
+
pfmsoft-eve-snippets = false
|
|
41
|
+
|
|
42
|
+
[dependency-groups]
|
|
43
|
+
dev = [
|
|
44
|
+
"pytest>=9.1.1",
|
|
45
|
+
"pytest-cov>=7.1.0",
|
|
46
|
+
"ruff>=0.15.21",
|
|
47
|
+
]
|
|
48
|
+
[project.urls]
|
|
49
|
+
Documentation = "https://github.com/DonalChilde/pfmsoft-api-request#readme"
|
|
50
|
+
# Documentation_rtd = "https://esi-link.readthedocs.io"
|
|
51
|
+
Issues = "https://github.com/DonalChilde/pfmsoft-api-request/issues"
|
|
52
|
+
Source = "https://github.com/DonalChilde/pfmsoft-api-request"
|
|
53
|
+
|
|
54
|
+
[tool.uv]
|
|
55
|
+
exclude-newer = "1 week"
|
|
56
|
+
|
|
57
|
+
[tool.pytest.ini_options]
|
|
58
|
+
pythonpath = "src"
|
|
59
|
+
log_file = "dev/pytest-logs/tests.log"
|
|
60
|
+
log_file_date_format = "%Y-%m-%dT%H:%M:%S.%f%z"
|
|
61
|
+
log_file_format = "%(asctime)s %(levelname)s:%(funcName)s: %(message)s [in %(pathname)s:%(lineno)d]"
|
|
62
|
+
log_file_level = "INFO"
|
|
63
|
+
|
|
64
|
+
[tool.coverage.run]
|
|
65
|
+
branch = true
|
|
66
|
+
parallel = true
|
|
67
|
+
|
|
68
|
+
[tool.ruff.lint]
|
|
69
|
+
select = ["B", "UP", "D", "FIX", "I", "F401"]
|
|
70
|
+
# non-imperative-mood (D401)
|
|
71
|
+
ignore = ["D401", "D101", "D421"]
|
|
72
|
+
# extend-select = ["I"]
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
[tool.ruff.lint.pydocstyle]
|
|
76
|
+
convention = "google"
|
|
77
|
+
|
|
78
|
+
[tool.ruff.format]
|
|
79
|
+
docstring-code-format = true
|
|
80
|
+
docstring-code-line-length = 88
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""api-request: A command line first interface to the Eve Online API."""
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import version
|
|
4
|
+
|
|
5
|
+
__project_namespace__ = "pfmsoft"
|
|
6
|
+
__author__ = "Chad Lowe"
|
|
7
|
+
__email__ = "pfmsoft.dev@gmail.com"
|
|
8
|
+
__app_name__ = "pfmsoft-api-request"
|
|
9
|
+
"""Name of this module is defined here for consitency, used in determining the app_dir
|
|
10
|
+
and other paths."""
|
|
11
|
+
__description__ = "A command line first interface to the Eve Online API"
|
|
12
|
+
__version__ = version(__app_name__)
|
|
13
|
+
__release__ = __version__
|
|
14
|
+
__url__ = "https://github.com/DonalChilde/pfmsoft-api-request"
|
|
15
|
+
__license__ = "MIT"
|
|
16
|
+
|
|
17
|
+
from .request.api_requester import ApiRequester
|
|
18
|
+
from .request.models import (
|
|
19
|
+
FailedResponse,
|
|
20
|
+
Request,
|
|
21
|
+
Requests,
|
|
22
|
+
Response,
|
|
23
|
+
ResponseMetadata,
|
|
24
|
+
Responses,
|
|
25
|
+
Source,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
__all__ = [
|
|
29
|
+
"ApiRequester",
|
|
30
|
+
"FailedResponse",
|
|
31
|
+
"Request",
|
|
32
|
+
"Requests",
|
|
33
|
+
"Response",
|
|
34
|
+
"ResponseMetadata",
|
|
35
|
+
"Responses",
|
|
36
|
+
"Source",
|
|
37
|
+
]
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""Cache implementations and helpers for request orchestration.
|
|
2
|
+
|
|
3
|
+
Exports:
|
|
4
|
+
- `InMemoryCache` and `InMemoryCacheFactory` for ephemeral, process-local
|
|
5
|
+
caching.
|
|
6
|
+
- `SqliteCache` and `SqliteCacheFactory` for persistent SQLite-backed
|
|
7
|
+
caching.
|
|
8
|
+
- `merge_cached_revalidation_metadata` for `304 Not Modified`
|
|
9
|
+
revalidation merge semantics.
|
|
10
|
+
|
|
11
|
+
Typical usage:
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
from api_request import ApiRequester
|
|
15
|
+
from api_request.cache import SqliteCacheFactory
|
|
16
|
+
from api_request.rate_limit import AiolimiterRateLimiterFactory
|
|
17
|
+
|
|
18
|
+
async with ApiRequester(
|
|
19
|
+
cache_factory=SqliteCacheFactory("./.cache/api-request.sqlite3"),
|
|
20
|
+
rate_limiter_factory=AiolimiterRateLimiterFactory(max_rate=100.0),
|
|
21
|
+
) as requester:
|
|
22
|
+
...
|
|
23
|
+
```
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
from .memory_cache import InMemoryCache, InMemoryCacheFactory
|
|
27
|
+
from .metadata_helpers import merge_cached_revalidation_metadata
|
|
28
|
+
from .sqlite_cache.sqlite_cache import SqliteCache, SqliteCacheFactory
|
|
29
|
+
|
|
30
|
+
__all__ = [
|
|
31
|
+
"InMemoryCache",
|
|
32
|
+
"merge_cached_revalidation_metadata",
|
|
33
|
+
"SqliteCache",
|
|
34
|
+
"InMemoryCacheFactory",
|
|
35
|
+
"SqliteCacheFactory",
|
|
36
|
+
]
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"""In-memory cache implementations for API requests.
|
|
2
|
+
|
|
3
|
+
These caches are primarily intended for tests and local development where a
|
|
4
|
+
persistent backing store is unnecessary.
|
|
5
|
+
|
|
6
|
+
Behavior summary:
|
|
7
|
+
- Storage is process-local and non-durable.
|
|
8
|
+
- `set` performs upsert semantics.
|
|
9
|
+
- `update_304` preserves cached body text and merges metadata.
|
|
10
|
+
- `flush` is a no-op.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from types import TracebackType
|
|
14
|
+
from typing import Self
|
|
15
|
+
from uuid import UUID
|
|
16
|
+
|
|
17
|
+
from whenever import Instant
|
|
18
|
+
|
|
19
|
+
from ..request.models import ResponseMetadata
|
|
20
|
+
from .metadata_helpers import merge_cached_revalidation_metadata
|
|
21
|
+
from .models import CachedResponse, CacheInfo
|
|
22
|
+
from .protocols import CacheFactoryProtocol, CacheProtocol
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class InMemoryCache(CacheProtocol):
|
|
26
|
+
"""Dictionary-backed cache implementation.
|
|
27
|
+
|
|
28
|
+
The cache stores `CachedResponse` objects in memory and performs no I/O.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
def __init__(self) -> None:
|
|
32
|
+
"""Initialize an empty in-memory cache."""
|
|
33
|
+
self._entries: dict[UUID, CachedResponse] = {}
|
|
34
|
+
|
|
35
|
+
async def __aenter__(self) -> Self:
|
|
36
|
+
"""Enter the asynchronous context manager."""
|
|
37
|
+
return self
|
|
38
|
+
|
|
39
|
+
async def __aexit__(
|
|
40
|
+
self,
|
|
41
|
+
exc_type: type[BaseException] | None,
|
|
42
|
+
exc_value: BaseException | None,
|
|
43
|
+
traceback: TracebackType | None,
|
|
44
|
+
) -> None:
|
|
45
|
+
"""Exit the asynchronous context manager."""
|
|
46
|
+
_ = exc_type, exc_value, traceback
|
|
47
|
+
|
|
48
|
+
async def get(self, cache_key: UUID) -> CachedResponse | None:
|
|
49
|
+
"""Get a cached response by key, or None when missing."""
|
|
50
|
+
return self._entries.get(cache_key)
|
|
51
|
+
|
|
52
|
+
@staticmethod
|
|
53
|
+
def _ensure_validators(metadata: ResponseMetadata) -> None:
|
|
54
|
+
"""Ensure at least one cache validator is present."""
|
|
55
|
+
if metadata.etag is None and metadata.last_modified is None:
|
|
56
|
+
raise ValueError("Cached responses require etag or last_modified")
|
|
57
|
+
|
|
58
|
+
@staticmethod
|
|
59
|
+
def _build_cached_response(
|
|
60
|
+
*,
|
|
61
|
+
cache_key: UUID,
|
|
62
|
+
text: str,
|
|
63
|
+
metadata: ResponseMetadata,
|
|
64
|
+
) -> CachedResponse:
|
|
65
|
+
"""Build a CachedResponse from response text and metadata."""
|
|
66
|
+
InMemoryCache._ensure_validators(metadata)
|
|
67
|
+
return CachedResponse(
|
|
68
|
+
cache_key=cache_key,
|
|
69
|
+
response_text=text,
|
|
70
|
+
response_metadata_json=metadata.serialize(),
|
|
71
|
+
etag=metadata.etag,
|
|
72
|
+
last_modified=metadata.last_modified,
|
|
73
|
+
expires_at=metadata.expires_at,
|
|
74
|
+
cache_timestamp=Instant.now().timestamp_nanos(),
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
async def set(
|
|
78
|
+
self, cache_key: UUID, text: str, metadata: ResponseMetadata
|
|
79
|
+
) -> CachedResponse:
|
|
80
|
+
"""Create or replace a cached response entry.
|
|
81
|
+
|
|
82
|
+
Raises:
|
|
83
|
+
ValueError: If both metadata validators are absent.
|
|
84
|
+
"""
|
|
85
|
+
cached_response = self._build_cached_response(
|
|
86
|
+
cache_key=cache_key,
|
|
87
|
+
text=text,
|
|
88
|
+
metadata=metadata,
|
|
89
|
+
)
|
|
90
|
+
self._entries[cache_key] = cached_response
|
|
91
|
+
return cached_response
|
|
92
|
+
|
|
93
|
+
async def update_304(
|
|
94
|
+
self, cache_key: UUID, metadata: ResponseMetadata
|
|
95
|
+
) -> CachedResponse:
|
|
96
|
+
"""Refresh a stale entry from 304 metadata while preserving body text.
|
|
97
|
+
|
|
98
|
+
Raises:
|
|
99
|
+
KeyError: If no existing entry is present.
|
|
100
|
+
ValueError: If merged metadata lacks both validators.
|
|
101
|
+
"""
|
|
102
|
+
existing = self._entries.get(cache_key)
|
|
103
|
+
if existing is None:
|
|
104
|
+
raise KeyError(f"No cached response found for key {cache_key}")
|
|
105
|
+
|
|
106
|
+
existing_metadata = ResponseMetadata.deserialize(
|
|
107
|
+
existing.response_metadata_json
|
|
108
|
+
)
|
|
109
|
+
merged_metadata = merge_cached_revalidation_metadata(
|
|
110
|
+
cached=existing_metadata,
|
|
111
|
+
refreshed=metadata,
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
cached_response = self._build_cached_response(
|
|
115
|
+
cache_key=cache_key,
|
|
116
|
+
text=existing.response_text,
|
|
117
|
+
metadata=merged_metadata,
|
|
118
|
+
)
|
|
119
|
+
self._entries[cache_key] = cached_response
|
|
120
|
+
return cached_response
|
|
121
|
+
|
|
122
|
+
async def delete(self, cache_key: UUID) -> None:
|
|
123
|
+
"""Delete a cached response from the cache.
|
|
124
|
+
|
|
125
|
+
This operation is idempotent and does not raise for missing keys.
|
|
126
|
+
"""
|
|
127
|
+
self._entries.pop(cache_key, None)
|
|
128
|
+
|
|
129
|
+
async def clear(
|
|
130
|
+
self, only_expired: bool = False, age_limit: int | None = None
|
|
131
|
+
) -> None:
|
|
132
|
+
"""Clear cached entries matching the requested filters.
|
|
133
|
+
|
|
134
|
+
Args:
|
|
135
|
+
only_expired: When true, remove only expired entries.
|
|
136
|
+
age_limit: When provided, remove entries with `cache_age` greater
|
|
137
|
+
than or equal to this nanosecond threshold.
|
|
138
|
+
|
|
139
|
+
Notes:
|
|
140
|
+
When both filters are provided, entries must satisfy both.
|
|
141
|
+
"""
|
|
142
|
+
if not only_expired and age_limit is None:
|
|
143
|
+
self._entries.clear()
|
|
144
|
+
return
|
|
145
|
+
|
|
146
|
+
removable_keys: list[UUID] = []
|
|
147
|
+
for cache_key, cached_response in self._entries.items():
|
|
148
|
+
is_expired_match = not only_expired or cached_response.is_expired
|
|
149
|
+
is_age_match = age_limit is None or cached_response.cache_age >= age_limit
|
|
150
|
+
if is_expired_match and is_age_match:
|
|
151
|
+
removable_keys.append(cache_key)
|
|
152
|
+
|
|
153
|
+
for cache_key in removable_keys:
|
|
154
|
+
del self._entries[cache_key]
|
|
155
|
+
|
|
156
|
+
async def flush(self) -> None:
|
|
157
|
+
"""Flush the cache.
|
|
158
|
+
|
|
159
|
+
This implementation has no buffered writes, so flush is a no-op.
|
|
160
|
+
"""
|
|
161
|
+
return None
|
|
162
|
+
|
|
163
|
+
async def cache_info(self) -> CacheInfo:
|
|
164
|
+
"""Return summary information about the cache."""
|
|
165
|
+
return CacheInfo(size=len(self._entries))
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
class InMemoryCacheFactory(CacheFactoryProtocol):
|
|
169
|
+
"""Factory for creating InMemoryCache instances.
|
|
170
|
+
|
|
171
|
+
The InMemoryCache class takes no arguments, so this factory simply returns a new
|
|
172
|
+
instance of InMemoryCache.
|
|
173
|
+
"""
|
|
174
|
+
|
|
175
|
+
def __call__(self) -> CacheProtocol:
|
|
176
|
+
"""Create and return a new in-memory cache instance."""
|
|
177
|
+
return InMemoryCache()
|