Completed
Push — master ( fe6d7a...5e9dbb )
by
unknown
02:04
created

tracim.views.example_api.example_api_controller   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 69
dl 0
loc 110
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A ExampleApiController.get_user() 0 15 1
A ExampleApiController.get_users() 0 20 1
A ExampleApiController.del_user() 0 9 1
A ExampleApiController.bind() 0 15 1
A ExampleApiController.add_user() 0 17 1
A ExampleApiController.about() 0 9 1
1
# -*- coding: utf-8 -*-
2
from datetime import datetime
3
4
from pyramid.config import Configurator
5
6
from hapic.data import HapicData
7
8
from tracim.extensions import hapic
9
from tracim.views.controllers import Controller
10
from tracim.views.example_api.schema import *
11
12
13
class ExampleApiController(Controller):
14
15
    @hapic.with_api_doc()
16
    @hapic.output_body(AboutResponseSchema())
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable AboutResponseSchema does not seem to be defined.
Loading history...
17
    def about(self, context, request):
18
        """
19
        General information about this API.
20
        """
21
        return {
22
            'version': '1.2.3',
23
            'datetime': datetime(2017, 12, 7, 10, 55, 8, 488996),
24
        }
25
26
    @hapic.with_api_doc()
27
    @hapic.output_body(ListsUserSchema())
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ListsUserSchema does not seem to be defined.
Loading history...
28
    def get_users(self, context, request):
29
        """
30
        Obtain users list.
31
        """
32
        return {
33
            'item_nb': 1,
34
            'items': [
35
                {
36
                    'id': 4,
37
                    'username': 'some_user',
38
                    'display_name': 'Damien Accorsi',
39
                    'company': 'Algoo',
40
                },
41
            ],
42
            'pagination': {
43
                'first_id': 0,
44
                'last_id': 5,
45
                'current_id': 0,
46
            }
47
        }
48
49
    @hapic.with_api_doc()
50
    @hapic.input_path(UserPathSchema())
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable UserPathSchema does not seem to be defined.
Loading history...
51
    @hapic.output_body(UserSchema())
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable UserSchema does not seem to be defined.
Loading history...
52
    def get_user(self, context, request, hapic_data: HapicData):
53
        """
54
        Obtain one user
55
        """
56
        return {
57
             'id': 4,
58
             'username': 'some_user',
59
             'email_address': '[email protected]',
60
             'first_name': 'Damien',
61
             'last_name': 'Accorsi',
62
             'display_name': 'Damien Accorsi',
63
             'company': 'Algoo',
64
        }
65
66
    @hapic.with_api_doc()
67
    # TODO - G.M - 2017-12-5 - Support input_forms ?
68
    # TODO - G.M - 2017-12-5 - Support exclude, only ?
69
    @hapic.input_body(UserSchema(exclude=('id',)))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable UserSchema does not seem to be defined.
Loading history...
70
    @hapic.output_body(UserSchema())
71
    def add_user(self, context, request, hapic_data: HapicData):
72
        """
73
        Add new user
74
        """
75
        return {
76
             'id': 4,
77
             'username': 'some_user',
78
             'email_address': '[email protected]',
79
             'first_name': 'Damien',
80
             'last_name': 'Accorsi',
81
             'display_name': 'Damien Accorsi',
82
             'company': 'Algoo',
83
        }
84
85
    @hapic.with_api_doc()
86
    @hapic.output_body(NoContentSchema(),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable NoContentSchema does not seem to be defined.
Loading history...
87
                       default_http_code=204)
88
    @hapic.input_path(UserPathSchema())
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable UserPathSchema does not seem to be defined.
Loading history...
89
    def del_user(self, context, request, hapic_data: HapicData):
90
        """
91
        delete user
92
        """
93
        return NoContentSchema()
94
95
    def bind(self, configurator: Configurator):
96
        configurator.add_route('about', '/about', request_method='GET')
97
        configurator.add_view(self.about, route_name='about', renderer='json')
98
99
        configurator.add_route('get_users', '/users', request_method='GET')  # nopep8
100
        configurator.add_view(self.get_users, route_name='get_users', renderer='json')  # nopep8
101
102
        configurator.add_route('get_user', '/users/{id}', request_method='GET')  # nopep8
103
        configurator.add_view(self.get_user, route_name='get_user', renderer='json')  # nopep8
104
105
        configurator.add_route('add_user', '/users/', request_method='POST')  # nopep8
106
        configurator.add_view(self.add_user, route_name='add_user', renderer='json')  # nopep8
107
108
        configurator.add_route('del_user', '/users/{id}', request_method='DELETE')  # nopep8
109
        configurator.add_view(self.del_user, route_name='del_user', renderer='json')  # nopep8
110