Git Product home page Git Product logo

fastapi-responseschema's People

Contributors

acwazz avatar dependabot[bot] avatar flc995 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

flc995 khoadautay

fastapi-responseschema's Issues

[Feat] how to add other Exception handler

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

[BUG] - cannot import name 'DictIntStrAny' from 'fastapi.encoders

Environment

  • fastapi version is: 0.104.1
  • fastapi-responseschema version is: branch dependabot/pip/master/fastapi-0.104.1
  • python version is: 3.9

Describe 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)

[ENHANCEMENT] - Support for Microsoft Windows?

Environment

  • fastapi version is: 0.79.0
  • fastapi-responseschema version is: 1.2.2
  • python version is: 3.9.12

Describe 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 :(

immagine

Error / exception failed to be wrapped

Environment

  • fastapi version is: 0.104
  • fastapi-responseschema version is: 2.1
  • python version is: 3.9

Describe 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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.