SHARE
TWEET

Untitled

a guest Jul 21st, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from flask import Flask, request
  2. from flask_restplus import Api, Resource, fields
  3. from flask_sqlalchemy import SQLAlchemy
  4. from marshmallow import Schema, fields as FM
  5. from sqlalchemy import (Text)  # you can add another table column type if you need
  6. from flask_migrate import Migrate
  7.  
  8. app = Flask(__name__)
  9. # we can access swagger documentation at localhost:5000/docs
  10. api = Api(app, doc='/docs')
  11. # our base API path will be {BASE_URL}/my_api
  12. name_space = api.namespace('my_api', description='API Project')
  13.  
  14. # configure SQLAlchemy
  15. app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
  16. # my MySQL user is root with password root and my database is my_project
  17. app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:root@localhost/my_project'
  18. db = SQLAlchemy(app)
  19.  
  20.  
  21. # model to table Log
  22. class Log(db.Model):
  23.     id = db.Column(db.Integer, primary_key=True)
  24.     detail = db.Column(Text)
  25.  
  26.  
  27. # will be used to serialize model Log into JSON for API result
  28. class LogSchema(Schema):
  29.     id = FM.Int()
  30.     detail = FM.Str()
  31.  
  32.  
  33. # this will be used to add parameter in swagger for inserting/updating record
  34. logSwagger = api.model('Log', {
  35.     'detail': fields.String(required=True, description='Log detail content')
  36. })
  37.  
  38. schema = LogSchema()
  39.  
  40.  
  41. @name_space.route("/")
  42. class LogList(Resource):
  43.     #you can add additional Swagger response information here
  44.     @api.doc(
  45.         responses=
  46.         {
  47.             200: 'OK',
  48.             400: 'Invalid Argument',
  49.             500: 'Mapping Key Error',
  50.         })
  51.     def get(self):
  52.         logs = Log.query.all()
  53.         result = []
  54.         schema = LogSchema()
  55.         for log in logs:
  56.             result.append(schema.dump(log))
  57.         return {
  58.             "data": result
  59.         }
  60.  
  61.     @name_space.expect(logSwagger)
  62.     def post(self):
  63.         payload = request.get_json()
  64.         log = Log()
  65.         log.detail = payload['detail']
  66.         db.session.add(log)
  67.         db.session.commit()
  68.  
  69.         return {
  70.             "data": schema.dump(log)
  71.         }
  72.  
  73.  
  74. @name_space.route("/<int:id>")
  75. class LogDetail(Resource):
  76.     def get(self, id):  # will be used to see one particular log detail
  77.         return {
  78.             "status": "See detail for log with id " + str(id)
  79.         }
  80.  
  81.     def put(self, id):  # will be used to update particular log
  82.         return {
  83.             "status": "Updated log with id " + str(id)
  84.         }
  85.  
  86.     def delete(self, id):  # will be used to delete particular log
  87.         return {
  88.             "status": "Deleted log with id " + str(id)
  89.         }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top