kennethreitz / responder

A familiar HTTP Service Framework for Python.
http://responder.kennethreitz.org/
Other
3.59k stars 217 forks source link

Added response validation #493

Closed tabotkevin closed 8 months ago

tabotkevin commented 8 months ago

check examples/response_validation.py

""" A decorator for serializing response dictionaries or SQLAlchemy objects. Supports both Pydantic and Marshmallow.

    Usage::
        from typing import Optional

        from pydantic import BaseModel
        from sqlalchemy import Column, Integer, String
        import responder

        from .models import Item

        class Item(Base):
            __tablename__ = "items"
            id = Column(Integer, primary_key=True)
            name = Column(String)

        class ItemCreate(BaseModel)
            id: Optional[int]
            name: str

            class Config:
                from_attributes = True

        api = responder.API()

        @api.route("/all")
        @api.ensure(ItemCreate)
        async def all_items(req, resp):
            "Get all items"

        return session.query(Item)

        @api.route("/create")
        @api.trust(ItemCreate)
        @api.ensure(ItemCreate)
        async def create(req, resp, *, data):
            "Create item"

            item = Item(**data)
            session.add(item)
            session.commit()

            return item
    """

Summary by CodeRabbit

coderabbitai[bot] commented 8 months ago

Walkthrough

The update introduces a Python web service with Responder, integrating SQLAlchemy for database operations and Pydantic/Marshmallow for data validation. New API endpoints for book management are added, and the Responder module is enhanced with serialization and application mounting capabilities.

Changes

File Path Change Summary
examples/response_validation.py Added a web service with endpoints for creating and retrieving books, using SQLAlchemy and data validation with Pydantic/Marshmallow.
responder/api.py Added ensure and mount methods for response serialization and application mounting, respectively.
setup.py Included "sqlalchemy" in the dependencies list.

🐇 In the code's gentle weave, 🍂
A tale of books and schemas conceived.
As autumn leaves in crisp air twirl,
Our service dances, a new world unfurled. 🌐✨


Tips ### Chat with CodeRabbit Bot (`@coderabbitai`) - If you reply to a *review comment* from CodeRabbit, the bot will automatically respond. - To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment - Note: Review comments are made on code diffs or files, not on the PR overview. - Add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. ### CodeRabbit Commands (invoked as PR comments) - `@coderabbitai pause` to pause the reviews on a PR. - `@coderabbitai resume` to resume the paused reviews. - `@coderabbitai review` to trigger a review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai help` to get help. Note: For conversation with the bot, please use the review comments on code diffs or files. ### CodeRabbit Configration File (`.coderabbit.yaml`) - You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository. - The JSON schema for the configuration file is available [here](https://coderabbit.ai/integrations/coderabbit-overrides.v2.json). - If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json`