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.
python3
should be installed along with all the packages in requirements.txt
file
To install all the required dependencies
pip install -r requirements.txt
.
├── 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.
I have created 2 routers, /auth
and /book
as the first level root routers.
The /auth
router contains
-
/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 endpointsRequest
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
-
/home
endpoint provides all the books that are there in both the csv files as a JSONRequest
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 },.... ]
-
/addBook
aPOST
endpoint which is used to add a new entry to theregularUser.csv
add the last and after adding calling thehome
would return with this new entry appendedRequest
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 -
/removeBook
aDELETE
endpoint which is used to remove any entry from thecsv
provided the bookName which is found in theregularUser.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" }
Visit the /docs
endpoint for inbuilt docs auto generated by the FastAPI.