Name | starlette-resource JSON |
Version |
0.1.1
JSON |
| download |
home_page | |
Summary | Starlette resource that helps you follow a layered architecture. |
upload_time | 2023-11-03 14:20:53 |
maintainer | |
docs_url | None |
author | Gabriel Couture |
requires_python | >=3.8,<4.0 |
license | MIT |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Starlette-resource
[![ci](https://github.com/gacou54/starlette-resource/workflows/Test/badge.svg)](https://github.com/gacou54/starlette-resource/actions?query=workflow%3ATest)
[Starlette](https://www.starlette.io/) resource classes that helps you follow a layered architecture.
This module was made to facilitate the implementation of a layered architecture.
The `Resource` and` WebSocketResource` classes are essentially the same things as Starlette's
[`HTTPEndpoint`](https://www.starlette.io/endpoints/#httpendpoint) and [`WebSocketEndpoint`](https://www.starlette.io/endpoints/#websocketendpoint)
classes. So you can use these classes in the same way.
The difference is that the `Resource` and `WebSockerResource` must be instantiated before being passed to Starlette's [`Route`](https://www.starlette.io/routing/).
Works with Python 3.8+.
### Example
```python
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import PlainTextResponse
from starlette.routing import Route, WebSocketRoute
from starlette.websockets import WebSocket
from starlette_resource import Resource, WebSocketResource
class GreetingService:
async def greet(self, name: str) -> str:
return f'Hello {name}!'
class GreetingResource(Resource):
def __init__(self, hello_service: GreetingService) -> None:
self.hello_service = hello_service
async def get(self, req: Request) -> PlainTextResponse:
name = req.path_params['name']
greeting_message = await self.hello_service.greet(name)
return PlainTextResponse(greeting_message)
async def post(self, req: Request):
...
async def put(self, req: Request):
...
async def delete(self, req: Request):
...
class GreetingWebSocketResource(WebSocketResource):
def __init__(self, hello_service: GreetingService) -> None:
self.hello_service = hello_service
async def on_receive(self, websocket: WebSocket, data: str) -> None:
greeting_message = await self.hello_service.greet(data)
await websocket.send_text(greeting_message)
# Services
greeting_service = GreetingService()
# Resources
greeting_resource = GreetingResource(greeting_service)
greeting_websocket_resource = GreetingWebSocketResource(greeting_service)
app = Starlette(
debug=True,
routes=[
Route('/greet/{name}', greeting_resource),
WebSocketRoute('/websocket_greet', greeting_websocket_resource)
]
)
```
### Installation
Simply install from PyPI:
`pip install starlette-resource`
Raw data
{
"_id": null,
"home_page": "",
"name": "starlette-resource",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "",
"keywords": "",
"author": "Gabriel Couture",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/36/af/d0165bb3bc548cde25b348ee78277f5c92c5920a95acd644306840f7280b/starlette_resource-0.1.1.tar.gz",
"platform": null,
"description": "# Starlette-resource\n[![ci](https://github.com/gacou54/starlette-resource/workflows/Test/badge.svg)](https://github.com/gacou54/starlette-resource/actions?query=workflow%3ATest)\n\n[Starlette](https://www.starlette.io/) resource classes that helps you follow a layered architecture.\n\nThis module was made to facilitate the implementation of a layered architecture.\nThe `Resource` and` WebSocketResource` classes are essentially the same things as Starlette's\n[`HTTPEndpoint`](https://www.starlette.io/endpoints/#httpendpoint) and [`WebSocketEndpoint`](https://www.starlette.io/endpoints/#websocketendpoint)\nclasses. So you can use these classes in the same way.\n\nThe difference is that the `Resource` and `WebSockerResource` must be instantiated before being passed to Starlette's [`Route`](https://www.starlette.io/routing/).\n\nWorks with Python 3.8+.\n\n### Example\n```python\nfrom starlette.applications import Starlette\nfrom starlette.requests import Request\nfrom starlette.responses import PlainTextResponse\nfrom starlette.routing import Route, WebSocketRoute\nfrom starlette.websockets import WebSocket\n\nfrom starlette_resource import Resource, WebSocketResource\n\n\nclass GreetingService:\n async def greet(self, name: str) -> str:\n return f'Hello {name}!'\n\n\nclass GreetingResource(Resource):\n def __init__(self, hello_service: GreetingService) -> None:\n self.hello_service = hello_service\n\n async def get(self, req: Request) -> PlainTextResponse:\n name = req.path_params['name']\n greeting_message = await self.hello_service.greet(name)\n\n return PlainTextResponse(greeting_message)\n \n async def post(self, req: Request):\n ...\n\n async def put(self, req: Request):\n ...\n\n async def delete(self, req: Request):\n ...\n\n\nclass GreetingWebSocketResource(WebSocketResource):\n def __init__(self, hello_service: GreetingService) -> None:\n self.hello_service = hello_service\n\n async def on_receive(self, websocket: WebSocket, data: str) -> None:\n greeting_message = await self.hello_service.greet(data)\n\n await websocket.send_text(greeting_message)\n\n\n# Services\ngreeting_service = GreetingService()\n\n# Resources\ngreeting_resource = GreetingResource(greeting_service)\ngreeting_websocket_resource = GreetingWebSocketResource(greeting_service)\n\napp = Starlette(\n debug=True,\n routes=[\n Route('/greet/{name}', greeting_resource),\n WebSocketRoute('/websocket_greet', greeting_websocket_resource)\n ]\n)\n```\n\n### Installation\n\nSimply install from PyPI:\n\n`pip install starlette-resource`",
"bugtrack_url": null,
"license": "MIT",
"summary": "Starlette resource that helps you follow a layered architecture.",
"version": "0.1.1",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "a4e37298765c977a942bf62d4fe521eafadea08b4a51cb9ec3e300ba2f145bc8",
"md5": "150a40bff3e3286151e11187e29c9980",
"sha256": "76d7974745adedf22c2beafb541854834ae2d25b1ea1efcea393c27100c5a761"
},
"downloads": -1,
"filename": "starlette_resource-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "150a40bff3e3286151e11187e29c9980",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 2826,
"upload_time": "2023-11-03T14:20:51",
"upload_time_iso_8601": "2023-11-03T14:20:51.982222Z",
"url": "https://files.pythonhosted.org/packages/a4/e3/7298765c977a942bf62d4fe521eafadea08b4a51cb9ec3e300ba2f145bc8/starlette_resource-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "36afd0165bb3bc548cde25b348ee78277f5c92c5920a95acd644306840f7280b",
"md5": "516ef99598b1bd7931d39207e83890b3",
"sha256": "1bed38bb53c8c51beff9d8ec44c09e3f7224c24b5b5874dc25ca45a1b210a074"
},
"downloads": -1,
"filename": "starlette_resource-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "516ef99598b1bd7931d39207e83890b3",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 1918,
"upload_time": "2023-11-03T14:20:53",
"upload_time_iso_8601": "2023-11-03T14:20:53.220511Z",
"url": "https://files.pythonhosted.org/packages/36/af/d0165bb3bc548cde25b348ee78277f5c92c5920a95acd644306840f7280b/starlette_resource-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-11-03 14:20:53",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "starlette-resource"
}