acwazz / fastapi-responseschema Goto Github PK
View Code? Open in Web Editor NEW☄️ Global response wrappers for FastAPI
Home Page: https://acwazz.github.io/fastapi-responseschema/
License: MIT License
☄️ Global response wrappers for FastAPI
Home Page: https://acwazz.github.io/fastapi-responseschema/
License: MIT License
I have some other exceptions to handle, for example, in the exception, there is a trace_id. How do I add handling for this particular exception?
class CustomException(Exception)
title: str
message: str
trace_id: str
Environment
fastapi
version is: 0.104.1fastapi-responseschema
version is: branch dependabot/pip/master/fastapi-0.104.1python
version is: 3.9Describe the bug
from .interfaces import AbstractResponseSchema
File ".venv/lib/python3.9/site-packages/fastapi_responseschema/interfaces.py", line 8, in
from fastapi.encoders import DictIntStrAny, SetIntStr
ImportError: cannot import name 'DictIntStrAny' from 'fastapi.encoders' (.venv/lib/python3.9/site-packages/fastapi/encoders.py)
Environment
fastapi
version is: 0.79.0fastapi-responseschema
version is: 1.2.2python
version is: 3.9.12Describe the enhancement
Hello, I'm a big fan of your work. I was just trying to run this library on a MS Windows environment in Conda. However I get the error:
ModuleNotFoundError: No module named 'termios'
There are some references that the module termios is only available on Linux:
https://stackoverflow.com/questions/52062977/unable-to-import-termios-package
By any chance do you expect to support Windows too? Maybe by using a cross-platform module, don't know.
However I guess this is a low priority, just wanted to let you know.
Expected behavior
Work on Windows.
Snippets
Just used the example code in README.MD
Additional context
I'm a linux user usually, just happened to use Windows :(
Environment
fastapi
version is: 0.104fastapi-responseschema
version is: 2.1python
version is: 3.9Describe the bug
The lib failed to wrapper the validation error/exception from Pydantic 2.0, although I have specified error_response_schema. http status: 400, 422, response:
{
"detail": [
{
"type": "missing",
"loc": [
"body",
"username"
],
"msg": "Field required",
"input": null,
"url": "https://errors.pydantic.dev/2.5/v/missing"
}
]
}
The wrap result from successful response is good.
class ResponseMetadata(BaseModel):
error: bool = False
message: Optional[str] = None
pagination: Optional[PaginationMetadata] = None
T = TypeVar("T")
class ResponseSchema(AbstractResponseSchema[T], Generic[T]):
data: T
meta: ResponseMetadata
@classmethod
def from_exception(cls, reason, status_code, message: str = "Error", **others):
return cls(
data=reason,
meta=ResponseMetadata(error=status_code >= 400, message=message)
)
@classmethod
def from_api_route(
cls, content: Any, status_code: int, description: Optional[str] = None, **others
):
return cls(
data=content,
meta=ResponseMetadata(error=status_code >= 400, message=description)
)
class PagedResponseSchema(AbstractPagedResponseSchema[T], Generic[T]):
data: Union[Sequence[T], T]
meta: ResponseMetadata
@classmethod
def create(
cls,
items: Sequence[T],
total: int,
params: PaginationParams,
):
return cls(
data=items,
meta=ResponseMetadata(
error=False,
pagination=PaginationMetadata.from_abstract_page_create(total=total, params=params)
)
)
@classmethod
def from_exception(cls, reason: T, status_code: int, message: str = "Error", **others):
return cls(
data=reason,
meta=ResponseMetadata(error=status_code >= 400, message=message)
)
@classmethod
def from_api_route(cls, content: Sequence[T], status_code: int, description: Optional[str] = None, **others):
return cls(error=status_code >= 400, data=content.data, meta=content.meta)
class StandardAPIRoute(SchemaAPIRoute):
response_schema = ResponseSchema
error_response_schema = ResponseSchema
class PagedRoute(PagedSchemaAPIRoute):
response_schema = ResponseSchema
paged_response_schema = PagedResponseSchema
app.router.route_class = StandardAPIRoute
We should compare the FastAPI performance with response schema and vanilla.
Using the library both the response_model and the response_model_exclude filters have stopped working.
How can i resolve it?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.