from flask import request from flask_restful import Resource
from models.book import Book from resources import api from services.book_service import BookService
classBookResource(Resource): defget(self, book_id: int): book = BookService().get_book_by_id(book_id) if book: return book.serialize(), 200 else: return {'error': f'Book(book_id:{book_id}) not found'}, 404
classBookListResource(Resource): defget(self): book_list = BookService().get_all_books() return [book.serialize() for book in book_list]
defpost(self): request_json = request.json if request_json: title = request_json.get('title', None) author = request_json.get('author', None) publish_time = datetime.fromisoformat(request_json.get('publish_time', None)) book = Book(title=title, author=author, publish_time=publish_time)
if title and author and publish_time: book = BookService().create_book(book) return book.serialize(), 200 else: return {'error': 'Invalid request, please provide book info as json'}, 400
from flask import request from flask_restful import Resource
from models.book import Book from resources import api from services.book_service import BookService
classBookResource(Resource): defget(self, book_id: int): book = BookService().get_book_by_id(book_id) if book: return book.serialize(), 200 else: return {'error': f'Book(book_id:{book_id}) not found'}, 404
classBookListResource(Resource): defget(self): book_list = BookService().get_all_books() return [book.serialize() for book in book_list]
defpost(self): try: request_json = request.json if request_json: title = request_json.get('title', None) author = request_json.get('author', None) publish_time = datetime.fromisoformat(request_json.get('publish_time', None)) book = Book(title=title, author=author, publish_time=publish_time)
if title and author and publish_time: book = BookService().create_book(book) return book.serialize(), 200 else: return {'error': 'Invalid request, please provide book info as json'}, 400 except Exception as e: return {'error': f'{e}'}, 500
from flask import request from flask_restful import Resource
from models.book import Book from resources import api from services.book_service import BookService
classBookResource(Resource): defget(self, book_id: int): book = BookService().get_book_by_id(book_id) if book: return book.serialize(), 200 else: return {'error': f'Book(book_id:{book_id}) not found'}, 404
defput(self, book_id: int): try: request_json = request.json if request_json: title = request_json.get('title', None) author = request_json.get('author', None) publish_time_str = request_json.get('publish_time', None) publish_time = datetime.fromisoformat(publish_time_str) if publish_time_str elseNone book = Book(id=book_id, title=title, author=author, publish_time=publish_time)
book = BookService().update_book(book) return book.serialize(), 200 else: return {'error': 'Invalid request, please provide book info as json'}, 400 except Exception as e: return {'error': f'{e}'}, 500
classBookListResource(Resource): defget(self): book_list = BookService().get_all_books() return [book.serialize() for book in book_list]
defpost(self): try: request_json = request.json if request_json: title = request_json.get('title', None) author = request_json.get('author', None) publish_time_str = request_json.get('publish_time', None) publish_time = datetime.fromisoformat(publish_time_str) if publish_time_str elseNone
if title and author and publish_time: book = Book(title=title, author=author, publish_time=publish_time) book = BookService().create_book(book) return book.serialize(), 200 else: return {'error': 'Invalid request, please provide complete book info'}, 400 else: return {'error': 'Invalid request, please provide book info as json'}, 400 except Exception as e: return {'error': f'{e}'}, 500
if username and password: user = UserService().login(username, password) if user: return user.serialize(), 200 else: return {'error': 'Invalid username or password'}, 401 else: return {'error': 'Invalid request, please provide complete username and password'}, 400 else: return {'error': 'Invalid request, please provide username and password info as json'}, 400 except Exception as e: return {'error': f'{e}'}, 500
api.add_resource(LoginResource, '/login')
同时还有 user_resource 需要添加一下
1 2 3 4 5 6 7 8 9 10 11 12
# -*- coding:utf-8 -*- # Author: Zachary from flask import Flask from flask_restful import Api from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__) api = Api(app) app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqldb://root:980226@localhost:3306/restful_db' db = SQLAlchemy(app)
from resources import student_resource, book_resource, user_resource
import jwt from flask import request from flask_restful import Resource
from common.constants import LOGIN_SECRET from models.book import Book from resources import api from services.book_service import BookService
classBookResource(Resource): defget(self, book_id: int): book = BookService().get_book_by_id(book_id) if book: return book.serialize(), 200 else: return {'error': f'Book(book_id:{book_id}) not found'}, 404
defput(self, book_id: int): try: request_json = request.json if request_json: title = request_json.get('title', None) author = request_json.get('author', None) publish_time_str = request_json.get('publish_time', None) publish_time = datetime.fromisoformat(publish_time_str) if publish_time_str elseNone book = Book(id=book_id, title=title, author=author, publish_time=publish_time)
book = BookService().update_book(book) return book.serialize(), 200 else: return {'error': 'Invalid request, please provide book info as json'}, 400 except Exception as e: return {'error': f'{e}'}, 500
classBookListResource(Resource): defget(self): book_list = BookService().get_all_books() return [book.serialize() for book in book_list]
try: request_json = request.json if request_json: title = request_json.get('title', None) author = request_json.get('author', None) publish_time_str = request_json.get('publish_time', None) publish_time = datetime.fromisoformat(publish_time_str) if publish_time_str elseNone
if title and author and publish_time: book = Book(title=title, author=author, publish_time=publish_time) book = BookService().create_book(book) return book.serialize(), 200 else: return {'error': 'Invalid request, please provide complete book info'}, 400 else: return {'error': 'Invalid request, please provide book info as json'}, 400 except Exception as e: return {'error': f'{e}'}, 500
import jwt from flask import request from flask_restful import Resource
from common.api_tools import token_required from common.constants import LOGIN_SECRET from models.book import Book from resources import api from services.book_service import BookService
classBookResource(Resource): defget(self, book_id: int): book = BookService().get_book_by_id(book_id) if book: return book.serialize(), 200 else: return {'error': f'Book(book_id:{book_id}) not found'}, 404
@token_required() defput(self, book_id: int): try: request_json = request.json if request_json: title = request_json.get('title', None) author = request_json.get('author', None) publish_time_str = request_json.get('publish_time', None) publish_time = datetime.fromisoformat(publish_time_str) if publish_time_str elseNone book = Book(id=book_id, title=title, author=author, publish_time=publish_time)
book = BookService().update_book(book) return book.serialize(), 200 else: return {'error': 'Invalid request, please provide book info as json'}, 400 except Exception as e: return {'error': f'{e}'}, 500
classBookListResource(Resource): defget(self): book_list = BookService().get_all_books() return [book.serialize() for book in book_list]
@token_required() defpost(self): try: request_json = request.json if request_json: title = request_json.get('title', None) author = request_json.get('author', None) publish_time_str = request_json.get('publish_time', None) publish_time = datetime.fromisoformat(publish_time_str) if publish_time_str elseNone
if title and author and publish_time: book = Book(title=title, author=author, publish_time=publish_time) book = BookService().create_book(book) return book.serialize(), 200 else: return {'error': 'Invalid request, please provide complete book info'}, 400 else: return {'error': 'Invalid request, please provide book info as json'}, 400 except Exception as e: return {'error': f'{e}'}, 500
# -*- coding:utf-8 -*- # Author: Zachary from apispec import APISpec from apispec.ext.marshmallow import MarshmallowPlugin from flask import Flask from flask_apispec import FlaskApiSpec from flask_restful import Api from flask_restful_swagger import swagger from flask_sqlalchemy import SQLAlchemy
import jwt from flask import request from flask_apispec import MethodResource, doc from flask_restful import Resource
from common.api_tools import token_required from common.constants import LOGIN_SECRET from models.book import Book from resources import api, docs from services.book_service import BookService
classBookResource(MethodResource, Resource): @doc(description='Get book by id', tags=['Book Requests']) defget(self, book_id: int): book = BookService().get_book_by_id(book_id) if book: return book.serialize(), 200 else: return {'error': f'Book(book_id:{book_id}) not found'}, 404
@token_required() defput(self, book_id: int): try: request_json = request.json if request_json: title = request_json.get('title', None) author = request_json.get('author', None) publish_time_str = request_json.get('publish_time', None) publish_time = datetime.fromisoformat(publish_time_str) if publish_time_str elseNone book = Book(id=book_id, title=title, author=author, publish_time=publish_time)
book = BookService().update_book(book) return book.serialize(), 200 else: return {'error': 'Invalid request, please provide book info as json'}, 400 except Exception as e: return {'error': f'{e}'}, 500
classBookListResource(Resource): defget(self): book_list = BookService().get_all_books() return [book.serialize() for book in book_list]
@token_required() defpost(self): try: request_json = request.json if request_json: title = request_json.get('title', None) author = request_json.get('author', None) publish_time_str = request_json.get('publish_time', None) publish_time = datetime.fromisoformat(publish_time_str) if publish_time_str elseNone
if title and author and publish_time: book = Book(title=title, author=author, publish_time=publish_time) book = BookService().create_book(book) return book.serialize(), 200 else: return {'error': 'Invalid request, please provide complete book info'}, 400 else: return {'error': 'Invalid request, please provide book info as json'}, 400 except Exception as e: return {'error': f'{e}'}, 500
import jwt from flask import request from flask_apispec import MethodResource, doc from flask_restful import Resource
from common.api_tools import token_required from common.constants import LOGIN_SECRET from models.book import Book from resources import api, docs from services.book_service import BookService
classBookResource(MethodResource, Resource): @doc(description='Get a book by book id', tags=['Book Requests']) defget(self, book_id: int): book = BookService().get_book_by_id(book_id) if book: return book.serialize(), 200 else: return {'error': f'Book(book_id:{book_id}) not found'}, 404
@doc(description='Update a book by book id', tags=['Book Requests']) @token_required() defput(self, book_id: int): try: request_json = request.json if request_json: title = request_json.get('title', None) author = request_json.get('author', None) publish_time_str = request_json.get('publish_time', None) publish_time = datetime.fromisoformat(publish_time_str) if publish_time_str elseNone book = Book(id=book_id, title=title, author=author, publish_time=publish_time)
book = BookService().update_book(book) return book.serialize(), 200 else: return {'error': 'Invalid request, please provide book info as json'}, 400 except Exception as e: return {'error': f'{e}'}, 500
classBookListResource(Resource): defget(self): book_list = BookService().get_all_books() return [book.serialize() for book in book_list]
@token_required() defpost(self): try: request_json = request.json if request_json: title = request_json.get('title', None) author = request_json.get('author', None) publish_time_str = request_json.get('publish_time', None) publish_time = datetime.fromisoformat(publish_time_str) if publish_time_str elseNone
if title and author and publish_time: book = Book(title=title, author=author, publish_time=publish_time) book = BookService().create_book(book) return book.serialize(), 200 else: return {'error': 'Invalid request, please provide complete book info'}, 400 else: return {'error': 'Invalid request, please provide book info as json'}, 400 except Exception as e: return {'error': f'{e}'}, 500
import jwt from flask import request, Response from flask_apispec import MethodResource, doc from flask_restful import Resource
from common.api_tools import token_required from common.constants import LOGIN_SECRET from models.book import Book from resources import api, docs, app from services.book_service import BookService
classBookResource(MethodResource, Resource): @doc(description='Get a book by book id', tags=['Book Requests']) defget(self, book_id: int): book = BookService().get_book_by_id(book_id) if book: return book.serialize(), 200 else: return {'error': f'Book(book_id:{book_id}) not found'}, 404
@doc(description='Update a book by book id', tags=['Book Requests']) @token_required() defput(self, book_id: int): try: request_json = request.json if request_json: title = request_json.get('title', None) author = request_json.get('author', None) publish_time_str = request_json.get('publish_time', None) publish_time = datetime.fromisoformat(publish_time_str) if publish_time_str elseNone book = Book(id=book_id, title=title, author=author, publish_time=publish_time)
book = BookService().update_book(book) return book.serialize(), 200 else: return {'error': 'Invalid request, please provide book info as json'}, 400 except Exception as e: return {'error': f'{e}'}, 500
classBookListResource(Resource): defget(self): book_list = BookService().get_all_books() return [book.serialize() for book in book_list]
@token_required() defpost(self): try: request_json = request.json if request_json: title = request_json.get('title', None) author = request_json.get('author', None) publish_time_str = request_json.get('publish_time', None) publish_time = datetime.fromisoformat(publish_time_str) if publish_time_str elseNone
if title and author and publish_time: book = Book(title=title, author=author, publish_time=publish_time) book = BookService().create_book(book) return book.serialize(), 200 else: return {'error': 'Invalid request, please provide complete book info'}, 400 else: return {'error': 'Invalid request, please provide book info as json'}, 400 except Exception as e: return {'error': f'{e}'}, 500
import jwt from flask import request, Response from flask_apispec import MethodResource, doc, use_kwargs from flask_restful import Resource from marshmallow import Schema, fields
from common.api_tools import token_required from common.constants import LOGIN_SECRET from models.book import Book from resources import api, docs, app from services.book_service import BookService
classBookRequestSchema(Schema): title = fields.String(required=True) author = fields.String(requird=True) publish_time = fields.DateTime(required=True)
classBookResource(MethodResource, Resource): @doc(description='Get a book by book id', tags=['Book Requests']) defget(self, book_id: int): book = BookService().get_book_by_id(book_id) if book: return book.serialize(), 200 else: return {'error': f'Book(book_id:{book_id}) not found'}, 404
@doc(description='Update a book by book id', tags=['Book Requests']) @use_kwargs(BookRequestSchema, location='json') @token_required() defput(self, book_id: int, **kwargs): try: title = kwargs.get('title', None) author = kwargs.get('author', None) publish_time = kwargs.get('publish_time', None) book = Book(id=book_id, title=title, author=author, publish_time=publish_time)
book = BookService().update_book(book) return book.serialize(), 200 except Exception as e: return {'error': f'{e}'}, 500
classBookListResource(Resource): defget(self): book_list = BookService().get_all_books() return [book.serialize() for book in book_list]
@token_required() defpost(self): try: request_json = request.json if request_json: title = request_json.get('title', None) author = request_json.get('author', None) publish_time_str = request_json.get('publish_time', None) publish_time = datetime.fromisoformat(publish_time_str) if publish_time_str elseNone
if title and author and publish_time: book = Book(title=title, author=author, publish_time=publish_time) book = BookService().create_book(book) return book.serialize(), 200 else: return {'error': 'Invalid request, please provide complete book info'}, 400 else: return {'error': 'Invalid request, please provide book info as json'}, 400 except Exception as e: return {'error': f'{e}'}, 500
import jwt from flask import request, Response from flask_apispec import MethodResource, doc, use_kwargs from flask_restful import Resource from marshmallow import Schema, fields
from common.api_tools import token_required from common.constants import LOGIN_SECRET from models.book import Book from resources import api, docs, app from services.book_service import BookService
classBookRequestSchema(Schema): title = fields.String(required=True) author = fields.String(requird=True) publish_time = fields.DateTime(required=True)
classBookResource(MethodResource, Resource): @doc(description='Get a book by book id', tags=['Book Requests']) defget(self, book_id: int): book = BookService().get_book_by_id(book_id) if book: return book.serialize(), 200 else: return {'error': f'Book(book_id:{book_id}) not found'}, 404
@doc(description='Update a book by book id', tags=['Book Requests']) @use_kwargs(TokenSchema, location='headers') @use_kwargs(BookRequestSchema, location='json') @token_required() defput(self, book_id: int, **kwargs): try: title = kwargs.get('title', None) author = kwargs.get('author', None) publish_time = kwargs.get('publish_time', None) book = Book(id=book_id, title=title, author=author, publish_time=publish_time)
book = BookService().update_book(book) return book.serialize(), 200 except Exception as e: return {'error': f'{e}'}, 500
classBookListResource(Resource): defget(self): book_list = BookService().get_all_books() return [book.serialize() for book in book_list]
@token_required() defpost(self): try: request_json = request.json if request_json: title = request_json.get('title', None) author = request_json.get('author', None) publish_time_str = request_json.get('publish_time', None) publish_time = datetime.fromisoformat(publish_time_str) if publish_time_str elseNone
if title and author and publish_time: book = Book(title=title, author=author, publish_time=publish_time) book = BookService().create_book(book) return book.serialize(), 200 else: return {'error': 'Invalid request, please provide complete book info'}, 400 else: return {'error': 'Invalid request, please provide book info as json'}, 400 except Exception as e: return {'error': f'{e}'}, 500
import jwt from flask import request, Response from flask_apispec import MethodResource, doc, use_kwargs, marshal_with from flask_restful import Resource from marshmallow import Schema, fields from marshmallow_sqlalchemy import SQLAlchemyAutoSchema
from common.api_tools import token_required from common.constants import LOGIN_SECRET from models.book import Book from resources import api, docs, app from services.book_service import BookService
classBookRequestSchema(Schema): title = fields.String(required=True) author = fields.String(requird=True) publish_time = fields.DateTime(required=True)
classBookSchema(SQLAlchemyAutoSchema): classMeta: model = Book load_instance = True
classBookResource(MethodResource, Resource): @doc(description='Get a book by book id', tags=['Book Requests']) @marshal_with(BookSchema, 200) defget(self, book_id: int): book = BookService().get_book_by_id(book_id) if book: return book, 200 else: return {'error': f'Book(book_id:{book_id}) not found'}, 404
@doc(description='Update a book by book id', tags=['Book Requests']) @use_kwargs(TokenSchema, location='headers') @use_kwargs(BookRequestSchema, location='json') @marshal_with(BookSchema, 200) @token_required() defput(self, book_id: int, **kwargs): try: title = kwargs.get('title', None) author = kwargs.get('author', None) publish_time = kwargs.get('publish_time', None) book = Book(id=book_id, title=title, author=author, publish_time=publish_time)
book = BookService().update_book(book) return book, 200 except Exception as e: return {'error': f'{e}'}, 500
classBookListResource(Resource): defget(self): book_list = BookService().get_all_books() return [book.serialize() for book in book_list]
@token_required() defpost(self): try: request_json = request.json if request_json: title = request_json.get('title', None) author = request_json.get('author', None) publish_time_str = request_json.get('publish_time', None) publish_time = datetime.fromisoformat(publish_time_str) if publish_time_str elseNone
if title and author and publish_time: book = Book(title=title, author=author, publish_time=publish_time) book = BookService().create_book(book) return book.serialize(), 200 else: return {'error': 'Invalid request, please provide complete book info'}, 400 else: return {'error': 'Invalid request, please provide book info as json'}, 400 except Exception as e: return {'error': f'{e}'}, 500