Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  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. @api.doc(
  77. responses=
  78. {
  79. 200: 'OK',
  80. 400: 'Invalid Argument',
  81. 500: 'Mapping Key Error'
  82. },
  83. params=
  84. {
  85. 'id': 'Id log to get the detail'
  86. })
  87. def get(self, id):
  88. log = Log.query.get(id)
  89. return {
  90. "data": schema.dump(log)
  91. }
  92.  
  93. @api.doc(
  94. responses=
  95. {
  96. 200: 'OK',
  97. 400: 'Invalid Argument',
  98. 500: 'Mapping Key Error'
  99. },
  100. params=
  101. {
  102. 'id': 'Id log to be updated'
  103. })
  104. @name_space.expect(logSwagger)
  105. def put(self, id):
  106. payload = request.get_json()
  107. log = Log.query.get(id)
  108. log.detail = payload['detail']
  109. db.session.add(log)
  110. db.session.commit()
  111. return {
  112. "data": schema.dump(log)
  113. }
  114.  
  115. @api.doc(
  116. responses=
  117. {
  118. 200: 'OK',
  119. 400: 'Invalid Argument',
  120. 500: 'Mapping Key Error'
  121. },
  122. params=
  123. {
  124. 'id': 'Id log to be removed'
  125. })
  126. def delete(self, id):
  127. log = Log.query.get(id)
  128. db.session.delete(log)
  129. db.session.commit()
  130. return {
  131. "status": "OK"
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement