|
@@ -0,0 +1,131 @@
|
|
1
|
+# -*- coding: utf-8 -*-
|
|
2
|
+import json
|
|
3
|
+from http import HTTPStatus
|
|
4
|
+
|
|
5
|
+from pyramid.config import Configurator
|
|
6
|
+from wsgiref.simple_server import make_server
|
|
7
|
+import time
|
|
8
|
+from datetime import datetime
|
|
9
|
+import hapic
|
|
10
|
+from example.fake_api.schema import *
|
|
11
|
+from hapic.data import HapicData
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+class NoContentException(Exception):
|
|
15
|
+ pass
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+class Controllers(object):
|
|
19
|
+ @hapic.with_api_doc()
|
|
20
|
+ @hapic.output_body(AboutResponseSchema())
|
|
21
|
+ def about(self, context, request):
|
|
22
|
+ """
|
|
23
|
+ General information about this API.
|
|
24
|
+ """
|
|
25
|
+ return {
|
|
26
|
+ 'version': '1.2.3',
|
|
27
|
+ 'datetime': datetime.now(),
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ @hapic.with_api_doc()
|
|
31
|
+ @hapic.output_body(ListsUserSchema())
|
|
32
|
+ def get_users(self, context, request):
|
|
33
|
+ """
|
|
34
|
+ Obtain users list.
|
|
35
|
+ """
|
|
36
|
+ return {
|
|
37
|
+ 'item_nb': 1,
|
|
38
|
+ 'items': [
|
|
39
|
+ {
|
|
40
|
+ 'id': 4,
|
|
41
|
+ 'username': 'some_user',
|
|
42
|
+ 'display_name': 'Damien Accorsi',
|
|
43
|
+ 'company': 'Algoo',
|
|
44
|
+ },
|
|
45
|
+ ],
|
|
46
|
+ 'pagination': {
|
|
47
|
+ 'first_id': 0,
|
|
48
|
+ 'last_id': 5,
|
|
49
|
+ 'current_id': 0,
|
|
50
|
+ }
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ @hapic.with_api_doc()
|
|
54
|
+ @hapic.input_path(UserPathSchema())
|
|
55
|
+ @hapic.output_body(UserSchema())
|
|
56
|
+ def get_user(self, context, request, hapic_data: HapicData):
|
|
57
|
+ """
|
|
58
|
+ Obtain one user
|
|
59
|
+ """
|
|
60
|
+ return {
|
|
61
|
+ 'id': 4,
|
|
62
|
+ 'username': 'some_user',
|
|
63
|
+ 'email_address': 'some.user@hapic.com',
|
|
64
|
+ 'first_name': 'Damien',
|
|
65
|
+ 'last_name': 'Accorsi',
|
|
66
|
+ 'display_name': 'Damien Accorsi',
|
|
67
|
+ 'company': 'Algoo',
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+ @hapic.with_api_doc()
|
|
71
|
+ # TODO - G.M - 2017-12-5 - Support input_forms ?
|
|
72
|
+ # TODO - G.M - 2017-12-5 - Support exclude, only ?
|
|
73
|
+ @hapic.input_body(UserSchema(exclude=('id',)))
|
|
74
|
+ @hapic.output_body(UserSchema())
|
|
75
|
+ def add_user(self, context, request, hapic_data: HapicData):
|
|
76
|
+ """
|
|
77
|
+ Add new user
|
|
78
|
+ """
|
|
79
|
+ return {
|
|
80
|
+ 'id': 4,
|
|
81
|
+ 'username': 'some_user',
|
|
82
|
+ 'email_address': 'some.user@hapic.com',
|
|
83
|
+ 'first_name': 'Damien',
|
|
84
|
+ 'last_name': 'Accorsi',
|
|
85
|
+ 'display_name': 'Damien Accorsi',
|
|
86
|
+ 'company': 'Algoo',
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ @hapic.with_api_doc()
|
|
90
|
+ @hapic.handle_exception(NoContentException, http_code=HTTPStatus.NO_CONTENT)
|
|
91
|
+ @hapic.input_path(UserPathSchema())
|
|
92
|
+ def del_user(self, context, request, hapic_data: HapicData):
|
|
93
|
+ """
|
|
94
|
+ delete user
|
|
95
|
+ """
|
|
96
|
+
|
|
97
|
+ # TODO - G.M - 2017-12-05 - Add better
|
|
98
|
+ # way to doc response of this function, using response object ?
|
|
99
|
+ # return bottle.Response(
|
|
100
|
+ # status=204,
|
|
101
|
+ # )
|
|
102
|
+ raise NoContentException
|
|
103
|
+
|
|
104
|
+ def bind(self, configurator: Configurator):
|
|
105
|
+ configurator.add_route('about', '/about', request_method='GET')
|
|
106
|
+ configurator.add_view(self.about, route_name='about', renderer='json')
|
|
107
|
+
|
|
108
|
+ configurator.add_route('get_users', '/users', request_method='GET') # nopep8
|
|
109
|
+ configurator.add_view(self.get_users, route_name='get_users', renderer='json') # nopep8
|
|
110
|
+
|
|
111
|
+ configurator.add_route('get_user', '/users/{id}', request_method='GET') # nopep8
|
|
112
|
+ configurator.add_view(self.get_user, route_name='get_user', renderer='json') # nopep8
|
|
113
|
+
|
|
114
|
+ configurator.add_route('add_user', '/users/', request_method='POST') # nopep8
|
|
115
|
+ configurator.add_view(self.add_user, route_name='add_user', renderer='json') # nopep8
|
|
116
|
+
|
|
117
|
+ configurator.add_route('del_user', '/users/{id}', request_method='DELETE') # nopep8
|
|
118
|
+ configurator.add_view(self.del_user, route_name='del_user', renderer='json') # nopep8
|
|
119
|
+
|
|
120
|
+configurator = Configurator(autocommit=True)
|
|
121
|
+controllers = Controllers()
|
|
122
|
+controllers.bind(configurator)
|
|
123
|
+hapic.set_context(hapic.ext.pyramid.PyramidContext(configurator))
|
|
124
|
+time.sleep(1)
|
|
125
|
+s = json.dumps(hapic.generate_doc())
|
|
126
|
+time.sleep(1)
|
|
127
|
+# print swagger doc
|
|
128
|
+print(s)
|
|
129
|
+# Run app
|
|
130
|
+server = make_server('0.0.0.0', 8081, configurator.make_wsgi_app())
|
|
131
|
+server.serve_forever()
|