diff --git a/README.md b/README.md index 3c76203a..7f7c4504 100644 --- a/README.md +++ b/README.md @@ -100,11 +100,11 @@ The following code snippet is a complete example showing how to install a ```python from fastapi import FastAPI -from dispatch.fastapi import Dispatch +from dispatch.fastapi import Endpoint import requests app = FastAPI() -dispatch = Dispatch(app) +dispatch = Endpoint(app) @dispatch.function def publish(url, payload): diff --git a/examples/auto_retry/app.py b/examples/auto_retry/app.py index 466e76e0..4b14f806 100644 --- a/examples/auto_retry/app.py +++ b/examples/auto_retry/app.py @@ -22,7 +22,7 @@ import requests from fastapi import FastAPI -from dispatch.fastapi import Dispatch +from dispatch.fastapi import Endpoint # Create the FastAPI app like you normally would. app = FastAPI() @@ -32,7 +32,7 @@ # Create a Dispatch instance and pass the FastAPI app to it. It automatically # sets up the necessary routes and handlers. -dispatch = Dispatch(app) +dispatch = Endpoint(app) def third_party_api_call(x): diff --git a/examples/fanout/fanout.py b/examples/fanout/fanout.py index c6d50e9e..3027fae0 100644 --- a/examples/fanout/fanout.py +++ b/examples/fanout/fanout.py @@ -15,11 +15,11 @@ from fastapi import FastAPI from dispatch import gather -from dispatch.fastapi import Dispatch +from dispatch.fastapi import Endpoint app = FastAPI() -dispatch = Dispatch(app) +dispatch = Endpoint(app) @dispatch.function diff --git a/examples/getting_started/app.py b/examples/getting_started/app.py index 2e90fae4..11ab722e 100644 --- a/examples/getting_started/app.py +++ b/examples/getting_started/app.py @@ -57,14 +57,14 @@ import requests from fastapi import FastAPI -from dispatch.fastapi import Dispatch +from dispatch.fastapi import Endpoint # Create the FastAPI app like you normally would. app = FastAPI() # Create a Dispatch instance and pass the FastAPI app to it. It automatically # sets up the necessary routes and handlers. -dispatch = Dispatch(app) +dispatch = Endpoint(app) # Use the `dispatch.function` decorator declare a stateful function. diff --git a/examples/github_stats/app.py b/examples/github_stats/app.py index e34b8fe5..6ffbff1e 100644 --- a/examples/github_stats/app.py +++ b/examples/github_stats/app.py @@ -17,11 +17,11 @@ import httpx from fastapi import FastAPI -from dispatch.fastapi import Dispatch +from dispatch.fastapi import Endpoint app = FastAPI() -dispatch = Dispatch(app) +dispatch = Endpoint(app) def get_gh_api(url): diff --git a/src/dispatch/__init__.py b/src/dispatch/__init__.py index 45a45e75..154257db 100644 --- a/src/dispatch/__init__.py +++ b/src/dispatch/__init__.py @@ -4,7 +4,7 @@ import dispatch.integrations from dispatch.coroutine import all, any, call, gather, race -from dispatch.function import DEFAULT_API_URL, Client, Registry +from dispatch.function import DEFAULT_API_URL, Client from dispatch.id import DispatchID from dispatch.proto import Call, Error, Input, Output from dispatch.status import Status @@ -23,5 +23,4 @@ "all", "any", "race", - "Registry", ] diff --git a/src/dispatch/fastapi.py b/src/dispatch/fastapi.py index 0cb98384..8b488776 100644 --- a/src/dispatch/fastapi.py +++ b/src/dispatch/fastapi.py @@ -3,10 +3,10 @@ Example: import fastapi - from dispatch.fastapi import Dispatch + from dispatch.fastapi import Endpoint app = fastapi.FastAPI() - dispatch = Dispatch(app, api_key="test-key") + dispatch = Endpoint(app, api_key="test-key") @dispatch.function() def my_function(): @@ -27,7 +27,7 @@ def read_root(): import fastapi.responses from http_message_signatures import InvalidSignature -from dispatch.function import Batch, Client, Registry +from dispatch.function import Batch, Registry from dispatch.proto import Input from dispatch.sdk.v1 import function_pb2 as function_pb from dispatch.signature import ( @@ -43,7 +43,7 @@ def read_root(): logger = logging.getLogger(__name__) -class Dispatch(Registry): +class Endpoint(Registry): """A Dispatch programmable endpoint, powered by FastAPI.""" __slots__ = ("client",) @@ -126,6 +126,10 @@ def batch(self) -> Batch: return self.client.batch() +Dispatch = Endpoint +"""An alias for Endpoint, provided for backward compatibility.""" + + def parse_verification_key( verification_key: Ed25519PublicKey | str | bytes | None, ) -> Ed25519PublicKey | None: @@ -174,7 +178,7 @@ def __init__(self, status, code, message): self.message = message -def _new_app(function_registry: Dispatch, verification_key: Ed25519PublicKey | None): +def _new_app(endpoint: Endpoint, verification_key: Ed25519PublicKey | None): app = fastapi.FastAPI() @app.exception_handler(_ConnectError) @@ -224,7 +228,7 @@ async def execute(request: fastapi.Request): raise _ConnectError(400, "invalid_argument", "function is required") try: - func = function_registry.functions[req.function] + func = endpoint.functions[req.function] except KeyError: logger.debug("function '%s' not found", req.function) raise _ConnectError( diff --git a/src/dispatch/remote.py b/src/dispatch/remote.py new file mode 100644 index 00000000..bfebdc7d --- /dev/null +++ b/src/dispatch/remote.py @@ -0,0 +1,5 @@ +from dispatch.function import Registry + + +class Endpoint(Registry): + """A remote registry of functions.""" diff --git a/tests/dispatch/test_function.py b/tests/dispatch/test_function.py index 0befc0f7..ee02e4aa 100644 --- a/tests/dispatch/test_function.py +++ b/tests/dispatch/test_function.py @@ -1,12 +1,12 @@ import pickle import unittest -from dispatch.function import Client, Registry +from dispatch.remote import Endpoint class TestFunction(unittest.TestCase): def setUp(self): - self.dispatch = Registry( + self.dispatch = Endpoint( endpoint="http://example.com", api_url="http://dispatch.com", api_key="foobar", diff --git a/tests/test_fastapi.py b/tests/test_fastapi.py index 33ee7160..03880ad2 100644 --- a/tests/test_fastapi.py +++ b/tests/test_fastapi.py @@ -13,7 +13,7 @@ from fastapi.testclient import TestClient from dispatch.experimental.durable.registry import clear_functions -from dispatch.fastapi import Dispatch, parse_verification_key +from dispatch.fastapi import Endpoint, parse_verification_key from dispatch.function import Arguments, Error, Function, Input, Output from dispatch.proto import _any_unpickle as any_unpickle from dispatch.sdk.v1 import call_pb2 as call_pb @@ -30,7 +30,7 @@ def create_dispatch_instance(app, endpoint): - return Dispatch( + return Endpoint( app, endpoint=endpoint, api_key="0000000000000000", diff --git a/tests/test_full.py b/tests/test_full.py index f9b142b1..0ed7a3f2 100644 --- a/tests/test_full.py +++ b/tests/test_full.py @@ -4,7 +4,7 @@ import httpx import dispatch -from dispatch.fastapi import Dispatch +from dispatch.fastapi import Endpoint from dispatch.proto import _any_unpickle as any_unpickle from dispatch.signature import private_key_from_pem, public_key_from_pem from dispatch.test import DispatchServer, DispatchService, EndpointClient @@ -40,7 +40,7 @@ def setUp(self): api_key, api_url=self.dispatch_server.url ) - self.dispatch = Dispatch( + self.dispatch = Endpoint( self.endpoint_app, endpoint="http://function-service", # unused verification_key=verification_key,