@api.route("/books/") class BooksList(Resource):     def get(self):         """         returns a list of books         """          cursor = mydb.books.find({}, {"_id": 0})         data = []         for book in cursor:             data.append(book)          return {"response": data}      def post(self):         """         Add a new book to the list         """          data = request.get_json()         if not data:             data = {"response": "ERROR"}             return data, 404         else:             title = data.get('title')             if title:                 if mydb.books.find_one({"title": title}):                     return {"response": "book already exists."}, 403                 else:                     mydb.insert(data)   @api.route("/books/<string:title>") class Book(Resource):      def put(self, title):         """         Edits a selected book         """          data = request.get_json()          mydb.books.update({'title': title}, {'set': data})      def delete(self, title):         """     delete a selected book     """          mydb.books.delete({'title': title})

app = Flask(__name__) api = Api(app=app)
 ns_books = api.namespace('books', description = "Books operations") ns_movies = api.namespace('movies', description = "Movies operations")  @ns_books.route("/") class BooksList(Resource):     def get(self):         """         returns a list of books         """      def post(self):         """         Add a new book to the list         """  @ns_books.route("/<string:title>") class Book(Resource):      def put(self, title):         """         Edits a selected book         """       def delete(self, title):         """     delete a selected book     """  @ns_movies.route("/") class MoviesList(Resource):     def get(self):         """         returns a list of movies         """      def post(self):         """         Add a new movie to the list         """  @ns_movies.route("/<string:title>") class Movie(Resource):      def put(self, title):         """         Edits a selected movie         """      def delete(self, title):         """     delete a selected movie     """

@ns_books.route("/")
class BooksList(Resource):
    @api.response(200, 'Flask RESTPlus tuto : Success')
    @api.response(400, 'Flask RESTPlus tuto: Validation error')
    def get(self):
        """
        returns a list of books
        """

        cursor = mydb.books.find({}, {"_id": 0})
        data = []
        for book in cursor:
            data.append(book)

        return {"response": data}, 200

books_arguments = reqparse.RequestParser() books_arguments.add_argument('author', type=str, required=True) books_arguments.add_argument('language', type=str, choices=['en', 'fr'])   @ns_books.route("/") class BooksList(Resource):     @api.expect(books_arguments)     def get(self):         """         returns a list of books         """         data = books_arguments.parse_args(request)          if data.get('language'):             cursor = mydb.books.find({'author': data.get('author'), 'language': data.get('language')}, {"_id": 0})         else:             cursor = mydb.books.find({'author': data.get('author')}, {"_id": 0})         data = []         for book in cursor:             data.append(book)          return {"response": data}

from flask_restplus import fields

book_definition = api.model('Book Informations', {
    'author': fields.String(required=True),
    'title': fields.String(required=True),
    'language': fields.String(required=True)
})

@ns_books.route("/")
class BooksList(Resource):
    @api.expect(books_arguments)
    def get(self):
        """
        returns a list of books
        """

    @api.expect(book_definition)
    def post(self):
        """
        Add a new book to the list
        """
        data = request.get_json()
        title = data.get('title')
        if title:
            if mydb.books.find_one({"title": title}):
                return {"response": "book already exists."}
            else:
                mydb.books.insert_one(data)

list_books = api.model('Books', {
    'books': fields.List(fields.Nested(book_definition))
})

@ns_books.route("/")
class BooksList(Resource):
    @api.marshal_with(list_books)
    def get(self):
        """
        returns a list of books
        """
        data = books_arguments.parse_args(request)

        if data.get('language'):
            cursor=mydb.books.find({'author':data.get('author'),'language':data.get('language')}, {"_id":0})
        else:
            cursor = mydb.books.find({'author': data.get('author')}, {"_id":0})
        data = []
        for book in cursor:
            data.append(book)

        return {"books": data}