No project description provided
Project description
jsend-python
This is a package to response jsend-like format
requirements
pip install django djangorestframework
Installation
pip install git+https://github.com/Visapick-Team/jsend-python.git
Response
Django
settings.py
INSTALLED_APPS = [
...
"jsend2",
]
REST_FRAMEWORK = {
# jsend2 limit/offset pagination
"DEFAULT_PAGINATION_CLASS": "jsend2.django.utils.JSendLimitOffsetPagination",
# jsend2 response
"DEFAULT_RENDERER_CLASSES": [
"jsend2.django.utils.JSendRenderer",
],
}
output
{
"status": "faild",
"message": "Any message",
"data": {
...
},
"code": "-100",
"total": 100,
"offset": 12
}
status: str
- required
- API status as a string, standard values are
success
,fail
anderror
.
message: str
- optional
- Default is None and not included in response.
data: Any
- required
- The original result of API response.
code: int
- optional
- Default is None and not included in response.
total: int
- optional
- Total objects retrieved if the result is paginated.
count: int
- optional
- Object's count in the data if the result is paginated.
offset: int
- optional
- Object's offset if the result is paginated.
``
FastAPI
main.py
from fastapi import FastAPI
from fastapi_pagination import add_pagination
from router import router
app = FastAPI()
app.include_router(router)
add_pagination(app)
router.py
from fastapi import APIRouter
from fastapi import Query
from pagination import Pagination
from response import Response
router = APIRouter()
single Object
@router.get(
"/user",
response_model=Response[UserOut],
response_model_exclude_none=True
)
async def get_user(user_id: int = Query(ge=0)):
user: UserOut = find_user(user_id)
return Response(
data=user,
status="success" # default 'success'
)
Paginated
@router.get(
"/users",
response_model=Pagination[UserOut],
response_model_exclude_none=True
)
async def get_users():
response = paginate(users)
response.status = "success" # default 'success'
return response
Exception Handling
Django
settings.py
REST_FRAMEWORK = {
# jsend2 exception handler
"EXCEPTION_HANDLER": "jsend2.django.exception.jsend_exception_handler",
}
urls.py (Project urls)
from jsend2.django.exception import myhandler400, myhandler403, myhandler404, myhandler500
handler500 = myhandler500
handler404 = myhandler404
handler400 = myhandler400
handler403 = myhandler403
FastAPI
main.py
from fastapi import FastAPI, HTTPException
from api import router
from src.jsend2.jfast.exception import ExceptionMiddleware
app = FastAPI()
app.include_router(router)
ExceptionMiddleware(app)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
jsend2-0.0.8.tar.gz
(6.5 kB
view details)
Built Distribution
File details
Details for the file jsend2-0.0.8.tar.gz
.
File metadata
- Download URL: jsend2-0.0.8.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd192b3120c8987ea9ceaf3f1026670873fdeadb349239d8e968d73d4f9fb5ea |
|
MD5 | a6ce3b2beeb711810a73b78d7c2fd46e |
|
BLAKE2b-256 | 8f9c3f810de613a8f6abcdbb61e56712e5a240beafc4fa864508dc0e89f872bf |
File details
Details for the file jsend2-0.0.8-py3-none-any.whl
.
File metadata
- Download URL: jsend2-0.0.8-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 438fb1c5d64fd552da431c5e2ba223e8e25370d9c36e3ef6e8bc4c114d27e04b |
|
MD5 | fa457cd25394a853db389d5c2911a09b |
|
BLAKE2b-256 | 023bdd9142b248beb0c71f54effc7455e93556c14b6f8ecb59765cd7df30ceff |