Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[WIP] Generate server implementation for pure Python business logic #1367

Open
31 of 34 tasks
crisidev opened this issue May 5, 2022 · 0 comments
Open
31 of 34 tasks
Assignees
Labels
enhancement New feature or request python-server Python server SDK server Rust server SDK x-large

Comments

@crisidev
Copy link
Contributor

crisidev commented May 5, 2022

Motivation and Context

Pyo3 will allow us to generate an idiomatic Python implementation which is exported from Rust as a shared library.

The framework heavy-lifting will be handled completely in Rust, while the business logic will be implemented in pure Python.

Example

# Import the shared library application, context and input/output types.
from pokemon_service import PokemonService, SharedContext
from pokemon_service.model import PokemonDescriptionInput, PokemonDescriptionOutput, AuthError
from pokemon_service.http import Request
# import application specific code.
from .database import DatabasePool

# Define a context class to be shared between handlers
@dataclass
class Context:
    db = DatabasePool()
 
# Create the application and register context.   
app = PokemonService()
app.context(Context())

# Define a middleware function.
@app.middleware
async def auth_middleware(request: Request):
    if request.headers.get("a-token") != "i_am_so_secret":
        raise AuthError("Invalid a-token")

# Define the operation using the type imported from Rust.
@app.pokemon_description
async def pokemon_description(input: PokemonDescriptionInput, ctx: SharedContext) -> PokemonDescriptionOutput:
    description = await ctx.db.get_description(input.name)
    return PokemonDescriptionOutput(description)

# Start the application. 
app.run()

Features

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request python-server Python server SDK server Rust server SDK x-large
Projects
None yet
Development

No branches or pull requests

2 participants