1) We will set up environment for Swagger OpenAPI as shown in this link: https://survival8.blogspot.com/p/setting-up-conda-environment-for-project.html 2) List all the files that we have: C:\Users\ashish\Desktop\swagger_hello_world>dir /s/b C:\Users\ashish\Desktop\swagger_hello_world\api C:\Users\ashish\Desktop\swagger_hello_world\config C:\Users\ashish\Desktop\swagger_hello_world\hw_app.py C:\Users\ashish\Desktop\swagger_hello_world\my_client.py C:\Users\ashish\Desktop\swagger_hello_world\swagger C:\Users\ashish\Desktop\swagger_hello_world\api\api_endpoints.py C:\Users\ashish\Desktop\swagger_hello_world\api\__init__.py C:\Users\ashish\Desktop\swagger_hello_world\config\appconfig.properties C:\Users\ashish\Desktop\swagger_hello_world\config\config.py C:\Users\ashish\Desktop\swagger_hello_world\config\__init__.py C:\Users\ashish\Desktop\swagger_hello_world\swagger\sw_conf.yaml 3) Get the contents of all the files that we have using "C:\Users\ashish\Desktop\swagger_hello_world>(for /r %i in (*) do type %i) > allContent.log": C:\Users\ashish\Desktop\swagger_hello_world>type C:\Users\ashish\Desktop\swagger_hello_world\hw_app.py import connexion from config.config import Config from flask_cors import CORS import logging logger = logging.getLogger(__name__) app = connexion.FlaskApp(__name__) print("ENV: ",app.app.config['ENV'].upper()) app_conf = Config().getAppConf(app.app.config['ENV'].upper()) CORS(app.app, methods=['POST', 'PUT', 'DELETE'], allow_headers=['Content-Type']) @app.route('/') def home(): return "Hello World, Generic!", 200 if __name__ == '__main__': app.add_api('swagger/sw_conf.yaml', arguments={'title': 'Hello World Service'}) app.run(host=app_conf["IP_ADDRESS"], port=app_conf["PORT"], debug=app_conf["DEBUG"]) C:\Users\ashish\Desktop\swagger_hello_world>type C:\Users\ashish\Desktop\swagger_hello_world\my_client.py import requests headers = {'content-type': 'application/json'} URL = "http://127.0.0.1:8083/v1.0/post_test" r = requests.post(url = URL, data = {}, json = { "msg": "Post Holla" }, headers = headers) print("Response text: " + r.text) URL = "http://127.0.0.1:8083/v1.0/get_test" r = requests.get(url = URL, data = {}, json = { "msg": "Get Holla" }, headers = headers) print("Response text: " + r.text) C:\Users\ashish\Desktop\swagger_hello_world>type C:\Users\ashish\Desktop\swagger_hello_world\api\api_endpoints.py import connexion import logging from flask import Response import json class MyEndpoints(object): def __init__(self): self.logger=logging.getLogger("MyEndpoints") self.logger.debug('From __init__()') def get_hello_world(self): req_data = connexion.request.get_json() data = req_data.get('msg') return Response(json.dumps("msg: " + str(data)), status=201, mimetype='application/json') def post_hello_world(self): req_data = connexion.request.get_json() data = req_data.get('msg') return Response(json.dumps("msg: " + str(data)), status=201, mimetype='application/json') class_instance = MyEndpoints() C:\Users\ashish\Desktop\swagger_hello_world>type C:\Users\ashish\Desktop\swagger_hello_world\api\__init__.py C:\Users\ashish\Desktop\swagger_hello_world>type C:\Users\ashish\Desktop\swagger_hello_world\config\appconfig.properties [DEVELOPMENT] DEBUG=True IP_ADDRESS = 0.0.0.0 PORT = 8081 [TEST] DEBUG = True IP_ADDRESS = 0.0.0.0 PORT = 8082 [PRODUCTION] DEBUG = IP_ADDRESS = 0.0.0.0 PORT = 8083 C:\Users\ashish\Desktop\swagger_hello_world>type C:\Users\ashish\Desktop\swagger_hello_world\config\config.py import configparser import os import logging from logging.config import fileConfig class Config(object): def __init__(self): try: self.app_config = configparser.RawConfigParser() self.app_config.read([os.path.expanduser('config//appconfig.properties')]) self.logger = logging.getLogger('Config') fileConfig('config//loggingconfig.ini') except Exception as e: print(str(e)) def getAppConf(self,env): self.logger.info("getAppConf") return self.app_config[env] def getLogger(self,name): logger = logging.getLogger(name) return logger C:\Users\ashish\Desktop\swagger_hello_world>type C:\Users\ashish\Desktop\swagger_hello_world\config\__init__.py print('init.py') C:\Users\ashish\Desktop\swagger_hello_world>type C:\Users\ashish\Desktop\swagger_hello_world\swagger\sw_conf.yaml swagger: '2.0' info: title: {{ title }} description: Hello world microservice. version: "0.1" consumes: - application/json produces: - application/json basePath: /v1.0 tags: - name: "na" description: "NA" schemes: - "http" paths: /get_test: get: operationId: api.api_endpoints.class_instance.get_hello_world summary: Get Hello World parameters: - in: body name: my_data description: NA. schema: type: object items: $ref: '#/definitions/my_data' responses: 200: description: successful return schema: type: string /post_test: post: operationId: api.api_endpoints.class_instance.post_hello_world summary: Post Hello World parameters: - in: body name: my_data description: NA. schema: type: object items: $ref: '#/definitions/my_data' responses: 200: description: successful return schema: type: string definitions: my_data: type: object properties: msg: type: string 4) Execution commands: 4.1) (swagger) C:\Users\ashish\Desktop\swagger_hello_world>python hw_app.py 4.2) (base) C:\Users\ashish\Desktop\swagger_hello_world>python my_client.py 5) Execution logs: 5.1) (swagger) C:\Users\ashish\Desktop\swagger_hello_world>python hw_app.py init.py ENV: PRODUCTION * Serving Flask app "hw_app" (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off * Running on http://0.0.0.0:8083/ (Press CTRL+C to quit) 127.0.0.1 - - [05/05/2018 14:44:27] "POST /v1.0/post_test HTTP/1.1" 201 - 127.0.0.1 - - [05/05/2018 14:44:27] "GET /v1.0/get_test HTTP/1.1" 201 - 5.2) (base) C:\Users\ashish\Desktop\swagger_hello_world>python my_client.py Response text: "msg: Post Holla" Response text: "msg: Get Holla" NOTE: application is directly launching in the "production" mode. 6) Link to Google Drive for code: https://drive.google.com/open?id=1UfcfAOQPIHHhyn42tougG8vdWrQ4FqOP
Demo of Swagger OpenAPI Specs using Python
Subscribe to:
Posts (Atom)
No comments:
Post a Comment