예제 #1
0
class ViewMeta(keepasync(*HTTP_METHOD_NAMES)):  # type: ignore
    def __init__(
        cls,
        name: str,
        bases: typing.Tuple[type],
        namespace: typing.Dict[str, typing.Any],
    ):
        for function_name in filter(lambda key: key in HTTP_METHOD_NAMES,
                                    namespace.keys()):
            function = namespace[function_name]
            namespace[function_name] = parse_params(function)

        setattr(
            cls,
            "__methods__",
            [m.upper() for m in HTTP_METHOD_NAMES if hasattr(cls, m)],
        )

        super().__init__(name, bases, namespace)
예제 #2
0
import typing

from indexpy.concurrency import keepasync

from .request import WebSocket

MiddlewareMeta = keepasync("before_accept", "after_close")


class MiddlewareMixin(metaclass=MiddlewareMeta):  # type: ignore

    mounts: typing.Sequence[typing.Callable] = ()

    def __init__(self, get_response: typing.Callable) -> None:
        self.get_response = self.mount_middleware(get_response)

    def mount_middleware(self,
                         get_response: typing.Callable) -> typing.Callable:
        for middleware in reversed(self.mounts):
            get_response = middleware(get_response)
        return get_response

    async def __call__(self, websocket: WebSocket) -> None:
        await self.before_accept(websocket)
        await self.get_response(websocket)
        await self.after_close(websocket)

    async def before_accept(self, websocket: WebSocket) -> None:
        """
        Called before calling websocket handler
        """
예제 #3
0
 class Base(metaclass=keepasync("hello", "test")):
     def hello(self):
         pass
예제 #4
0
    class Test(metaclass=keepasync("hello", "test")):
        def hello(self):
            pass

        async def test(self):
            pass
예제 #5
0
from __future__ import annotations

import typing

from indexpy.concurrency import keepasync

if typing.TYPE_CHECKING:
    from .request import Request

from .responses import Response, convert_response

MiddlewareMeta = keepasync("process_request", "process_response",
                           "process_exception")


class MiddlewareMixin(metaclass=MiddlewareMeta):  # type: ignore

    mounts: typing.Sequence[typing.Callable] = ()

    def __init__(self, get_response: typing.Callable) -> None:
        self.get_response = self.mount_middleware(get_response)

    def mount_middleware(self,
                         get_response: typing.Callable) -> typing.Callable:
        for middleware in reversed(self.mounts):
            get_response = middleware(get_response)
        return get_response

    async def __call__(self, request: Request) -> Response:
        response = await self.process_request(request)
예제 #6
0
from __future__ import annotations

import typing

from indexpy.concurrency import keepasync

if typing.TYPE_CHECKING:
    from .request import WebSocket

MiddlewareMeta = keepasync("before_accept", "after_close", "catch_error")


class MiddlewareMixin(metaclass=MiddlewareMeta):  # type: ignore

    mounts: typing.Sequence[typing.Callable] = ()

    def __init__(self, websocket_handler: typing.Callable) -> None:
        self.websocket_handler = self.mount_middleware(websocket_handler)

    def mount_middleware(
            self, websocket_handler: typing.Callable) -> typing.Callable:
        for middleware in reversed(self.mounts):
            websocket_handler = middleware(websocket_handler)
        return websocket_handler

    async def __call__(self, websocket: WebSocket) -> None:
        await self.before_accept(websocket)
        try:
            await self.websocket_handler(websocket)
        except Exception as exc:
            await self.catch_error(websocket, exc)