Skip to main content

Starlette-resource

ci

Starlette 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 and 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.

Works with Python 3.8+.

Example

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

Metadata

Release files for starlette-resource 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for starlette-resource 0.1.1
File Size Uploaded Provenance Actions
starlette_resource-0.1.1.tar.gz 1.9 kB Without provenance Details for starlette_resource-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for starlette-resource 0.1.1
File Interpreter ABI Platform Actions
starlette_resource-0.1.1-py3-none-any.whl Python 3 none any Details for starlette_resource-0.1.1-py3-none-any.whl

Total release size: 4.7 kB

Release files / starlette_resource-0.1.1.tar.gz

Download URL starlette_resource-0.1.1.tar.gz
Size 1.9 kB
Tags Source
SHA-256 checksum
How to use checksums
1bed38bb53c8c51beff9d8ec44c09e3f7224c24b5b5874dc25ca45a1b210a074
BLAKE2b-256 checksum
How to use checksums
36afd0165bb3bc548cde25b348ee78277f5c92c5920a95acd644306840f7280b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.6 Linux/6.5.8-200.fc38.x86_64

Release files / starlette_resource-0.1.1-py3-none-any.whl

Download URL starlette_resource-0.1.1-py3-none-any.whl
Size 2.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
76d7974745adedf22c2beafb541854834ae2d25b1ea1efcea393c27100c5a761
BLAKE2b-256 checksum
How to use checksums
a4e37298765c977a942bf62d4fe521eafadea08b4a51cb9ec3e300ba2f145bc8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.6 Linux/6.5.8-200.fc38.x86_64

Security for starlette-resource 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page