A robust REST API for managing books with automatic data enrichment from Google Books API. Built with Django REST Framework and featuring Redis caching, JWT authentication, and Swagger documentation.
-
CRUD Operations for Books
- Create, Read, Update, and Delete books
- Automatic data enrichment from Google Books API
- Input validation and error handling
-
Data Enrichment
- Automatic fetching of additional book data from Google Books API
- Enriched data includes:
- Book cover images
- Publisher information
- Page count
- Categories
- Ratings and reviews
- Description
- Preview links
-
Caching System
- Redis-based caching
- Cache invalidation strategies
- Configurable TTL (Time To Live)
- Performance optimization
-
Authentication & Security
- JWT (JSON Web Token) authentication
- Token refresh mechanism
- Protected endpoints
- Role-based access control
-
API Documentation
- Interactive Swagger UI
- ReDoc alternative interface
- Detailed endpoint descriptions
- Request/Response examples
- Backend Framework: Django 4.2
- API Framework: Django REST Framework 3.14
- Database: PostgreSQL 15
- Caching: Redis 7
- Documentation: drf-spectacular
- Authentication: djangorestframework-simplejwt
- Containerization: Docker & Docker Compose
- Proxy Server: Nginx
- Testing: pytest
books-api/
βββ books/ # Main application
β βββ api/ # API endpoints
β β βββ views.py # API views
β β βββ urls.py # URL routing
β β βββ serializers.py # API serializers
β βββ services/ # External services
β β βββ cache.py # Caching service
β β βββ enrichment.py # Google Books integration
β βββ models.py # Database models
β βββ admin.py # Admin interface
β βββ tests/ # Test suites
βββ core/ # Project settings
βββ nginx/ # Nginx configuration
βββ docker-compose.yml
βββ requirements.txt
- Docker and Docker Compose
- Git
-
Clone the repository:
git clone <repository-url> cd books-api
-
Create environment file:
cp .env.example .env
-
Build and run the containers:
docker compose up --build
-
Run migrations:
docker-compose exec web python manage.py migrate- Create a superuser to access the admin interface:
docker-compose exec web python manage.py createsuperuser- Load sample data (optional):
docker-compose exec web python manage.py seed_booksThe API will be available at http://localhost
DEBUG: Enable/disable debug modeDJANGO_SETTINGS_MODULE: Django settings modulePOSTGRES_DB: Database namePOSTGRES_USER: Database userPOSTGRES_PASSWORD: Database passwordREDIS_URL: Redis connection URL
There are two ways to obtain the JWT token for authentication:
-
Obtain JWT token:
curl -X POST http://localhost/api/token/ \ -H "Content-Type: application/json" \ -d '{"username": "your_username", "password": "your_password"}' -
Use the token in requests:
curl http://localhost/api/books/ \ -H "Authorization: Bearer your_token_here"
-
Make a POST request to
/api/token/with the following body:{ "username": "your_username", "password": "your_password" } -
The response will be in the format:
{ "access": "your_jwt_token_here", "refresh": "your_refresh_token_here" } -
To use the token, add the
Authorizationheader in all requests:Authorization: Bearer your_jwt_token_here -
To renew an expired token, use the
/api/token/refresh/endpoint with the refresh token:{ "refresh": "your_refresh_token_here" }
POST /api/token/: Obtain JWT tokenPOST /api/token/refresh/: Refresh JWT tokenGET /api/books/: List all booksPOST /api/books/: Create a new bookGET /api/books/{id}/: Get book detailsPUT /api/books/{id}/: Update a bookDELETE /api/books/{id}/: Delete a bookPOST /api/books/{id}/refresh_enriched_data/: Refresh book's enriched data
- Swagger UI:
http://localhost/api/docs/ - ReDoc:
http://localhost/api/redoc/
The project has comprehensive test coverage with different types of tests:
- Unit Tests
- Integration Tests
- API Tests
- Cache Tests
- Error Tests
To run tests with coverage report:
# Run tests
docker-compose exec web pytest
# Run tests with coverage
docker-compose exec web coverage run -m pytest
docker-compose exec web coverage reportCurrent test coverage is 99%, which is excellent for a production project. The only uncovered statements are specific error paths in the services layer that are challenging to simulate in tests, such as rare API response scenarios and specific JSON parsing errors.
Coverage by file:
models.py: 100%views.py: 100%services.py: 93%serializers.py: 100%urls.py: 100%admin.py: 100%tests.py: 100%
The project follows PEP 8 guidelines and uses:
- Black for code formatting
- isort for import sorting
- mypy for type checking
- Application logs: Available through Docker Compose logs
- Nginx access logs: Available in the nginx container
- Cache operations: Logged at INFO level
- API requests: Logged with detailed information
- Redis caching reduces load on Google Books API
- Nginx serves as reverse proxy and load balancer
- Database queries are optimized with proper indexing
-
Update environment variables:
- Set
DEBUG=0 - Use strong passwords
- Configure proper Redis and PostgreSQL settings
- Set
-
Security measures:
- Enable HTTPS
- Configure proper CORS settings
- Implement rate limiting
- Use secure headers
-
Performance optimization:
- Configure proper cache settings
- Optimize database queries
- Set up monitoring
This project is licensed under the MIT License - see the LICENSE file for details.