dispatchrun / dispatch-py

Python package to develop applications with Dispatch.
https://pypi.org/project/dispatch-py/
Apache License 2.0
56 stars 3 forks source link

Separate FunctionRegistry from FastAPI app #37

Closed chriso closed 9 months ago

chriso commented 9 months ago

dispatch.fastapi.configure now returns a function registry that provides a decorator to register functions.

Previously, we were adding attributes to the FastAPI app:

import fastapi
from dispatch.fastapi import configure

app = fastapi.FastAPI()
configure(app, ...)

@app.dispatch_function
def my_function(input: Input) -> Output: ...

# dispatch a call a function:
app.dispatch(my_function, ...)

Now, the new Dispatch constructor returns a dispatch.Registry instance which provides the decorator:

import fastapi
from dispatch.fastapi import Dispatch

app = fastapi.FastAPI()
dispatch = Dispatch(app, ...)

@dispatch.function
def my_function(...): ...

# dispatch a call a function:
my_function.dispatch(...)

This is better because:

achille-roussel commented 9 months ago

I've been thinking about namings, what if we were following the same model as FastAPI and have the main object called Dispatch?

from fastapi import FastAPI
from dispatch.fastapi import Dispatch

app = FastAPI()
dispatch = Dispatch(app)

I don't know how the model scales to supporting other frameworks that may have different conventions, but maybe we'll just adopt a different convention for each so it feels more native.

chriso commented 9 months ago

That's cleaner, I like it. edit: updated