diff --git a/examples/auto_retry.py b/examples/auto_retry.py index 0fad2f8..069a3a5 100644 --- a/examples/auto_retry.py +++ b/examples/auto_retry.py @@ -1,10 +1,13 @@ -import dispatch -import dispatch.integrations.requests import random + import requests +import dispatch +import dispatch.integrations.requests + rng = random.Random(2) + def third_party_api_call(x: int) -> str: # Simulate a third-party API call that fails. print(f"Simulating third-party API call with {x}") diff --git a/examples/fanout.py b/examples/fanout.py index ac04459..856d496 100644 --- a/examples/fanout.py +++ b/examples/fanout.py @@ -1,6 +1,8 @@ -import dispatch import httpx +import dispatch + + @dispatch.function def get_repo(repo_owner: str, repo_name: str): url = f"https://api.github.com/repos/{repo_owner}/{repo_name}" @@ -39,4 +41,5 @@ async def fanout(): ) return await reduce_stargazers(repos) + print(dispatch.run(fanout())) diff --git a/examples/getting_started.py b/examples/getting_started.py index f2ecace..be1b41f 100644 --- a/examples/getting_started.py +++ b/examples/getting_started.py @@ -1,6 +1,7 @@ -import dispatch import requests +import dispatch + # Use the `dispatch.function` decorator declare a stateful function. @dispatch.function diff --git a/examples/github_stats.py b/examples/github_stats.py index 996743d..2edc617 100644 --- a/examples/github_stats.py +++ b/examples/github_stats.py @@ -14,10 +14,12 @@ """ -import dispatch import httpx + +import dispatch from dispatch.error import ThrottleError + def get_gh_api(url): print(f"GET {url}") response = httpx.get(url) diff --git a/src/dispatch/function.py b/src/dispatch/function.py index 98442d0..4b53704 100644 --- a/src/dispatch/function.py +++ b/src/dispatch/function.py @@ -386,6 +386,7 @@ def set_default_registry(reg: Registry): # for results. _calls: Dict[str, asyncio.Future] = {} + class Client: """Client for the Dispatch API.""" diff --git a/src/dispatch/http.py b/src/dispatch/http.py index 099163a..55778bd 100644 --- a/src/dispatch/http.py +++ b/src/dispatch/http.py @@ -16,11 +16,11 @@ overload, ) -from aiohttp import web, ClientConnectionError +from aiohttp import ClientConnectionError, web from http_message_signatures import InvalidSignature from typing_extensions import ParamSpec, TypeAlias -from dispatch.function import Batch, Function, Registry, default_registry, _calls +from dispatch.function import Batch, Function, Registry, _calls, default_registry from dispatch.proto import CallResult, Input from dispatch.sdk.v1 import function_pb2 as function_pb from dispatch.signature import ( @@ -81,7 +81,9 @@ def function(self, func): def batch(self) -> Batch: return self.registry.batch() - async def run(self, url: str, method: str, headers: Mapping[str, str], data: bytes) -> bytes: + async def run( + self, url: str, method: str, headers: Mapping[str, str], data: bytes + ) -> bytes: return await function_service_run( url, method, diff --git a/src/dispatch/scheduler.py b/src/dispatch/scheduler.py index 9140c57..a5ac9fe 100644 --- a/src/dispatch/scheduler.py +++ b/src/dispatch/scheduler.py @@ -490,7 +490,9 @@ async def _run(self, input: Input) -> Output: state.suspended = {} -async def run_coroutine(state: State, coroutine: Coroutine, pending_calls: List[Call]) -> Optional[Output]: +async def run_coroutine( + state: State, coroutine: Coroutine, pending_calls: List[Call] +) -> Optional[Output]: return await make_coroutine(state, coroutine, pending_calls) diff --git a/src/dispatch/status.py b/src/dispatch/status.py index 82f90d2..7563132 100644 --- a/src/dispatch/status.py +++ b/src/dispatch/status.py @@ -50,6 +50,7 @@ def temporary(self) -> bool: Status.HTTP_ERROR, } + # Maybe we should find a better way to define that enum. It's that way to please # Mypy and provide documentation for the enum values. diff --git a/src/dispatch/test/__init__.py b/src/dispatch/test/__init__.py index 1b93ff4..dd69a9e 100644 --- a/src/dispatch/test/__init__.py +++ b/src/dispatch/test/__init__.py @@ -60,6 +60,7 @@ _dispatch_ids = (str(i) for i in range(2**32 - 1)) + class Client(BaseClient): def session(self) -> aiohttp.ClientSession: # Use an individual sessionn in the test client instead of the default @@ -76,6 +77,7 @@ def __init__(self, app: web.Application): def url(self): return f"http://{self.host}:{self.port}" + class Service(web.Application): tasks: Dict[str, asyncio.Task] _session: Optional[aiohttp.ClientSession] = None