| 1 |  |  | # -*- coding: utf-8 -*- | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 2 |  |  | # TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop this file | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 3 |  |  | from datetime import datetime | 
            
                                                        
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 5 |  |  | from pyramid.config import Configurator | 
            
                                                        
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 7 |  |  | from hapic.data import HapicData | 
            
                                                        
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 9 |  |  | from tracim.extensions import hapic | 
            
                                                        
            
                                    
            
            
                | 10 |  |  | from tracim.views.controllers import Controller | 
            
                                                        
            
                                    
            
            
                | 11 |  |  | from tracim.views.example_api.schema import * | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 14 |  |  | class ExampleApiController(Controller): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 16 |  |  |     @hapic.with_api_doc() | 
            
                                                        
            
                                    
            
            
                | 17 |  |  |     @hapic.output_body(AboutResponseSchema()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 18 |  |  |     def about(self, context, request): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 19 |  |  |         """ | 
            
                                                        
            
                                    
            
            
                | 20 |  |  |         General information about this API. | 
            
                                                        
            
                                    
            
            
                | 21 |  |  |         """ | 
            
                                                        
            
                                    
            
            
                | 22 |  |  |         return { | 
            
                                                        
            
                                    
            
            
                | 23 |  |  |             'version': '1.2.3', | 
            
                                                        
            
                                    
            
            
                | 24 |  |  |             'datetime': datetime(2017, 12, 7, 10, 55, 8, 488996), | 
            
                                                        
            
                                    
            
            
                | 25 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |     @hapic.with_api_doc() | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |     @hapic.output_body(ListsUserSchema()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |     def get_users(self, context, request): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 30 |  |  |         """ | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |         Obtain users list. | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |         """ | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |         return { | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |             'item_nb': 1, | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |             'items': [ | 
            
                                                        
            
                                    
            
            
                | 36 |  |  |                 { | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |                     'id': 4, | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |                     'username': 'some_user', | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |                     'display_name': 'Damien Accorsi', | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |                     'company': 'Algoo', | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |                 }, | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |             ], | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |             'pagination': { | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |                 'first_id': 0, | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |                 'last_id': 5, | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |                 'current_id': 0, | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |     @hapic.with_api_doc() | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |     @hapic.input_path(UserPathSchema()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |     @hapic.output_body(UserSchema()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |     def get_user(self, context, request, hapic_data: HapicData): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |         """ | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |         Obtain one user | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |         """ | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |         return { | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |              'id': 4, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |              'username': 'some_user', | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |              'email_address': '[email protected]', | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |              'first_name': 'Damien', | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |              'last_name': 'Accorsi', | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |              'display_name': 'Damien Accorsi', | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |              'company': 'Algoo', | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |     @hapic.with_api_doc() | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |     # TODO - G.M - 2017-12-5 - Support input_forms ? | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |     # TODO - G.M - 2017-12-5 - Support exclude, only ? | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |     @hapic.input_body(UserSchema(exclude=('id',))) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |     @hapic.output_body(UserSchema()) | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |     def add_user(self, context, request, hapic_data: HapicData): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |         """ | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |         Add new user | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |         """ | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |         return { | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |              'id': 4, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |              'username': 'some_user', | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |              'email_address': '[email protected]', | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |              'first_name': 'Damien', | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |              'last_name': 'Accorsi', | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |              'display_name': 'Damien Accorsi', | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |              'company': 'Algoo', | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |     @hapic.with_api_doc() | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |     @hapic.output_body(NoContentSchema(), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |                        default_http_code=204) | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |     @hapic.input_path(UserPathSchema()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |     def del_user(self, context, request, hapic_data: HapicData): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |         """ | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |         delete user | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |         """ | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |         return NoContentSchema() | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |     def bind(self, configurator: Configurator): | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |         configurator.add_route('about', '/about', request_method='GET') | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |         configurator.add_view(self.about, route_name='about', renderer='json') | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |         configurator.add_route('get_users', '/users', request_method='GET')  # nopep8 | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |         configurator.add_view(self.get_users, route_name='get_users', renderer='json')  # nopep8 | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |         configurator.add_route('get_user', '/users/{id}', request_method='GET')  # nopep8 | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |         configurator.add_view(self.get_user, route_name='get_user', renderer='json')  # nopep8 | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |         configurator.add_route('add_user', '/users/', request_method='POST')  # nopep8 | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |         configurator.add_view(self.add_user, route_name='add_user', renderer='json')  # nopep8 | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |         configurator.add_route('del_user', '/users/{id}', request_method='DELETE')  # nopep8 | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |         configurator.add_view(self.del_user, route_name='del_user', renderer='json')  # nopep8 | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |  | 
            
                        
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.