Skip to content

Commit

Permalink
Merge branch 'base-resource-refactor' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
tjholm committed Oct 27, 2022
2 parents 554ea94 + 665e4b9 commit 664a16f
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 47 deletions.
32 changes: 17 additions & 15 deletions nitric/resources/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@
from nitricapi.nitric.resource.v1 import (
Resource,
ResourceType,
ResourceServiceStub,
ApiResource,
ApiScopes,
ApiSecurityDefinition,
ApiSecurityDefinitionJwt, ResourceDeclareRequest
)
from nitric.utils import new_default_channel
from grpclib import GRPCError
from nitric.api.exception import exception_from_grpc_error


class JwtSecurityDefinition:
"""Represents the JWT security definition for an API."""

Expand All @@ -56,11 +55,11 @@ class ApiOptions:
security: Union[dict[str, List[str]], None]

def __init__(
self,
path: str = "",
middleware: List[Middleware] = None,
security_definitions: dict[str, SecurityDefinition] = None,
security: dict[str, List[str]] = None,
self,
path: str = "",
middleware: List[Middleware] = None,
security_definitions: dict[str, SecurityDefinition] = None,
security: dict[str, List[str]] = None,
):
"""Construct a new API options object."""
self.middleware = middleware
Expand All @@ -83,7 +82,8 @@ def _to_resource(b: Api) -> Resource:
return Resource(name=b.name, type=ResourceType.Api)


def security_definition_to_grpc_declaration(security_definitions: dict[str,SecurityDefinition]) -> Union[dict[str, ApiSecurityDefinition], None]:
def security_definition_to_grpc_declaration(security_definitions: dict[str, SecurityDefinition]) -> Union[
dict[str, ApiSecurityDefinition], None]:
if security_definitions is None or len(security_definitions) == 0:
return None
return {
Expand Down Expand Up @@ -114,6 +114,7 @@ class Api(BaseResource):

def __init__(self, name: str, opts: ApiOptions = None):
"""Construct a new HTTP API."""
super().__init__()
if opts is None:
opts = ApiOptions()

Expand All @@ -138,7 +139,6 @@ async def _register(self):
except GRPCError as grpc_err:
raise exception_from_grpc_error(grpc_err)


def _route(self, match: str, opts: RouteOptions = None) -> Route:
"""Define an HTTP route to be handled by this API."""
if opts is None:
Expand All @@ -155,8 +155,9 @@ def all(self, match: str, opts: MethodOptions = None):

def decorator(function: HttpMiddleware):
r = self._route(match)
r.method([HttpMethod.GET, HttpMethod.POST, HttpMethod.PATCH, HttpMethod.PUT, HttpMethod.DELETE, HttpMethod.OPTIONS], function, opts)

r.method([HttpMethod.GET, HttpMethod.POST, HttpMethod.PATCH, HttpMethod.PUT, HttpMethod.DELETE,
HttpMethod.OPTIONS], function, opts)

return decorator

def methods(self, methods: List[HttpMethod], match: str, opts: MethodOptions = None):
Expand All @@ -167,7 +168,7 @@ def methods(self, methods: List[HttpMethod], match: str, opts: MethodOptions = N
def decorator(function: HttpMiddleware):
r = self._route(match)
r.method(methods, function, opts)

return decorator

def get(self, match: str, opts: MethodOptions = None):
Expand Down Expand Up @@ -224,7 +225,7 @@ def decorator(function: HttpMiddleware):
r.patch(function, opts=opts)

return decorator

def put(self, match: str, opts: MethodOptions = None):
"""Define an HTTP route which will respond to HTTP PUT requests."""
if opts is None:
Expand Down Expand Up @@ -288,7 +289,7 @@ class Method:
opts: MethodOptions

def __init__(
self, route: Route, methods: List[HttpMethod], *middleware: HttpMiddleware, opts: MethodOptions = None
self, route: Route, methods: List[HttpMethod], *middleware: HttpMiddleware, opts: MethodOptions = None
):
"""Construct a method handler for the specified route."""
self.route = route
Expand All @@ -300,6 +301,7 @@ def start(self):
"""Start the server which will respond to incoming requests."""
Nitric._register_worker(self.server)


def api(name: str) -> Api:
"""Create a new API resource"""
return Nitric._create_resource(Api, name)
return Nitric._create_resource(Api, name)
2 changes: 1 addition & 1 deletion nitric/resources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from abc import ABC, abstractmethod
from asyncio import Task

from typing import TypeVar, Type, Coroutine, Union, List
from typing import TypeVar, Type, Union, List

from grpclib import GRPCError
from nitricapi.nitric.resource.v1 import Action, PolicyResource, Resource, ResourceType, ResourceDeclareRequest, \
Expand Down
7 changes: 1 addition & 6 deletions nitric/resources/buckets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,20 @@
#
from __future__ import annotations

import asyncio

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

from nitric.application import Nitric
from nitric.utils import new_default_channel
from nitricapi.nitric.resource.v1 import (
Resource,
ResourceServiceStub,
PolicyResource,
ResourceType,
Action, ResourceDeclareRequest,
)

from nitric.resources.base import BaseResource, SecureResource
from nitric.resources.base import SecureResource


class BucketPermission(Enum):
Expand Down
10 changes: 1 addition & 9 deletions nitric/resources/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,13 @@
from grpclib import GRPCError

from nitric.application import Nitric
from nitric.utils import new_default_channel
from nitricapi.nitric.resource.v1 import (
Resource,
ResourceServiceStub,
PolicyResource,
ResourceType,
Action, ResourceDeclareRequest,
)

from nitric.resources.base import BaseResource, SecureResource
from nitric.resources.base import SecureResource


class CollectionPermission(Enum):
Expand All @@ -47,11 +44,6 @@ class CollectionPermission(Enum):
deleting = "deleting"







class Collection(SecureResource):
"""A document collection resource."""

Expand Down
7 changes: 1 addition & 6 deletions nitric/resources/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,19 @@
#
from __future__ import annotations

import asyncio

from nitric.api.exception import exception_from_grpc_error
from typing import List, Union
from enum import Enum
from grpclib import GRPCError
from nitric.api.queues import QueueRef, Queues
from nitric.application import Nitric
from nitric.utils import new_default_channel
from nitricapi.nitric.resource.v1 import (
Resource,
ResourceServiceStub,
PolicyResource,
ResourceType,
Action, ResourceDeclareRequest,
)

from nitric.resources.base import BaseResource, SecureResource
from nitric.resources.base import SecureResource


class QueuePermission(Enum):
Expand Down
5 changes: 1 addition & 4 deletions nitric/resources/secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,14 @@
from grpclib import GRPCError

from nitric.application import Nitric
from nitric.utils import new_default_channel
from nitric.api.secrets import Secrets, SecretContainerRef
from nitricapi.nitric.resource.v1 import (
Resource,
ResourceServiceStub,
PolicyResource,
ResourceType,
Action, ResourceDeclareRequest,
)

from nitric.resources.base import BaseResource, SecureResource
from nitric.resources.base import SecureResource


class SecretPermission(Enum):
Expand Down
7 changes: 1 addition & 6 deletions nitric/resources/topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,20 @@
#
from __future__ import annotations

import asyncio

from nitric.api.events import Events, TopicRef
from nitric.api.exception import exception_from_grpc_error
from typing import List, Union
from enum import Enum
from grpclib import GRPCError
from nitric.application import Nitric
from nitric.faas import EventMiddleware, FunctionServer, SubscriptionWorkerOptions
from nitric.utils import new_default_channel
from nitricapi.nitric.resource.v1 import (
Resource,
ResourceServiceStub,
PolicyResource,
ResourceType,
Action, ResourceDeclareRequest,
)

from nitric.resources.base import BaseResource, SecureResource
from nitric.resources.base import SecureResource


class TopicPermission(Enum):
Expand Down

0 comments on commit 664a16f

Please sign in to comment.