Skip to content

Endpoint #135

New issue

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

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

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions examples/auto_retry/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions examples/fanout/fanout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions examples/getting_started/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions examples/github_stats/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions src/dispatch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,5 +23,4 @@
"all",
"any",
"race",
"Registry",
]
16 changes: 10 additions & 6 deletions src/dispatch/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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 (
Expand All @@ -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",)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions src/dispatch/remote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from dispatch.function import Registry


class Endpoint(Registry):
"""A remote registry of functions."""
4 changes: 2 additions & 2 deletions tests/dispatch/test_function.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions tests/test_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,7 +30,7 @@


def create_dispatch_instance(app, endpoint):
return Dispatch(
return Endpoint(
app,
endpoint=endpoint,
api_key="0000000000000000",
Expand Down
4 changes: 2 additions & 2 deletions tests/test_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down