📜  turbo warp help (1)

📅  最后修改于: 2023-12-03 15:20:41.268000             🧑  作者: Mango

Turbo Warp Help

Turbo Warp is a middleware for building web APIs with Python. It is built on top of Starlette and Pydantic, making it possible to write clean, fast and testable code.

Installation

Turbo Warp can be installed using pip:

pip install turbo-warp
Usage

Here's a simple example of how to create a Hello World API using Turbo Warp:

from turbo_warp import TurboWarp, turbo_response

app = TurboWarp()

@app.route("/")
async def hello_world():
    return turbo_response("Hello World")

if __name__ == "__main__":
    app.run()

In this example, we create a TurboWarp instance, define a route for the root URL and return a Turbo Response with the text "Hello World".

Request Parameters

Turbo Warp makes it easy to work with request parameters. You can access query and path parameters using the request object:

@app.route("/greet/{name}")
async def greet(request):
    name = request.path_params["name"]
    greeting = request.query_params.get("greeting", "Hello")
    return turbo_response(f"{greeting} {name}")

In this example, we define a route with a path parameter name and a query parameter greeting. We access these parameters using the path_params and query_params dictionaries.

Models and Validation

Turbo Warp uses Pydantic to define models for requests and responses. This makes it easy to validate input and output data:

from pydantic import BaseModel

class GreetRequest(BaseModel):
    name: str
    greeting: str = "Hello"

@app.route("/greet")
async def greet(request, body: GreetRequest):
    return turbo_response(f"{body.greeting} {body.name}")

In this example, we define a Pydantic model GreetRequest with two fields: name and greeting. We use this model as the type for the body argument of the route function. Turbo Warp automatically deserializes the request body into an instance of GreetRequest and validates the data.

Middleware

Turbo Warp supports adding middleware to the request/response pipeline. Middleware functions can modify the request and response objects before and after they are processed by the route function:

async def add_header(request, call_next):
    response = await call_next(request)
    response.headers["X-My-Header"] = "Hello World"
    return response

app.add_middleware(add_header)

@app.route("/greet/{name}")
async def greet(request):
    name = request.path_params["name"]
    greeting = request.query_params.get("greeting", "Hello")
    return turbo_response(f"{greeting} {name}")

In this example, we define a middleware function add_header that adds a custom header to the response. We add this middleware to the app using the add_middleware method. The call_next argument is a coroutine that processes the current request and returns a response. We modify the response object after it is generated by the route function.

Conclusion

Turbo Warp is a fast and easy-to-use framework for building web APIs with Python. It provides a simple and intuitive API for handling requests and responses, and supports advanced features like Pydantic models and middleware. Give it a try and let us know what you think!