apidaora

repository·master·Indexed 41 days ago

https://github.com/dutradda/apidaora

A high-performance Python ASGI framework for building OpenAPI/REST APIs. It utilizes type annotations such as dataclasses and TypedDict for request and response modeling, and integrates with jsondaora for JSON validation and orjson for serialization. Features include a @route decorator, ClassController for structured routing, and a BackgroundTask controller with support for Redis backends and argument-based locking mechanisms.

Tokens
5.5K
Snippets
29
Records
38
Agent score
99%

What's inside apidaora

  1. Using Background Task Controller with Lock

    master
    The Background Task Controller with Lock functionality allows you to manage background tasks while preventing duplicate execution of the same task. When a task is currently running or locked, attempting to create an identical task will result in an error. This pattern is useful for ensuring that long-running or resource-intensive operations are not triggered multiple times concurrently.
  2. Use Background Task Controller with Lock by Args

    master
    The Background Task Controller supports a locking mechanism based on task arguments. When a task is triggered with a specific set of arguments, the controller prevents duplicate tasks with the exact same arguments from running concurrently. If a user attempts to create a task with arguments that are currently being processed by an active task, the server will return an error. Once the original task completes, new tasks with those same arguments can be created again.
  3. Define request/response schemas using TypedDict and jsondaora

    master

    For complex data structures, use TypedDict annotated with @jsondaora to define request bodies and response shapes. This enables automatic input validation and structured output.

    from typing import TypedDict
    from jsondaora import jsondaora
    from apidaora import route
    
    
    @jsondaora
    class ReqBody(TypedDict):
        last_name: str
    
    
    @jsondaora
    class HelloOutput(TypedDict):
        hello_message: str
    
    
    @route.put('/hello/{name}')
    async def hello_controller(name: str, body: ReqBody) -> HelloOutput:
        return HelloOutput(hello_message=f'Hello {name}!')
    
    
    app = appdaora(hello_controller)
  4. Use Background Task Controller with Redis Backend

    master

    The Background Task Controller can be implemented using a Redis backend to manage asynchronous tasks. This setup allows you to offload long-running processes and query their status or results later via an API.

    To use this, you need a running Redis server and a web server (like uvicorn) to host the application. The workflow involves:

    1. Creating a task: Sending a request to the server to initiate a background process.
    2. Querying the task: Using the returned task_id to check the current status of the task.
    3. Retrieving results: Polling the status until the task is finished to obtain the final output.
    # Note: The implementation details are contained in the source file:
    # src/redis_background_task_controller/redis_background_task_controller.py
  5. Use Extra Arguments Middlewares

    master

    Extra Arguments Middlewares allow you to inject additional data or arguments into the request/response lifecycle. This is typically used to augment the context of a request with data derived from headers, query parameters, or other metadata before it reaches the application logic.

    # Implementation found in src/middlewares_extra_args/middlewares_extra_args.py
  6. Use Redis Background Task Controller with Lock by Args

    master
    The Redis Background Task Controller with Lock by Args allows you to execute background tasks using Redis, with a specific mechanism to prevent concurrent execution of tasks that share the exact same arguments. If a task is already running with a specific set of arguments, attempting to create a new task with those same arguments will result in an error, effectively acting as a distributed lock based on the task's input parameters.
  7. Create a simple API with appdaora and route

    master

    To create a basic API, use the @route decorator to define HTTP methods and paths, and pass the controller function(s) to appdaora() to create the ASGI application instance. The controller function's arguments are automatically mapped from path parameters or query parameters based on type annotations.

    from apidaora import appdaora, route
    
    
    @route.get('/hello')
    def hello_controller(name: str) -> str:
        return f'Hello {name}!'
    
    
    app = appdaora(hello_controller)
  8. Create and query background tasks with locking

    master

    The locking mechanism works as follows:

    1. Create a task: Send a request to initiate the task. The server will return a task_id.
    2. Prevent duplicates: If you attempt to create the same task while the previous one is still active/locked, the server will return an error.
    3. Query task status: Use the task_id provided during creation to query the server for the current status of the task.
    4. Retrieve results: Once the task status indicates it has finished, you can retrieve the results.
    5. Retry task: After the initial task has completed and the lock is released, you can successfully create the task again.