Simple JSON API with Flask
This challenge asks you to build a basic JSON API using Python and the Flask framework. APIs are fundamental to modern web development, enabling communication between different applications and services. This exercise will give you practical experience in handling HTTP requests, processing data, and returning JSON responses.
Problem Description
You are tasked with creating a simple API that manages a list of "books." The API should support the following functionalities:
- GET /books: Retrieve a list of all books. The books should be represented as a JSON array. Each book should have a
title(string) and anauthor(string). - GET /books/<book_id>: Retrieve a specific book by its ID. The ID is an integer. If the book is not found, return a 404 Not Found error with a JSON body containing a message "Book not found".
- POST /books: Add a new book to the list. The request body should be a JSON object with
titleandauthorfields. The API should assign a unique integer ID to the new book (starting from 1 and incrementing sequentially). Return a 201 Created status code and the newly created book (including its ID) in the response body. - PUT /books/<book_id>: Update an existing book. The request body should be a JSON object with
titleand/orauthorfields. If the book is not found, return a 404 Not Found error. Return a 200 OK status code and the updated book (including its ID) in the response body. - DELETE /books/<book_id>: Delete a book. If the book is not found, return a 404 Not Found error. Return a 204 No Content status code on successful deletion.
You should use an in-memory list to store the books. This means the data will be lost when the server restarts.
Examples
Example 1: GET /books
Input: (Request to /books)
Output:
[
{ "id": 1, "title": "The Lord of the Rings", "author": "J.R.R. Tolkien" },
{ "id": 2, "title": "Pride and Prejudice", "author": "Jane Austen" }
]
Explanation: The API returns a JSON array containing all books in the in-memory store.
Example 2: GET /books/1
Input: (Request to /books/1)
Output:
{ "id": 1, "title": "The Lord of the Rings", "author": "J.R.R. Tolkien" }
Explanation: The API returns the book with ID 1.
Example 3: POST /books
Input: (Request to /books with body: { "title": "1984", "author": "George Orwell" })
Output:
{ "id": 3, "title": "1984", "author": "George Orwell" }
Explanation: The API adds a new book with the provided title and author, assigns it ID 3, and returns the newly created book.
Example 4: DELETE /books/2
Input: (Request to /books/2)
Output: (Empty response body)
Status Code: 204 No Content
Explanation: The API deletes the book with ID 2 and returns a 204 status code.
Example 5: GET /books/99
Input: (Request to /books/99)
Output:
{ "message": "Book not found" }
Status Code: 404 Not Found
Explanation: The API returns a 404 error because there is no book with ID 99.
Constraints
- The book ID must be a positive integer.
- The
titleandauthorfields must be strings. - The API should handle invalid JSON input gracefully (e.g., return a 400 Bad Request error).
- The in-memory book list should be initialized with at least two books.
- The ID assignment for new books should be sequential, starting from 1.
- Assume the request body is always valid JSON if it is present.
Notes
- You can use the Flask framework to simplify the API development.
- Consider using Flask's built-in JSON handling capabilities.
- Error handling is important. Ensure your API returns appropriate HTTP status codes and informative error messages.
- Think about how to manage the in-memory book list and ensure data consistency.
- Focus on implementing the core functionalities first, and then add error handling and edge case management.
- You don't need to implement authentication or authorization for this challenge.