Browse Source

adapt api example to allow import

Guénaël Muller 6 years ago
parent
commit
5d77b8a077
3 changed files with 37 additions and 36 deletions
  1. 12 12
      example/fake_api/bottle_api.py
  2. 12 12
      example/fake_api/flask_api.py
  3. 13 12
      example/fake_api/pyramid_api.py

+ 12 - 12
example/fake_api/bottle_api.py View File

@@ -107,15 +107,15 @@ class Controllers(object):
107 107
         app.route('/users/', callback=self.add_user,  method='POST')
108 108
         app.route('/users/<id>', callback=self.del_user, method='DELETE')
109 109
 
110
-
111
-app = bottle.Bottle()
112
-controllers = Controllers()
113
-controllers.bind(app)
114
-hapic.set_context(hapic.ext.bottle.BottleContext(app))
115
-time.sleep(1)
116
-s = json.dumps(hapic.generate_doc())
117
-time.sleep(1)
118
-# print swagger doc
119
-print(s)
120
-# Run app
121
-app.run(host='localhost', port=8081, debug=True)
110
+if __name__ == "__main__":
111
+    app = bottle.Bottle()
112
+    controllers = Controllers()
113
+    controllers.bind(app)
114
+    hapic.set_context(hapic.ext.bottle.BottleContext(app))
115
+    time.sleep(1)
116
+    s = json.dumps(hapic.generate_doc())
117
+    time.sleep(1)
118
+    # print swagger doc
119
+    print(s)
120
+    # Run app
121
+    app.run(host='localhost', port=8081, debug=True)

+ 12 - 12
example/fake_api/flask_api.py View File

@@ -114,15 +114,15 @@ class Controllers(object):
114 114
                          view_func=self.del_user,
115 115
                          methods=['DELETE'])
116 116
 
117
-
118
-app = flask.Flask(__name__)
119
-controllers = Controllers()
120
-controllers.bind(app)
121
-hapic.set_context(hapic.ext.flask.FlaskContext(app))
122
-time.sleep(1)
123
-s = json.dumps(hapic.generate_doc())
124
-time.sleep(1)
125
-# print swagger doc
126
-print(s)
127
-# Run app
128
-app.run(host='localhost', port=8082, debug=True)
117
+if __name__ == "__main__":
118
+    app = flask.Flask(__name__)
119
+    controllers = Controllers()
120
+    controllers.bind(app)
121
+    hapic.set_context(hapic.ext.flask.FlaskContext(app))
122
+    time.sleep(1)
123
+    s = json.dumps(hapic.generate_doc())
124
+    time.sleep(1)
125
+    # print swagger doc
126
+    print(s)
127
+    # Run app
128
+    app.run(host='localhost', port=8082, debug=True)

+ 13 - 12
example/fake_api/pyramid_api.py View File

@@ -117,15 +117,16 @@ class Controllers(object):
117 117
         configurator.add_route('del_user', '/users/{id}', request_method='DELETE')  # nopep8
118 118
         configurator.add_view(self.del_user, route_name='del_user', renderer='json')  # nopep8
119 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', 8083, configurator.make_wsgi_app())
131
-server.serve_forever()
120
+if __name__ == "__main__":
121
+    configurator = Configurator(autocommit=True)
122
+    controllers = Controllers()
123
+    controllers.bind(configurator)
124
+    hapic.set_context(hapic.ext.pyramid.PyramidContext(configurator))
125
+    time.sleep(1)
126
+    s = json.dumps(hapic.generate_doc())
127
+    time.sleep(1)
128
+    # print swagger doc
129
+    print(s)
130
+    # Run app
131
+    server = make_server('0.0.0.0', 8083, configurator.make_wsgi_app())
132
+    server.serve_forever()