Collection of useful middlewares for aiohttp.web applications.
from aiohttp import web
from aiohttp_middlewares import (
cors_middleware,
error_middleware,
https_middleware,
IDEMPOTENT_METHODS,
shield_middleware,
timeout_middleware
)
app = web.Application(
middlewares=(
# Change schema of current request if
# X-Forwarded-Schema: https header is present
https_middleware(),
# Allow CORS headers for all requests from
# http://localhost:8000
cors_middleware(origins=("http://localhost:8000",)),
# Ensure all exceptions will result in predefined
# JSON response
error_middleware(),
# Shield idempotent methods against canceled errors
shield_middleware(methods=IDEMPOTENT_METHODS),
# Ensure that handler will not execute for more
# than 29.5 seconds
timeout_middleware(29.5),
)
)
OpenAPI 3 schema support for aiohttp.web applications.
from pathlib import Path
from aiohttp import web
from rororo import openapi_context, OperationTableDef, setup_openapi
operations = OperationTableDef()
@operations.register
async def hello_world(request: web.Request) -> web.Response:
with openapi_context(request) as context:
name = context.parameters.query.get("name", "world")
return web.json_response({"message": f"Hello, {name}!"})
def create_app(argv: list[str] = None) -> web.Application:
return setup_openapi(
web.Application(),
Path(__file__).parent / "openapi.yaml",
operations,
server_url="/api/"
)
tus.io server implementation for aiohttp.web applications.
from aiohttp import web
from aiohttp_tus import setup_tus
app = setup_tus(
web.Application(),
upload_path=Path(__file__).parent.parent / "uploads"
)
useRequest
hook & set of components for requesting API data within
React applications.
index.js
import React from "react";
import { mount, SadnessProvider } from "react-sadness";
import App from "./App";
mount(
<SadnessProvider>
<App />
</SadnessProvider>,
document.getElementById("ui")
);
App.js
import React from "react";
import { Response, useRequest } from "react-sadness";
const App = () => {
const { response } = useRequest("/api/projects");
return (
<Response data={response}>
{projects => <Projects data={projects} />}
</Response>
);
};
export default App;
Over the years Iβve contributed to several Open Source projects, most notably of: aiohttp & typeshed.
I have also created some extensions for Django & Flask web frameworks & other libraries for Python developers. However, many of those projects are not in use anymore and as result are not actively maintained.
For full list of my projects, please go to my Github profile: @playpauseandstop. You might interested in my gists as well.