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