Runner.__init__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 2
rs 10
1
import abc
2
from typing import Type
3
4
from werkzeug.serving import run_simple
5
from werkzeug.wrappers import Request as WSGIRequest
6
from werkzeug.wrappers import Response as WSGIResponse
7
8
from mountapi.core.settings import AbstractSettings
9
from mountapi.http import RequestData, Request, Response
10
from mountapi.routing import AbstractRouter
11
12
13
class AbstractRunner(metaclass=abc.ABCMeta):
14
    @abc.abstractmethod
15
    def run(self, settings: Type[AbstractSettings]) -> None:
16
        pass
17
18
19
class Runner(AbstractRunner):
20
    def __init__(self, router: AbstractRouter) -> None:
21
        self._router = router
22
23
    def run(self, settings: Type[AbstractSettings]) -> None:
24
        run_simple(
25
            hostname=settings.hostname,
26
            port=settings.port,
27
            application=self._get_wsgi_app(),
28
            use_reloader=True,
29
            use_debugger=settings.debug,
30
        )
31
32
    def _get_wsgi_app(self):
33
        @WSGIRequest.application
34
        def application(wsgi_request: WSGIRequest) -> WSGIResponse:
35
            request_data = RequestData(wsgi_request)
36
            request_func = self._router.dispatch(
37
                request_data.path, request_data.method
38
            )
39
            request = Request(request_data, request_func)
40
            response = Response(request)
41
            return response.wsgi()
42
43
        return application
44