Browse Source

adapt example_a tests for all contexts

Guénaël Muller 6 years ago
parent
commit
4bde534922
3 changed files with 6 additions and 145 deletions
  1. 0 139
      example_a2.py
  2. 3 3
      example_a_bottle.py
  3. 3 3
      example_a_flask.py

+ 0 - 139
example_a2.py View File

@@ -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()

example_a.py → example_a_bottle.py View File

@@ -90,7 +90,7 @@ class Controllers(object):
90 90
     # @hapic.error_schema(ErrorResponseSchema())
91 91
     @hapic.input_path(HelloPathSchema())
92 92
     @hapic.output_body(HelloResponseSchema())
93
-    def hello3(self, name: str):
93
+    def hello3(self, name: str, hapic_data: HapicData):
94 94
         return {
95 95
             'sentence': 'Hello !',
96 96
             'name': name,
@@ -125,7 +125,7 @@ controllers.bind(app)
125 125
 # print(yaml.dump(ss, default_flow_style=False))
126 126
 # time.sleep(1)
127 127
 
128
-hapic.set_context(hapic.ext.bottle.BottleContext())
129
-print(json.dumps(hapic.generate_doc(app)))
128
+hapic.set_context(hapic.ext.bottle.BottleContext(app))
129
+print(json.dumps(hapic.generate_doc()))
130 130
 
131 131
 app.run(host='localhost', port=8080, debug=True)

+ 3 - 3
example_a_flask.py View File

@@ -119,7 +119,7 @@ class Controllers(object):
119 119
     # @hapic.error_schema(ErrorResponseSchema())
120 120
     @hapic.input_path(HelloPathSchema())
121 121
     @hapic.output_body(HelloResponseSchema())
122
-    def hello3(self, name: str):
122
+    def hello3(self, name: str,hapic_data: HapicData ):
123 123
         return {
124 124
             'sentence': 'Hello !',
125 125
             'name': name,
@@ -150,7 +150,7 @@ controllers.bind(app)
150 150
 # print(yaml.dump(ss, default_flow_style=False))
151 151
 # time.sleep(1)
152 152
 
153
-hapic.set_context(hapic.ext.flask.FlaskContext())
154
-print(json.dumps(hapic.generate_doc(app)))
153
+hapic.set_context(hapic.ext.flask.FlaskContext(app))
154
+print(json.dumps(hapic.generate_doc()))
155 155
 #import pdb; pdb.set_trace()
156 156
 app.run(host='localhost', port=8080, debug=True)