A simple Web Server to build a Book Archive. Users can add any books to the archive and manage their books, by updating and deleting. And everyone can view all books in the archive.
Copy file .env.example to .env and change the following lines according to database settings:
MONGO_HOST=0.0.0.0
MONGO_PORT=27017
MONGO_USERNAME=admin
MONGO_PASSWORD=admin123
MONGO_DATABASE=example_db
Install dependencies:
- (Optional) Create virtual environment
venv
$ python3 -m venv venv
$ source venv/bin/activate
- Install requirement libraries
$ pip3 install -r requirements.txt
Reference: Introduction | Sanic Framework
- Web Server: main.py
- Configs: config.py
- Example APIs: books_blueprint.py
Task 1:
- Run Web Server
$ python3 main.py
- Call APIs via browser and Postman
GET localhost:8080 GET localhost:8080/books
Reference: 1.Databases
- Use
MongoDBdatabase andPyMongolibrary - Database design: collections
- Functions to query and update data: mongodb.py
Task 2:
- Write functions to
create,get,updateanddeletea book
Reference: RESTful API
- API Get all books:
GET /books - Validate HTTP request body: json_validator.py
Task 3:
- Complete CRUD books APIs
Create a book: POST /books Read a book by ID: GET /books/{id} Update a book by ID: PUT /books/{id} Delete a book by ID: DELETE /books/{id}
Reference: Cache strategies
- Use
Redisin-memory data store - Functions to set and get cache: redis_cached.py
- Time-to-live
Task 4:
- API Get all books: Cache data response
Reference: JSON Web Tokens
- Use JSON Web Token
JWT - Generate JWT: jwt_utils.py
- Authenticate: auth.py
- Authorization: Check if the user has permission to take
Task 5:
- Write API Register and API Login
- API Create a book: Must be logged in
- API Update, Delete: Only owner are taken
Reference: Unit Test a REST API
- Automatic API testing
- Unittest for books APIs: testing.py
Task 6:
- Complete unittest for all APIs
- Run unittest
$ python3 testing.py

