- Utilized Django / Django Rest Framework to build RESTful API.
- Used Django's Class-Based views for a more organized code structure.
- User Registration with username/password.
- User Authentication with JWT Token Authentication for more secure user interaction.
- Initialized
.env
file for security purposes of your environment variables. - Created
utils/
folder for extensibility of utility-like functions. - Dockerized application to be more scalable and stable.
- Implemented 4 Endpoints:
- RegisterUser
- GetCharacter
- AddFavoriteCharacter
- ListFavoriteCharacters
- Wrote 6 unit test cases for the endpoints.
- Utilized PostgreSQL as the database for more efficient data storage.
- Created URLs based on RESTful URL conventions.
- Swagger Documentation(configured with drf-spectacular).
- Postman Collection(you can find it in the
external/
folder). - API Flowchart(made with draw.io).
Please keep in mind that this application is not production-ready. This project uses entrypoint.sh
bash script to run application on Docker. We should use web server like gunicorn
or uvicorn
to serve our application to users. Also, to handle high traffic, we should use message brokers like RabbitMQ/Redis with Celery(asynchronous task queue) and monitoring software like Prometheus/Grafana to track application performance and identify areas for improvement.
character/
: Django app that contains all the models, views, and serializers.
lotr_rest_api/
: Django project that contains configuration files, such as: settings, swagger urls, and etc.
external/
: Contains external files, such as: flowchart, markdown files, postman collection, and etc.
character/utils/
: Contains utility functions, such as: get_character_by_id
to not repeat the code on multiple places.
.env.example
: Contains environment variables, which should be copied to .env
file.
- RegisterUser - POST -
/api/v1/register/
- TokenObtainPair - POST -
/api/v1/login/
- TokenRefresh - POST -
/api/v1/login/refresh/
- GetCharacter - This endpoint used for getting a character details by id from One API - GET -
/api/v1/character/{id}/
- ListCharacters - This endpoint is for listing authenticated user's favorite characters - GET -
/api/v1/list/
- AddFavoriteCharacter - This endpoint is for adding a character to authenticated user's favorite characters - POST -
/api/v1/character/
My tests include 6 unit test cases for the endpoints:
-
AddFavoriteCharacterTest - 3 test cases
- test_add_favorite_character_success
- test_add_favorite_character_invalid_id
- test_add_favorite_character_already_exists
-
ListFavoriteCharactersTest - 1 test cases(excluding initial setup)
- test_list_favorite_characters
-
GetCharacterTest - 1 test cases(excluding initial setup)
- test_get_character_success
- test_get_character_not_found
I didn't write tests for JWT Token Authentication, because I'm initializing every test case with a new user and I'm not using the same user for multiple test cases. So, I don't need to authenticate the user for the tests.
You can run the tests using the following command:
python manage.py test
There are two ways to run the project:
- Using Docker
- Using Python
- Clone the repository
- Copy
.env.example
to.env
and fill the variables - Run
docker compose up --build
- Access the API at
http://0.0.0.0:8000/api/v1/
- Access the Swagger documentation at
http://0.0.0.0:8000/swagger/
- Access the Redoc documentation at
http://0.0.0:8000/redoc/
- Clone the repository
- Create a virtual environment
- Install the dependencies using
pip install -r requirements.txt
- Copy
.env.example
to.env
and fill the variables - Apply the migrations using
python manage.py makemigrations
andpython manage.py migrate
- Run the server using
python manage.py runserver
- Access the API at
http://127.0.0.1:8000/api/v1/