en:python:webapi
                Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:python:webapi [2021/04/15 22:38] – [API] lonclegr | en:python:webapi [2021/04/25 22:58] (current) – [Architecture] lonclegr | ||
|---|---|---|---|
| Line 12: | Line 12: | ||
| Before going further: yes I know that a lot of solutions may solve this issue but the target is to make the POC in less than one hour. As I used to develop Web Restful API then it is my choice. | Before going further: yes I know that a lot of solutions may solve this issue but the target is to make the POC in less than one hour. As I used to develop Web Restful API then it is my choice. | ||
| + | ===== Architecture ===== | ||
| + | {{ : | ||
| + | |||
| + | {{ : | ||
| ===== Client side ===== | ===== Client side ===== | ||
| Line 60: | Line 64: | ||
| </ | </ | ||
| + | If you are running linux laptop you can adapt the script with curl. | ||
| + | |||
| + | <code bash> | ||
| + | curl -i -XPOST --silent http:// | ||
| + | HTTP/1.1 201 CREATED | ||
| + | Server: gunicorn | ||
| + | Date: Fri, 16 Apr 2021 03:07:27 GMT | ||
| + | Connection: close | ||
| + | Content-Type: | ||
| + | Content-Length: | ||
| + | |||
| + | {" | ||
| + | </ | ||
| ===== Server side ===== | ===== Server side ===== | ||
| + | ==== Run it ==== | ||
| + | |||
| + | |||
| + | <code bash> | ||
| + | python3 -m venv venv | ||
| + | source venv/ | ||
| + | pip install -r requirements.txt | ||
| + | gunicorn  | ||
| + | </ | ||
| ==== Logger ==== | ==== Logger ==== | ||
| - | <code python logger.py> | + | <file python logger.py> | 
| import logging | import logging | ||
| Line 84: | Line 110: | ||
| logger.addHandler(ch) | logger.addHandler(ch) | ||
| logger.addHandler(fh) | logger.addHandler(fh) | ||
| - | </code> | + | </file> | 
| ==== WSGI ==== | ==== WSGI ==== | ||
| - | <code python wsgi.py> | + | <file python wsgi.py> | 
| from api import app | from api import app | ||
| if __name__ == " | if __name__ == " | ||
| app.run() | app.run() | ||
| - | </code> | + | </file> | 
| ==== API ==== | ==== API ==== | ||
| - | <code python api.py > | + | <file python api.py> | 
| from flask import Flask, request | from flask import Flask, request | ||
| from flask_restful import Resource, Api | from flask_restful import Resource, Api | ||
| Line 104: | Line 130: | ||
| app = Flask(__name__) | app = Flask(__name__) | ||
| api = Api(app) | api = Api(app) | ||
| + | |||
| class HelloWorld(Resource): | class HelloWorld(Resource): | ||
| - | def post(self, action, user, computer): | + | def post(self, user, action, computer): | 
|         logger.info(f" |         logger.info(f" | ||
| return { | return { | ||
| Line 113: | Line 140: | ||
|             " |             " | ||
|             " |             " | ||
| - | }, 201 | + | } | 
| - | api.add_resource(HelloWorld, | + | |
| + | api.add_resource( | ||
| + |      | ||
| + |      | ||
| + | ) | ||
| if __name__ == ' | if __name__ == ' | ||
|     app.run(debug=True, |     app.run(debug=True, | ||
| - | </code> | + | </file> | 
| ==== Requirements ==== | ==== Requirements ==== | ||
| <file text requirements.txt> | <file text requirements.txt> | ||
| - | gunicorn | + | flake8 | 
| flask_restful | flask_restful | ||
| + | gunicorn | ||
| </ | </ | ||
en/python/webapi.1618540727.txt.gz · Last modified:  by lonclegr
                
                