Skip to content

Commit

Permalink
chore: update decorator static types (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjholm authored Jun 23, 2023
2 parents 74d43ca + 6c9832b commit ab67989
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
Empty file added nitric/py.typed
Empty file.
48 changes: 25 additions & 23 deletions nitric/resources/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def _route(self, match: str, opts: Optional[RouteOptions] = None) -> Route:
self.routes.append(r)
return r

def all(self, match: str, opts: Optional[MethodOptions] = None):
def all(self, match: str, opts: Optional[MethodOptions] = None) -> Callable[[HttpHandler], None]:
"""Define an HTTP route which will respond to HTTP GET requests."""

def decorator(function: HttpHandler) -> None:
Expand All @@ -201,78 +201,80 @@ def decorator(function: HttpHandler) -> None:

return decorator

def methods(self, methods: List[HttpMethod], match: str, opts: Optional[MethodOptions] = None):
def methods(
self, methods: List[HttpMethod], match: str, opts: Optional[MethodOptions] = None
) -> Callable[[HttpHandler], None]:
"""Define an HTTP route which will respond to specific HTTP requests defined by a list of verbs."""
if opts is None:
opts = MethodOptions()

def decorator(function: HttpHandler):
def decorator(function: HttpHandler) -> None:
r = self._route(match)
r.method(methods, function, opts=opts)

return decorator

def get(self, match: str, opts: Optional[MethodOptions] = None):
def get(self, match: str, opts: Optional[MethodOptions] = None) -> Callable[[HttpHandler], None]:
"""Define an HTTP route which will respond to HTTP GET requests."""
if opts is None:
opts = MethodOptions()

def decorator(function: HttpHandler):
def decorator(function: HttpHandler) -> None:
r = self._route(match)
r.get(function, opts=opts)

return decorator

def post(self, match: str, opts: Optional[MethodOptions] = None):
def post(self, match: str, opts: Optional[MethodOptions] = None) -> Callable[[HttpHandler], None]:
"""Define an HTTP route which will respond to HTTP POST requests."""
if opts is None:
opts = MethodOptions()

def decorator(function: HttpHandler):
def decorator(function: HttpHandler) -> None:
r = self._route(match)
r.post(function, opts=opts)

return decorator

def delete(self, match: str, opts: Optional[MethodOptions] = None):
def delete(self, match: str, opts: Optional[MethodOptions] = None) -> Callable[[HttpHandler], None]:
"""Define an HTTP route which will respond to HTTP DELETE requests."""
if opts is None:
opts = MethodOptions()

def decorator(function: HttpHandler):
def decorator(function: HttpHandler) -> None:
r = self._route(match)
r.delete(function, opts=opts)

return decorator

def options(self, match: str, opts: Optional[MethodOptions] = None):
def options(self, match: str, opts: Optional[MethodOptions] = None) -> Callable[[HttpHandler], None]:
"""Define an HTTP route which will respond to HTTP OPTIONS requests."""
if opts is None:
opts = MethodOptions()

def decorator(function: HttpHandler):
def decorator(function: HttpHandler) -> None:
r = self._route(match)
r.options(function, opts=opts)

return decorator

def patch(self, match: str, opts: Optional[MethodOptions] = None):
def patch(self, match: str, opts: Optional[MethodOptions] = None) -> Callable[[HttpHandler], None]:
"""Define an HTTP route which will respond to HTTP PATCH requests."""
if opts is None:
opts = MethodOptions()

def decorator(function: HttpHandler):
def decorator(function: HttpHandler) -> None:
r = self._route(match)
r.patch(function, opts=opts)

return decorator

def put(self, match: str, opts: Optional[MethodOptions] = None):
def put(self, match: str, opts: Optional[MethodOptions] = None) -> Callable[[HttpHandler], None]:
"""Define an HTTP route which will respond to HTTP PUT requests."""
if opts is None:
opts = MethodOptions()

def decorator(function: HttpHandler):
def decorator(function: HttpHandler) -> None:
r = self._route(match)
r.put(function, opts=opts)

Expand Down Expand Up @@ -311,31 +313,31 @@ def __init__(self, api: Api, path: str, opts: RouteOptions):

def method(
self, methods: List[HttpMethod], *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None
):
) -> None:
"""Register middleware for multiple HTTP Methods."""
return Method(self, methods, *middleware, opts=opts).start()

def get(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None):
def get(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None) -> None:
"""Register middleware for HTTP GET requests."""
return self.method([HttpMethod.GET], *middleware, opts=opts)

def post(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None):
def post(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None) -> None:
"""Register middleware for HTTP POST requests."""
return self.method([HttpMethod.POST], *middleware, opts=opts)

def put(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None):
def put(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None) -> None:
"""Register middleware for HTTP PUT requests."""
return self.method([HttpMethod.PUT], *middleware, opts=opts)

def patch(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None):
def patch(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None) -> None:
"""Register middleware for HTTP PATCH requests."""
return self.method([HttpMethod.PATCH], *middleware, opts=opts)

def delete(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None):
def delete(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None) -> None:
"""Register middleware for HTTP DELETE requests."""
return self.method([HttpMethod.DELETE], *middleware, opts=opts)

def options(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None):
def options(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None) -> None:
"""Register middleware for HTTP OPTIONS requests."""
return self.method([HttpMethod.OPTIONS], *middleware, opts=opts)

Expand All @@ -361,7 +363,7 @@ def __init__(
self.server = FunctionServer(ApiWorkerOptions(route.api.name, route.path, methods, opts))
self.server.http(*route.api.middleware, *route.middleware, *middleware)

def start(self):
def start(self) -> None:
"""Start the server which will respond to incoming requests."""
Nitric._register_worker(self.server) # type: ignore

Expand Down
10 changes: 6 additions & 4 deletions nitric/resources/buckets.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@

from nitric.exception import exception_from_grpc_error
from nitric.api.storage import BucketRef, Storage
from typing import List, Union
from typing import List, Union, Callable
from enum import Enum
from grpclib import GRPCError

from nitric.application import Nitric
from nitric.faas import FunctionServer, BucketNotificationWorkerOptions, Middleware, BucketNotificationContext
from nitric.faas import FunctionServer, BucketNotificationWorkerOptions, BucketNotificationHandler
from nitric.proto.nitric.resource.v1 import (
Resource,
ResourceType,
Expand Down Expand Up @@ -87,11 +87,13 @@ def allow(self, *args: Union[BucketPermission, str]) -> BucketRef:

return Storage().bucket(self.name)

def on(self, notification_type: str, notification_prefix_filter: str):
def on(
self, notification_type: str, notification_prefix_filter: str
) -> Callable[[BucketNotificationHandler], None]:
"""Create and return a bucket notification decorator for this bucket."""
print("this has been called")

def decorator(func: Middleware[BucketNotificationContext]):
def decorator(func: BucketNotificationHandler) -> None:
print("this has been called")
self._server = FunctionServer(
BucketNotificationWorkerOptions(
Expand Down
8 changes: 4 additions & 4 deletions nitric/resources/topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

from nitric.api.events import Events, TopicRef
from nitric.exception import exception_from_grpc_error
from typing import List, Union
from typing import List, Union, Callable
from enum import Enum
from grpclib import GRPCError
from nitric.application import Nitric
from nitric.faas import Middleware, FunctionServer, SubscriptionWorkerOptions, EventContext
from nitric.faas import FunctionServer, SubscriptionWorkerOptions, EventHandler
from nitric.proto.nitric.resource.v1 import (
Resource,
ResourceType,
Expand Down Expand Up @@ -80,10 +80,10 @@ def allow(self, *args: Union[TopicPermission, str]) -> TopicRef:

return Events().topic(self.name)

def subscribe(self):
def subscribe(self) -> Callable[[EventHandler], None]:
"""Create and return a subscription decorator for this topic."""

def decorator(func: Middleware[EventContext]):
def decorator(func: EventHandler) -> None:
server = FunctionServer(SubscriptionWorkerOptions(topic=self.name))
server.event(func)
# type ignored because the register call is treated as protected.
Expand Down

0 comments on commit ab67989

Please sign in to comment.