{"id":22352847,"url":"https://github.com/jrycw/piccolo-api-middlewares","last_synced_at":"2025-08-30T15:25:42.136Z","repository":{"id":229995234,"uuid":"778179985","full_name":"jrycw/piccolo-api-middlewares","owner":"jrycw","description":"ASGI middlewares","archived":false,"fork":false,"pushed_at":"2024-03-28T08:17:51.000Z","size":20,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-31T13:24:51.387Z","etag":null,"topics":["asgi","asyncio","middleware","piccolo","python","starlette"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jrycw.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-27T08:29:39.000Z","updated_at":"2024-03-31T10:01:45.000Z","dependencies_parsed_at":null,"dependency_job_id":"c83b8e11-4081-451e-aef7-61421587656b","html_url":"https://github.com/jrycw/piccolo-api-middlewares","commit_stats":null,"previous_names":["jrycw/piccolo-api-middlewares"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrycw%2Fpiccolo-api-middlewares","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrycw%2Fpiccolo-api-middlewares/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrycw%2Fpiccolo-api-middlewares/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrycw%2Fpiccolo-api-middlewares/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jrycw","download_url":"https://codeload.github.com/jrycw/piccolo-api-middlewares/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245650484,"owners_count":20650105,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asgi","asyncio","middleware","piccolo","python","starlette"],"created_at":"2024-12-04T12:28:06.367Z","updated_at":"2025-03-26T12:14:19.722Z","avatar_url":"https://github.com/jrycw.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# piccolo-api-middlewares\nThis repository is a personal exercise focused on transitioning the middlewares within the `Piccolo API` from the `async def dispatch(self, request, call_next)` style to `async def __call__(self, scope, receive, send)`.\n\n## Procedures\nHere are some general procedures for transforming the middleware:\n\n1. The middleware no longer needs to inherit from `starlette.middleware.base.BaseHTTPMiddleware`. Instead, replace `super().__init__(app)` with `self.app = app`.\n\n2. Remove the `dispatch` method.\n\n3. Add the `__call__` method, a Python dunder method, with the signature `async def __call__(self, scope, receive, send)`.\n\n4. Retrieve required information from the `scope` (a dictionary). `Starlette` provides helpful functions or data structures for this purpose. For example, the `request` object can be obtained from `request = starlette.requests.Request(scope)`.\n\n5. Instead of directly returning the response, obtain a response, call it with `scope, receive, send`, `await` it, and then forcefully `return`.\n\n6. `receive` and `send` are callable. Core business logic may need to be moved into these callables.\n\n7. Finally, remember to call `self.app` with `scope, receive, send`, `await` it, and then forcefully `return`.\n\n8. If you're open to adding the dependency, [asgiref](https://github.com/django/asgiref/) provides some useful type annotations.\n\n## References\n* [piccolo-api](https://piccolo-api.readthedocs.io/en/latest/)\n* [starlette](https://www.starlette.io/middleware/)\n* [starlette-csrf](https://github.com/frankie567/starlette-csrf/tree/main)\n* [encode](https://www.encode.io/articles/working-with-http-requests-in-asgi)\n* [How to secure APIs built with FastAPI: A complete guide](https://escape.tech/blog/how-to-secure-fastapi-api/#how-to-secure-fastapi-api-against-csrf)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjrycw%2Fpiccolo-api-middlewares","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjrycw%2Fpiccolo-api-middlewares","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjrycw%2Fpiccolo-api-middlewares/lists"}