|
@@ -0,0 +1,174 @@
|
|
1
|
+# -*- coding: utf-8 -*-
|
|
2
|
+import json
|
|
3
|
+from http import HTTPStatus
|
|
4
|
+
|
|
5
|
+#import bottle
|
|
6
|
+from pyramid.config import Configurator
|
|
7
|
+from wsgiref.simple_server import make_server
|
|
8
|
+import time
|
|
9
|
+import yaml
|
|
10
|
+import uuid
|
|
11
|
+from beaker.middleware import SessionMiddleware
|
|
12
|
+
|
|
13
|
+import hapic
|
|
14
|
+from example import HelloResponseSchema, HelloPathSchema, HelloJsonSchema, \
|
|
15
|
+ ErrorResponseSchema, HelloQuerySchema
|
|
16
|
+from hapic.data import HapicData
|
|
17
|
+
|
|
18
|
+# hapic.global_exception_handler(UnAuthExc, StandardErrorSchema)
|
|
19
|
+# hapic.global_exception_handler(UnAuthExc2, StandardErrorSchema)
|
|
20
|
+# hapic.global_exception_handler(UnAuthExc3, StandardErrorSchema)
|
|
21
|
+# bottle.default_app.push(app)
|
|
22
|
+
|
|
23
|
+# session_opts = {
|
|
24
|
+# 'session.type': 'file',
|
|
25
|
+# 'session.data_dir': '/tmp',
|
|
26
|
+# 'session.cookie_expires': 3600,
|
|
27
|
+# 'session.auto': True
|
|
28
|
+# }
|
|
29
|
+# session_middleware = SessionMiddleware(
|
|
30
|
+# app,
|
|
31
|
+# session_opts,
|
|
32
|
+# environ_key='beaker.session',
|
|
33
|
+# key='beaker.session.id',
|
|
34
|
+# )
|
|
35
|
+# app = session_middleware.wrap_app
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+def bob(f):
|
|
39
|
+ def boby(*args, **kwargs):
|
|
40
|
+ return f(*args, **kwargs)
|
|
41
|
+ return boby
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+class Controllers(object):
|
|
45
|
+ @hapic.with_api_doc()
|
|
46
|
+ # @hapic.ext.bottle.bottle_context()
|
|
47
|
+ @hapic.handle_exception(ZeroDivisionError, http_code=HTTPStatus.BAD_REQUEST)
|
|
48
|
+ @hapic.input_path(HelloPathSchema())
|
|
49
|
+ @hapic.input_query(HelloQuerySchema())
|
|
50
|
+ @hapic.output_body(HelloResponseSchema())
|
|
51
|
+ def hello(self, context, request, hapic_data: HapicData):
|
|
52
|
+ """
|
|
53
|
+ my endpoint hello
|
|
54
|
+ ---
|
|
55
|
+ get:
|
|
56
|
+ description: my description
|
|
57
|
+ parameters:
|
|
58
|
+ - in: "path"
|
|
59
|
+ description: "hello"
|
|
60
|
+ name: "name"
|
|
61
|
+ type: "string"
|
|
62
|
+ responses:
|
|
63
|
+ 200:
|
|
64
|
+ description: A pet to be returned
|
|
65
|
+ schema: HelloResponseSchema
|
|
66
|
+ """
|
|
67
|
+ name = request.matchdict.get('name', None)
|
|
68
|
+ if name == 'zero':
|
|
69
|
+ raise ZeroDivisionError('Don\'t call him zero !')
|
|
70
|
+
|
|
71
|
+ return {
|
|
72
|
+ 'sentence': 'Hello !',
|
|
73
|
+ 'name': name,
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ @hapic.with_api_doc()
|
|
77
|
+ # @hapic.ext.bottle.bottle_context()
|
|
78
|
+ # @hapic.error_schema(ErrorResponseSchema())
|
|
79
|
+ @hapic.input_path(HelloPathSchema())
|
|
80
|
+ @hapic.input_body(HelloJsonSchema())
|
|
81
|
+ @hapic.output_body(HelloResponseSchema())
|
|
82
|
+ @bob
|
|
83
|
+ def hello2(self, context, request, hapic_data: HapicData):
|
|
84
|
+ name = request.matchdict.get('name', None)
|
|
85
|
+ return {
|
|
86
|
+ 'sentence': 'Hello !',
|
|
87
|
+ 'name': name,
|
|
88
|
+ 'color': hapic_data.body.get('color'),
|
|
89
|
+ }
|
|
90
|
+
|
|
91
|
+ kwargs = {'validated_data': {'name': 'bob'}, 'name': 'bob'}
|
|
92
|
+
|
|
93
|
+ @hapic.with_api_doc()
|
|
94
|
+ # @hapic.ext.bottle.bottle_context()
|
|
95
|
+ # @hapic.error_schema(ErrorResponseSchema())
|
|
96
|
+ @hapic.input_path(HelloPathSchema())
|
|
97
|
+ @hapic.output_body(HelloResponseSchema())
|
|
98
|
+ def hello3(self, context, request, hapic_data: HapicData):
|
|
99
|
+ name = request.matchdict.get('name', None)
|
|
100
|
+ return {
|
|
101
|
+ 'sentence': 'Hello !',
|
|
102
|
+ 'name': name,
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ def bind(self, app):
|
|
106
|
+ app.route('/hello/{name}', callback=self.hello)
|
|
107
|
+ app.route('/hello/{name}', callback=self.hello2, method='POST')
|
|
108
|
+ app.route('/hello3/{name}', callback=self.hello3)
|
|
109
|
+ app.config.include('pyramid_debugtoolbar')
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+class PyramRoute(object):
|
|
113
|
+
|
|
114
|
+ def __init__(self, app, rule, method, callback, name, **options):
|
|
115
|
+ self.app = app
|
|
116
|
+ self.rule = rule
|
|
117
|
+ self.method = method
|
|
118
|
+ self.callback = callback
|
|
119
|
+ self.name = name
|
|
120
|
+
|
|
121
|
+ if not self.name:
|
|
122
|
+ self.name = str(uuid.uuid4())
|
|
123
|
+
|
|
124
|
+ with self.app.config as config:
|
|
125
|
+ config.add_route(self.name, self.rule, request_method=self.method)
|
|
126
|
+ config.add_view(
|
|
127
|
+ self.callback, route_name=self.name, renderer='json')
|
|
128
|
+ #import pdb; pdb.set_trace()
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+class Pyramidapp(object):
|
|
132
|
+
|
|
133
|
+ def __init__(self):
|
|
134
|
+ self.config = Configurator()
|
|
135
|
+ self.routes = []
|
|
136
|
+
|
|
137
|
+ def route(self,
|
|
138
|
+ rule,
|
|
139
|
+ callback,
|
|
140
|
+ method='GET',
|
|
141
|
+ name=None,
|
|
142
|
+ **options):
|
|
143
|
+ r = PyramRoute(self, rule, method, callback, name, **options)
|
|
144
|
+ self.routes.append(r)
|
|
145
|
+
|
|
146
|
+ def run(self, host, port, debug):
|
|
147
|
+ server = make_server('0.0.0.0', port, self.config.make_wsgi_app())
|
|
148
|
+ server.serve_forever()
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+app = Pyramidapp()
|
|
152
|
+
|
|
153
|
+controllers = Controllers()
|
|
154
|
+controllers.bind(app)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+# time.sleep(1)
|
|
158
|
+# s = hapic.generate_doc(app)
|
|
159
|
+# ss = json.loads(json.dumps(s))
|
|
160
|
+# for path in ss['paths']:
|
|
161
|
+# for method in ss['paths'][path]:
|
|
162
|
+# for response_code in ss['paths'][path][method]['responses']:
|
|
163
|
+# ss['paths'][path][method]['responses'][int(response_code)] = ss['paths'][path][method]['responses'][response_code]
|
|
164
|
+# del ss['paths'][path][method]['responses'][int(response_code)]
|
|
165
|
+# print(yaml.dump(ss, default_flow_style=False))
|
|
166
|
+# time.sleep(1)
|
|
167
|
+
|
|
168
|
+# hapic.set_context(hapic.ext.bottle.BottleContext())
|
|
169
|
+hapic.set_context(hapic.ext.pyramid.PyramidContext())
|
|
170
|
+#import pdb; pdb.set_trace()
|
|
171
|
+print(json.dumps(hapic.generate_doc(app)))
|
|
172
|
+app.run('localhost', 8080, True)
|
|
173
|
+#server = make_server('0.0.0.0', 8080, app.config.make_wsgi_app())
|
|
174
|
+# server.serve_forever()
|