global-api-tools 0.1.1__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.
- global_api_tools-0.1.1/LICENSE +21 -0
- global_api_tools-0.1.1/PKG-INFO +405 -0
- global_api_tools-0.1.1/README.md +370 -0
- global_api_tools-0.1.1/pyproject.toml +61 -0
- global_api_tools-0.1.1/setup.cfg +4 -0
- global_api_tools-0.1.1/src/global_api_tools/__init__.py +27 -0
- global_api_tools-0.1.1/src/global_api_tools/_compat.py +19 -0
- global_api_tools-0.1.1/src/global_api_tools/exceptions.py +369 -0
- global_api_tools-0.1.1/src/global_api_tools/logging.py +63 -0
- global_api_tools-0.1.1/src/global_api_tools/responses.py +178 -0
- global_api_tools-0.1.1/src/global_api_tools.egg-info/PKG-INFO +405 -0
- global_api_tools-0.1.1/src/global_api_tools.egg-info/SOURCES.txt +13 -0
- global_api_tools-0.1.1/src/global_api_tools.egg-info/dependency_links.txt +1 -0
- global_api_tools-0.1.1/src/global_api_tools.egg-info/requires.txt +18 -0
- global_api_tools-0.1.1/src/global_api_tools.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Aniket Modi
|
|
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,405 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: global-api-tools
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Reusable API response and exception handling utilities with FastAPI integration.
|
|
5
|
+
Author: Aniket Modi
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/aniketmodi123/reusable_code_lib
|
|
8
|
+
Project-URL: Repository, https://github.com/aniketmodi123/reusable_code_lib
|
|
9
|
+
Keywords: api,fastapi,error-handling,responses,utilities
|
|
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.9
|
|
15
|
+
Classifier: Framework :: FastAPI
|
|
16
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
17
|
+
Requires-Python: >=3.9
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Provides-Extra: fastapi
|
|
21
|
+
Requires-Dist: fastapi>=0.110; extra == "fastapi"
|
|
22
|
+
Provides-Extra: pydantic
|
|
23
|
+
Requires-Dist: pydantic>=1.10; extra == "pydantic"
|
|
24
|
+
Provides-Extra: database
|
|
25
|
+
Requires-Dist: sqlalchemy>=1.4; extra == "database"
|
|
26
|
+
Requires-Dist: asyncpg>=0.29; extra == "database"
|
|
27
|
+
Requires-Dist: psycopg2-binary>=2.9; extra == "database"
|
|
28
|
+
Provides-Extra: full
|
|
29
|
+
Requires-Dist: fastapi>=0.110; extra == "full"
|
|
30
|
+
Requires-Dist: pydantic>=1.10; extra == "full"
|
|
31
|
+
Requires-Dist: sqlalchemy>=1.4; extra == "full"
|
|
32
|
+
Requires-Dist: asyncpg>=0.29; extra == "full"
|
|
33
|
+
Requires-Dist: psycopg2-binary>=2.9; extra == "full"
|
|
34
|
+
Dynamic: license-file
|
|
35
|
+
|
|
36
|
+
# global-api-tools
|
|
37
|
+
|
|
38
|
+
`global-api-tools` is a reusable Python library for centralized API responses, logging, and exception handling.
|
|
39
|
+
|
|
40
|
+
It gives you one place to manage:
|
|
41
|
+
|
|
42
|
+
- `create_response`
|
|
43
|
+
- `value_correction`
|
|
44
|
+
- `logs`
|
|
45
|
+
- `ExceptionHandler`
|
|
46
|
+
- `unified_exception_handler`
|
|
47
|
+
|
|
48
|
+
## Install
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pip install global-api-tools
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
For local development:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
pip install .
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Optional extras:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
pip install ".[fastapi]"
|
|
64
|
+
pip install ".[pydantic]"
|
|
65
|
+
pip install ".[database]"
|
|
66
|
+
pip install ".[full]"
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Quick usage
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
from fastapi import FastAPI
|
|
73
|
+
from global_api_tools import create_response, register_exception_handlers
|
|
74
|
+
|
|
75
|
+
app = FastAPI()
|
|
76
|
+
register_exception_handlers(app)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
@app.get("/health")
|
|
80
|
+
async def health():
|
|
81
|
+
return create_response(200, data={"status": "ok"})
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Public API
|
|
85
|
+
|
|
86
|
+
Import from the published package like this:
|
|
87
|
+
|
|
88
|
+
Use only this import style in other projects:
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
from global_api_tools import (
|
|
92
|
+
ApiError,
|
|
93
|
+
ErrorHandler,
|
|
94
|
+
ExceptionHandler,
|
|
95
|
+
create_response,
|
|
96
|
+
explain_error,
|
|
97
|
+
get_logger,
|
|
98
|
+
get_status_code,
|
|
99
|
+
handle_exception,
|
|
100
|
+
logs,
|
|
101
|
+
register_exception_handlers,
|
|
102
|
+
unified_exception_handler,
|
|
103
|
+
value_correction,
|
|
104
|
+
)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### `create_response`
|
|
108
|
+
|
|
109
|
+
Params:
|
|
110
|
+
|
|
111
|
+
- `response_code: int`
|
|
112
|
+
- `data: Any = None`
|
|
113
|
+
- `schema: Any | None = None`
|
|
114
|
+
- `pagination: Mapping[str, Any] | None = None`
|
|
115
|
+
- `error_message: str | None = None`
|
|
116
|
+
- `error_code: str | None = None`
|
|
117
|
+
- `details: Sequence[Mapping[str, Any]] | None = None`
|
|
118
|
+
- `as_json_response: bool = True`
|
|
119
|
+
|
|
120
|
+
Usage:
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
from global_api_tools import create_response
|
|
124
|
+
|
|
125
|
+
return create_response(
|
|
126
|
+
200,
|
|
127
|
+
data={"name": "Aniket"},
|
|
128
|
+
pagination={"page": 1, "rows": 10, "total_rows": 100},
|
|
129
|
+
)
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### `value_correction`
|
|
133
|
+
|
|
134
|
+
Params:
|
|
135
|
+
|
|
136
|
+
- `data: Any`
|
|
137
|
+
|
|
138
|
+
Usage:
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
from decimal import Decimal
|
|
142
|
+
from global_api_tools import value_correction
|
|
143
|
+
|
|
144
|
+
cleaned = value_correction({"amount": Decimal("10.50"), "name": " demo "})
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### `logs`
|
|
148
|
+
|
|
149
|
+
Params:
|
|
150
|
+
|
|
151
|
+
- `msg: object = ""`
|
|
152
|
+
- `type: str = "info"`
|
|
153
|
+
- `file_name: str | Path | None = None`
|
|
154
|
+
- `logger: logging.Logger | None = None`
|
|
155
|
+
|
|
156
|
+
Usage:
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
from global_api_tools import logs
|
|
160
|
+
|
|
161
|
+
logs("report created", type="info")
|
|
162
|
+
logs("database failed", type="error", file_name="logs/app")
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### `get_logger`
|
|
166
|
+
|
|
167
|
+
Params:
|
|
168
|
+
|
|
169
|
+
- `name: str = "global_api_tools"`
|
|
170
|
+
- `file_name: str | Path | None = None`
|
|
171
|
+
|
|
172
|
+
Usage:
|
|
173
|
+
|
|
174
|
+
```python
|
|
175
|
+
from global_api_tools import get_logger
|
|
176
|
+
|
|
177
|
+
logger = get_logger("my_app", file_name="logs/app.log")
|
|
178
|
+
logger.info("started")
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### `unified_exception_handler`
|
|
182
|
+
|
|
183
|
+
Params:
|
|
184
|
+
|
|
185
|
+
- `request`
|
|
186
|
+
- `exc: Exception`
|
|
187
|
+
|
|
188
|
+
Usage:
|
|
189
|
+
|
|
190
|
+
```python
|
|
191
|
+
from fastapi import FastAPI, HTTPException
|
|
192
|
+
from fastapi.exceptions import RequestValidationError
|
|
193
|
+
from global_api_tools import unified_exception_handler
|
|
194
|
+
|
|
195
|
+
app = FastAPI()
|
|
196
|
+
|
|
197
|
+
app.add_exception_handler(HTTPException, unified_exception_handler)
|
|
198
|
+
app.add_exception_handler(Exception, unified_exception_handler)
|
|
199
|
+
app.add_exception_handler(RequestValidationError, unified_exception_handler)
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### `register_exception_handlers`
|
|
203
|
+
|
|
204
|
+
Params:
|
|
205
|
+
|
|
206
|
+
- `app`
|
|
207
|
+
- `handler: ErrorHandler | None = None`
|
|
208
|
+
|
|
209
|
+
Usage:
|
|
210
|
+
|
|
211
|
+
```python
|
|
212
|
+
from fastapi import FastAPI
|
|
213
|
+
from global_api_tools import register_exception_handlers
|
|
214
|
+
|
|
215
|
+
app = FastAPI()
|
|
216
|
+
register_exception_handlers(app)
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### `ExceptionHandler`
|
|
220
|
+
|
|
221
|
+
Params:
|
|
222
|
+
|
|
223
|
+
- `exc: Exception`
|
|
224
|
+
|
|
225
|
+
Usage:
|
|
226
|
+
|
|
227
|
+
```python
|
|
228
|
+
from global_api_tools import ExceptionHandler
|
|
229
|
+
|
|
230
|
+
try:
|
|
231
|
+
raise ValueError("invalid meter id")
|
|
232
|
+
except Exception as exc:
|
|
233
|
+
ExceptionHandler(exc)
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### `handle_exception`
|
|
237
|
+
|
|
238
|
+
Params:
|
|
239
|
+
|
|
240
|
+
- `exc: Exception`
|
|
241
|
+
- `request = None`
|
|
242
|
+
|
|
243
|
+
Usage:
|
|
244
|
+
|
|
245
|
+
```python
|
|
246
|
+
from global_api_tools import handle_exception
|
|
247
|
+
|
|
248
|
+
payload_or_response = handle_exception(ValueError("invalid input"))
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### `explain_error`
|
|
252
|
+
|
|
253
|
+
Params:
|
|
254
|
+
|
|
255
|
+
- `exc: Exception`
|
|
256
|
+
|
|
257
|
+
Usage:
|
|
258
|
+
|
|
259
|
+
```python
|
|
260
|
+
from global_api_tools import explain_error
|
|
261
|
+
|
|
262
|
+
message = explain_error(ValueError("invalid input"))
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
### `get_status_code`
|
|
266
|
+
|
|
267
|
+
Params:
|
|
268
|
+
|
|
269
|
+
- `exc: Exception`
|
|
270
|
+
|
|
271
|
+
Usage:
|
|
272
|
+
|
|
273
|
+
```python
|
|
274
|
+
from global_api_tools import get_status_code
|
|
275
|
+
|
|
276
|
+
status_code = get_status_code(ValueError("invalid input"))
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
### `ErrorHandler`
|
|
280
|
+
|
|
281
|
+
Params:
|
|
282
|
+
|
|
283
|
+
- `logger_name: str = "global_api_tools.errors"`
|
|
284
|
+
|
|
285
|
+
Usage:
|
|
286
|
+
|
|
287
|
+
```python
|
|
288
|
+
from global_api_tools import ErrorHandler
|
|
289
|
+
|
|
290
|
+
handler = ErrorHandler()
|
|
291
|
+
payload = handler.build_payload(ValueError("invalid input"))
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
### `ApiError`
|
|
295
|
+
|
|
296
|
+
Params:
|
|
297
|
+
|
|
298
|
+
- `message: str`
|
|
299
|
+
- `status_code: int = 400`
|
|
300
|
+
- `code: str = "api_error"`
|
|
301
|
+
- `details: list[dict[str, Any]] | None = None`
|
|
302
|
+
- `log_message: str | None = None`
|
|
303
|
+
|
|
304
|
+
Usage:
|
|
305
|
+
|
|
306
|
+
```python
|
|
307
|
+
from global_api_tools import ApiError
|
|
308
|
+
|
|
309
|
+
raise ApiError(
|
|
310
|
+
"Report is not ready.",
|
|
311
|
+
status_code=409,
|
|
312
|
+
code="report_pending",
|
|
313
|
+
details=[{"field": "report_id", "message": "still processing"}],
|
|
314
|
+
)
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
## Error response format
|
|
318
|
+
|
|
319
|
+
Every API failure uses one consistent structure:
|
|
320
|
+
|
|
321
|
+
```json
|
|
322
|
+
{
|
|
323
|
+
"success": false,
|
|
324
|
+
"status_code": 422,
|
|
325
|
+
"error": {
|
|
326
|
+
"code": "validation_error",
|
|
327
|
+
"type": "RequestValidationError",
|
|
328
|
+
"message": "One or more fields are invalid.",
|
|
329
|
+
"details": [
|
|
330
|
+
{
|
|
331
|
+
"field": "email",
|
|
332
|
+
"message": "field required"
|
|
333
|
+
}
|
|
334
|
+
]
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
## Package structure
|
|
340
|
+
|
|
341
|
+
```text
|
|
342
|
+
src/global_api_tools/
|
|
343
|
+
__init__.py
|
|
344
|
+
_compat.py
|
|
345
|
+
exceptions.py
|
|
346
|
+
logging.py
|
|
347
|
+
responses.py
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
## Extending it
|
|
351
|
+
|
|
352
|
+
Add new shared functionality inside `src/global_api_tools/` and re-export it from `src/global_api_tools/__init__.py`. That keeps imports stable across every project that uses the package.
|
|
353
|
+
|
|
354
|
+
## Import Rule
|
|
355
|
+
|
|
356
|
+
For installed usage, import from `global_api_tools` only.
|
|
357
|
+
|
|
358
|
+
Correct:
|
|
359
|
+
|
|
360
|
+
```python
|
|
361
|
+
from global_api_tools import create_response, unified_exception_handler
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
Do not rely on local-only paths like `utils` or `exception_handler` in other projects.
|
|
365
|
+
|
|
366
|
+
## Auto Publish From `prod`
|
|
367
|
+
|
|
368
|
+
This repo is configured so that a merged pull request into the `prod` branch publishes the package to PyPI through GitHub Actions.
|
|
369
|
+
|
|
370
|
+
Required setup:
|
|
371
|
+
|
|
372
|
+
1. Create the package on PyPI if needed.
|
|
373
|
+
2. In PyPI, configure a Trusted Publisher with these values:
|
|
374
|
+
- Project name: `global-api-tools`
|
|
375
|
+
- Owner: `aniketmodi123`
|
|
376
|
+
- Repository: `reusable_code_lib`
|
|
377
|
+
- Workflow: `publish-pypi.yml`
|
|
378
|
+
3. Before merging into `prod`, increase the version in `pyproject.toml`. PyPI will reject duplicate versions.
|
|
379
|
+
|
|
380
|
+
Workflow file:
|
|
381
|
+
|
|
382
|
+
- `.github/workflows/publish-pypi.yml`
|
|
383
|
+
|
|
384
|
+
Release flow:
|
|
385
|
+
|
|
386
|
+
```bash
|
|
387
|
+
git checkout dev
|
|
388
|
+
# make code changes
|
|
389
|
+
# update version in pyproject.toml when this is a release
|
|
390
|
+
git add .
|
|
391
|
+
git commit -m "release: 0.1.1"
|
|
392
|
+
git push origin dev
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
Then:
|
|
396
|
+
|
|
397
|
+
1. Create a pull request from `dev` to `prod`
|
|
398
|
+
2. Review and approve it
|
|
399
|
+
3. Merge the pull request
|
|
400
|
+
|
|
401
|
+
When the PR is merged into `prod`, GitHub Actions will use Trusted Publishing to:
|
|
402
|
+
|
|
403
|
+
- build the wheel and source distribution
|
|
404
|
+
- validate the package
|
|
405
|
+
- upload it to PyPI
|