Skip to content

lovetonight/sanic-training

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

Practice

Description

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.

Use Case Diagram

Setup

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

Framework Sanic

Reference: Introduction | Sanic Framework

Task 1:

  • Run Web Server
$ python3 main.py
  • Call APIs via browser and Postman
GET localhost:8080
GET localhost:8080/books

Database

Reference: 1.Databases

  • Use MongoDB database and PyMongo library
  • Database design: collections
  • Functions to query and update data: mongodb.py

Task 2:

  • Write functions to create, get, update and delete a book

RESTful API

Reference: RESTful API

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}

Caching

Reference: Cache strategies

  • Use Redis in-memory data store
  • Functions to set and get cache: redis_cached.py
  • Time-to-live

Task 4:

  • API Get all books: Cache data response

Authentication & Authorization

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

JWT

Task 5:

  • Write API Register and API Login
  • API Create a book: Must be logged in
  • API Update, Delete: Only owner are taken

Unittest

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages