Route   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 4
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 4
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 3 1
1
import abc
2
from typing import Callable
3
4
from mountapi.core import exceptions
5
from mountapi.endpoints import AbstractEndpoint
6
from mountapi.schema import AbstractSchema
7
8
9
class Route:
10
    def __init__(self, path, endpoint) -> None:
11
        self.path = path
12
        self.endpoint = endpoint
13
14
15
class AbstractRouter(exceptions.NotImplementedMixin, metaclass=abc.ABCMeta):
16
    @abc.abstractmethod
17
    def dispatch(self, path: str, method: str) -> Callable:
18
        raise self.not_implemented()
19
20
21
class Router(AbstractRouter):
22
    def __init__(self, schema: AbstractSchema) -> None:
23
        self._schema = schema
24
25
    def dispatch(self, path: str, method: str) -> Callable:
26
        match_result = self._schema.match(path)
27
        endpoint_kwargs = match_result['kwargs']
28
        endpoint_obj = match_result['endpoint'](**endpoint_kwargs)
29
        if self._is_accepted_method(endpoint_obj, method):
30
            return getattr(endpoint_obj, method.lower())
31
        else:
32
            raise exceptions.MethodNotAllowed()
33
34
    def _is_accepted_method(self, endpoint_obj: AbstractEndpoint, method: str):
35
        return method in endpoint_obj.allowed_methods
36