37创客科创中心

 找回密码
 立即注册
查看: 1385|回复: 1

apiflask

[复制链接]

194

主题

324

帖子

2399

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2399
发表于 2024-5-17 03:20:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. from apiflask import APIFlask, Schema, abort
  2. from apiflask.fields import Integer, String
  3. from apiflask.validators import Length, OneOf

  4. app = APIFlask(__name__)

  5. pets = [
  6.     {'id': 0, 'name': 'Kitty', 'category': 'cat'},
  7.     {'id': 1, 'name': 'Coco', 'category': 'dog'},
  8.     {'id': 2, 'name': 'Flash', 'category': 'cat'}
  9. ]


  10. class PetIn(Schema):
  11.     name = String(required=True, validate=Length(0, 10))
  12.     category = String(required=True, validate=OneOf(['dog', 'cat']))


  13. class PetOut(Schema):
  14.     id = Integer()
  15.     name = String()
  16.     category = String()


  17. @app.get('/')
  18. def say_hello():
  19.     return {'message': 'Hello!'}


  20. @app.get('/pets/<int:pet_id>')
  21. @app.output(PetOut)
  22. def get_pet(pet_id):
  23.     if pet_id > len(pets) - 1 or pets[pet_id].get('deleted'):
  24.         abort(404)
  25.     return pets[pet_id]


  26. @app.get('/pets')
  27. @app.output(PetOut(many=True))
  28. def get_pets():
  29.     return pets


  30. @app.post('/pets')
  31. @app.input(PetIn, location='json')
  32. @app.output(PetOut, status_code=201)
  33. def create_pet(json_data):
  34.     pet_id = len(pets)
  35.     json_data['id'] = pet_id
  36.     pets.append(json_data)
  37.     return pets[pet_id]


  38. @app.patch('/pets/<int:pet_id>')
  39. @app.input(PetIn(partial=True), location='json')
  40. @app.output(PetOut)
  41. def update_pet(pet_id, json_data):
  42.     if pet_id > len(pets) - 1:
  43.         abort(404)
  44.     for attr, value in json_data.items():
  45.         pets[pet_id][attr] = value
  46.     return pets[pet_id]


  47. @app.delete('/pets/<int:pet_id>')
  48. @app.output({}, status_code=204)
  49. def delete_pet(pet_id):
  50.     if pet_id > len(pets) - 1:
  51.         abort(404)
  52.     pets[pet_id]['deleted'] = True
  53.     pets[pet_id]['name'] = 'Ghost'
  54.     return ''
复制代码



回复

使用道具 举报

194

主题

324

帖子

2399

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2399
 楼主| 发表于 2024-5-17 03:23:56 | 显示全部楼层
数据库结合



  1. from apiflask import APIFlask, Schema
  2. from apiflask.fields import Integer, String
  3. from apiflask.validators import Length, OneOf
  4. from flask_sqlalchemy import SQLAlchemy

  5. app = APIFlask(__name__)
  6. app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
  7. app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

  8. db = SQLAlchemy(app)


  9. class PetModel(db.Model):
  10.     id = db.Column(db.Integer, primary_key=True)
  11.     name = db.Column(db.String(10))
  12.     category = db.Column(db.String(10))


  13. def init_database():
  14.     db.create_all()

  15.     pets = [
  16.         {'name': 'Kitty', 'category': 'cat'},
  17.         {'name': 'Coco', 'category': 'dog'},
  18.         {'name': 'Flash', 'category': 'cat'}
  19.     ]
  20.     for pet_data in pets:
  21.         pet = PetModel(**pet_data)
  22.         db.session.add(pet)
  23.     db.session.commit()


  24. class PetIn(Schema):
  25.     name = String(required=True, validate=Length(0, 10))
  26.     category = String(required=True, validate=OneOf(['dog', 'cat']))


  27. class PetOut(Schema):
  28.     id = Integer()
  29.     name = String()
  30.     category = String()


  31. @app.get('/')
  32. def say_hello():
  33.     return {'message': 'Hello!'}


  34. @app.get('/pets/<int:pet_id>')
  35. @app.output(PetOut)
  36. def get_pet(pet_id):
  37.     return db.get_or_404(PetModel, pet_id)


  38. @app.get('/pets')
  39. @app.output(PetOut(many=True))
  40. def get_pets():
  41.     return PetModel.query.all()


  42. @app.post('/pets')
  43. @app.input(PetIn, location='json')
  44. @app.output(PetOut, status_code=201)
  45. def create_pet(json_data):
  46.     pet = PetModel(**json_data)
  47.     db.session.add(pet)
  48.     db.session.commit()
  49.     return pet


  50. @app.patch('/pets/<int:pet_id>')
  51. @app.input(PetIn(partial=True), location='json')
  52. @app.output(PetOut)
  53. def update_pet(pet_id, json_data):
  54.     pet = db.get_or_404(PetModel, pet_id)
  55.     for attr, value in json_data.items():
  56.         setattr(pet, attr, value)
  57.     db.session.commit()
  58.     return pet


  59. @app.delete('/pets/<int:pet_id>')
  60. @app.output({}, status_code=204)
  61. def delete_pet(pet_id):
  62.     pet = db.get_or_404(PetModel, pet_id)
  63.     db.session.delete(pet)
  64.     db.session.commit()
  65.     return ''


  66. with app.app_context():
  67.     init_database()
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|37创客科创中心

GMT+8, 2025-12-10 05:58 , Processed in 0.146876 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表