Skip to content

princesanjivy/scalex-task

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Context

Built this simple server with endpoints as part of the assignment task for job application process at ScaleX.media. The APIs are built using the FastAPI python framework.

Prerequisite

python3 should be installed along with all the packages in requirements.txt file

To install all the required dependencies

pip install -r requirements.txt

Project Structure

.
├── data
│   ├── adminUser.csv
│   ├── book.py
│   ├── regularUser.csv
│   ├── user.csv
│   └── user.py
├── main.py
├── model
│   ├── book.py
│   └── user.py
├── README.md
├── requirements.txt
├── router
│   ├── auth.py
│   ├── book.py
└── utils
    ├── secret.py
    └── user.py

This project contains all the endpoint related functions inside the router directory and data models are listed in model's directory. The data directory acts as the database for the application where the 3 csv files present.

Endpoints

I have created 2 routers, /auth and /book as the first level root routers.

The /auth router contains

  1. /login endpoint is an unprotected endpoint which takes the user id in the request payload and return a Bearer Token (JWT) which is required for accessing rest of the endpoints

    Request

    curl  -X POST \
    'http://localhost:8000/auth/login' \
    --header 'Content-Type: application/json' \
    --data-raw '{
    "user_id":2
    }'

    Response

    {
        "type": "bearer",
        "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyIiwidXNlcm5hbWUiOiJTY2FsZXgiLCJleHAiOjE3MTQ3ODI4MDR9.bF8egGl7wN75W29UxZxaQguT2Jj0WW-63uPJl6cnHu4"
    }

The /book router contains, under these all the endpoints requires token

  1. /home endpoint provides all the books that are there in both the csv files as a JSON

    Request

    curl  -X GET \
    'http://localhost:8000/book/home' \
    --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyIiwidXNlcm5hbWUiOiJTY2FsZXgiLCJleHAiOjE3MTQ3ODI4MDR9.bF8egGl7wN75W29UxZxaQguT2Jj0WW-63uPJl6cnHu4'

    Response

    [
        {
            "Book Name": "The Da Vinci Code",
            "Author": "Dan Brown",
            "Publication Year": 2003
        },
        {
            "Book Name": "Think and Grow Rich",
            "Author": "Napoleon Hill",
            "Publication Year": 1937
        },....
    ]
  2. /addBook a POST endpoint which is used to add a new entry to the regularUser.csv add the last and after adding calling the home would return with this new entry appended

    Request

    curl  -X POST \
    'http://localhost:8000/book/addBook' \
    --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyIiwidXNlcm5hbWUiOiJTY2FsZXgiLCJleHAiOjE3MTQ3ODI4MDR9.bF8egGl7wN75W29UxZxaQguT2Jj0WW-63uPJl6cnHu4' \
    --header 'Content-Type: application/json' \
    --data-raw '{
    "Book Name": "Love Story",
    "Author": "Erich",
    "Publication Year": 1970
    }'

    Response

    No response, but instead returns with HTTP 201 Created as the status

  3. /removeBook a DELETE endpoint which is used to remove any entry from the csv provided the bookName which is found in the regularUser.csv

    Request

    curl  -X DELETE \
    'http://localhost:8000/book/removeBook' \
    --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyIiwidXNlcm5hbWUiOiJTY2FsZXgiLCJleHAiOjE3MTQ3ODI4MDR9.bF8egGl7wN75W29UxZxaQguT2Jj0WW-63uPJl6cnHu4' \
    --header 'Content-Type: application/json' \
    --data-raw '{
    "Book Name": "Love Story"
    }'

    Response

    {
        "message": "Love Story book has been deleted"
    }

Docs

Visit the /docs endpoint for inbuilt docs auto generated by the FastAPI.