Completed
Push — master ( e36a68...7e8a0c )
by Piotr
01:12
created

Runner   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 7 1
A __init__() 0 2 1
A _get_wsgi_app() 0 12 2
A application() 0 9 1
1
from typing import Type
2
3
from werkzeug.serving import run_simple
4
from werkzeug.wrappers import Request as WSGIRequest
5
from werkzeug.wrappers import Response as WSGIResponse
6
7
from mountapi.http import RequestData, Request, Response
8
from mountapi.core.settings import AbstractSettings
9
from mountapi.routing import AbstractRouter
10
11
12
class Runner:
13
    def __init__(self, router: AbstractRouter) -> None:
14
        self._router = router
15
16
    def run(self, settings: Type[AbstractSettings]) -> None:
17
        run_simple(
18
            hostname=settings.hostname,
19
            port=settings.port,
20
            application=self._get_wsgi_app(),
21
            use_reloader=True,
22
            use_debugger=settings.debug,
23
        )
24
25
    def _get_wsgi_app(self):
26
        @WSGIRequest.application
27
        def application(wsgi_request: WSGIRequest) -> WSGIResponse:
28
            request_data = RequestData(wsgi_request)
29
            request_func = self._router.dispatch(
30
                request_data.path, request_data.method
31
            )
32
            request = Request(request_data, request_func)
33
            response = Response(request)
34
            return response.wsgi()
35
36
        return application
37