FlaskBlueprintIntegration.magic_route()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 10
nop 3
dl 17
loc 17
ccs 0
cts 8
cp 0
crap 6
rs 9.9
c 0
b 0
f 0
1
"""
2
Extra pieces for the flask integration that aren't quite stable.
3
For the stable code for flask see lagom.integrations.flask
4
"""
5
6
from typing import List, Type, Optional, Callable, Dict
7
8
from flask.blueprints import Blueprint
9
10
from lagom.integrations.flask import FlaskIntegration
11
from lagom.interfaces import ExtendableContainer
12
13
14
class FlaskBlueprintIntegration(FlaskIntegration):
15
    """
16
    Wraps a flask blueprint and a container so that dependencies are provided to
17
    flask routes
18
    """
19
20
    blueprint: Blueprint
21
    _injection_map: Dict[Callable, Callable]
22
23
    def __init__(
24
        self,
25
        blueprint: Blueprint,
26
        container: ExtendableContainer,
27
        request_singletons: Optional[List[Type]] = None,
28
    ):
29
        """
30
        :param blueprint: The flask blueprint to provide dependency injection for
31
        :param request_singletons: A list of types that should be singletons for a request
32
        :param container: an existing container to provide dependencies
33
        """
34
        # A blueprint is not actually a flask app but we can pretend it is for this
35
        # integration.
36
        # TODO: Refactor this properly when moving out of experimental
37
        super().__init__(blueprint, container, request_singletons)  # type: ignore
38
        self.blueprint = blueprint
39
40
    def route(self, rule, **options):
41
        """Equivalent to the flask @route decorator
42
        Injectable arguments should be set by making the default value
43
        lagom.injectable
44
        """
45
46
        def _decorator(f):
47
            endpoint = options.pop("endpoint", f.__name__)
48
            if f not in self._injection_map:
49
                self._injection_map[f] = self._container.partial(
50
                    f, shared=self._request_singletons
51
                )
52
            self.blueprint.add_url_rule(
53
                rule, endpoint, self._injection_map[f], **options
54
            )
55
            return f
56
57
        return _decorator
58
59 View Code Duplication
    def magic_route(self, rule, **options):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
60
        """Equivalent to the flask @route decorator
61
        The injection container will try and bind all arguments
62
        """
63
64
        def _decorator(f):
65
            endpoint = options.pop("endpoint", f.__name__)
66
            if f not in self._injection_map:
67
                self._injection_map[f] = self._container.magic_partial(
68
                    f, shared=self._request_singletons
69
                )
70
            self.blueprint.add_url_rule(
71
                rule, endpoint, self._injection_map[f], **options
72
            )
73
            return f
74
75
        return _decorator
76