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:28] – [Logger] 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 24: | Line 28: | ||
| # before the main command, let's call the API | # before the main command, let's call the API | ||
| - | Invoke-WebRequest (" | + | Invoke-WebRequest (" | 
| # main command | # main command | ||
| Line 30: | Line 34: | ||
| # after the main command, let's call the API | # after the main command, let's call the API | ||
| - | Invoke-WebRequest (" | + | Invoke-WebRequest (" | 
| </ | </ | ||
| Line 39: | Line 43: | ||
| StatusCode  | StatusCode  | ||
| StatusDescription : Created | StatusDescription : Created | ||
| - | Content  | + | Content  | 
|                     " |                     " | ||
| Line 49: | Line 53: | ||
|                     Server: gunicorn/ |                     Server: gunicorn/ | ||
| - |                     {" | + |                     {" | 
| Forms : {} | Forms : {} | ||
| Headers  | Headers  | ||
| 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 ==== | ||
| + | <file python wsgi.py> | ||
| + | from api import app | ||
| + | |||
| + | if __name__ == " | ||
| + | app.run() | ||
| + | </ | ||
| ==== API ==== | ==== API ==== | ||
| + | <file python api.py> | ||
| + | from flask import Flask, request | ||
| + | from flask_restful import Resource, Api | ||
| + | from logger import logger | ||
| + | app = Flask(__name__) | ||
| + | api = Api(app) | ||
| + | |||
| + | class HelloWorld(Resource): | ||
| + | def post(self, user, action, computer): | ||
| + |         logger.info(f" | ||
| + | return { | ||
| + |             " | ||
| + |             " | ||
| + |             " | ||
| + |             " | ||
| + | } | ||
| + | |||
| + | |||
| + | api.add_resource( | ||
| + | HelloWorld, | ||
| + |     '/ | ||
| + | ) | ||
| + | |||
| + | if __name__ == ' | ||
| + |     app.run(debug=True, | ||
| + | </ | ||
| + | |||
| + | ==== Requirements ==== | ||
| + | |||
| + | <file text requirements.txt> | ||
| + | flake8 | ||
| + | flask_restful | ||
| + | gunicorn | ||
| + | </ | ||
en/python/webapi.1618540084.txt.gz · Last modified:  by lonclegr
                
                