Skip to content

Commit d830662

Browse files
make fmt
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
1 parent 8727b01 commit d830662

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

src/dispatch/__init__.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
"""The Dispatch SDK for Python."""
2+
23
from __future__ import annotations
34

45
import os
5-
import dispatch.integrations
6-
76
from concurrent import futures
87
from http.server import HTTPServer
98
from typing import Any, Callable, Coroutine, Optional, TypeVar, overload
10-
from typing_extensions import ParamSpec, TypeAlias
119
from urllib.parse import urlsplit
1210

11+
from typing_extensions import ParamSpec, TypeAlias
12+
13+
import dispatch.integrations
1314
from dispatch.coroutine import all, any, call, gather, race
1415
from dispatch.function import DEFAULT_API_URL, Client, Function, Registry, Reset
1516
from dispatch.http import Dispatch
1617
from dispatch.id import DispatchID
1718
from dispatch.proto import Call, Error, Input, Output
18-
from dispatch.status import Status
1919
from dispatch.sdk.v1 import function_pb2_grpc as function_grpc
20+
from dispatch.status import Status
2021

2122
__all__ = [
2223
"Call",
@@ -44,21 +45,26 @@
4445

4546
_registry: Optional[Registry] = None
4647

48+
4749
def _default_registry():
4850
global _registry
4951
if not _registry:
5052
_registry = Registry()
5153
return _registry
5254

55+
5356
@overload
5457
def function(func: Callable[P, Coroutine[Any, Any, T]]) -> Function[P, T]: ...
5558

59+
5660
@overload
5761
def function(func: Callable[P, T]) -> Function[P, T]: ...
5862

63+
5964
def function(func):
6065
return _default_registry().function(func)
6166

67+
6268
def run(port: str = os.environ.get("DISPATCH_ENDPOINT_ADDR", "[::]:8000")):
6369
"""Run the default dispatch server on the given port. The default server
6470
uses a function registry where functions tagged by the `@dispatch.function`
@@ -73,8 +79,7 @@ def run(port: str = os.environ.get("DISPATCH_ENDPOINT_ADDR", "[::]:8000")):
7379
DISPATCH_ENDPOINT_ADDR environment variable, or '[::]:8000' if it
7480
wasn't set.
7581
"""
76-
parsed_url = urlsplit('//' + port)
82+
parsed_url = urlsplit("//" + port)
7783
server_address = (parsed_url.hostname, parsed_url.port)
7884
server = HTTPServer(server_address, Dispatch(_default_registry()))
7985
server.serve_forever()
80-

src/dispatch/grpc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
class FunctionService(FunctionServiceServicer):
1515
"""A Dispatch instance to be serviced by a gRPC server."""
1616

17-
def __init__( self, registry: Registry ):
17+
def __init__(self, registry: Registry):
1818
"""Initialize a Dispatch gRPC service.
1919
2020
Args:
@@ -47,4 +47,5 @@ def Run(self, request, context):
4747

4848
return output._message
4949

50+
5051
# TODO: interceptor for verification key

src/dispatch/http.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import logging
44
import os
55
from datetime import timedelta
6-
from typing import Optional, Union
76
from http.server import BaseHTTPRequestHandler
7+
from typing import Optional, Union
8+
89
from http_message_signatures import InvalidSignature
910

1011
from dispatch.function import Registry
@@ -27,7 +28,10 @@ class Dispatch:
2728
acts as a factory for DispatchHandler objects, by capturing the variables
2829
that would be shared between all DispatchHandler instances it created."""
2930

30-
def __init__( self, registry: Registry, verification_key: Optional[Union[Ed25519PublicKey, str, bytes]] = None,
31+
def __init__(
32+
self,
33+
registry: Registry,
34+
verification_key: Optional[Union[Ed25519PublicKey, str, bytes]] = None,
3135
):
3236
"""Initialize a Dispatch http handler.
3337
@@ -38,16 +42,29 @@ def __init__( self, registry: Registry, verification_k
3842
self.verification_key = parse_verification_key(verification_key)
3943

4044
def __call__(self, request, client_address, server):
41-
return FunctionService(request, client_address, server, registry=self.registry, verification_key=self.verification_key)
45+
return FunctionService(
46+
request,
47+
client_address,
48+
server,
49+
registry=self.registry,
50+
verification_key=self.verification_key,
51+
)
4252

4353

4454
class FunctionService(BaseHTTPRequestHandler):
4555

46-
def __init__(self, request, client_address, server, registry: Registry, verification_key: Optional[Ed25519PublicKey] = None):
56+
def __init__(
57+
self,
58+
request,
59+
client_address,
60+
server,
61+
registry: Registry,
62+
verification_key: Optional[Ed25519PublicKey] = None,
63+
):
4764
super().__init__(request, client_address, server)
4865
self.registry = registry
4966
self.verification_key = verification_key
50-
self.error_content_type = 'application/json'
67+
self.error_content_type = "application/json"
5168

5269
def send_error_response_invalid_argument(self, message: str):
5370
self.send_error_response(400, "invalid_argument", message)
@@ -80,8 +97,8 @@ def do_POST(self):
8097

8198
if self.verification_key is not None:
8299
signed_request = Request(
83-
method='POST',
84-
url=self.requestline, # TODO: need full URL
100+
method="POST",
101+
url=self.requestline, # TODO: need full URL
85102
headers=CaseInsensitiveDict(self.headers),
86103
body=data,
87104
)
@@ -108,7 +125,9 @@ def do_POST(self):
108125
func = self.registry.functions[req.function]
109126
except KeyError:
110127
logger.debug("function '%s' not found", req.function)
111-
self.send_error_response_not_found(f"function '{req.function}' does not exist")
128+
self.send_error_response_not_found(
129+
f"function '{req.function}' does not exist"
130+
)
112131
return
113132

114133
logger.info("running function '%s'", req.function)

0 commit comments

Comments
 (0)