This is a Demo REST API built with Django Rest Framework, and uses Postgresql as a DBMS and DRF Token based Authentication, Developed and tested on a debian based linux distro.
these instructions are tested on Debian based linux distro
- run
sudo apt update
. - run
sudo apt install postgresql-14
. - run
service postgresql start
after that make sure the service is running by usingservice postgresql status
. - change to postgres user by running
sudo su postgres
. - type
psql
to enter postgresql shell. - now create a new database user by typing
CREATE USER <choose_a_username> WITH PASSWORD '<choose_a_password>';
in the postgresql shell. - it will come in handy later for running tests that you add CREATEDB role to your new user by typing
ALTER USER <your_user_name> CREATEDB;
. - now create the actual DB that we will gonna use for this project by typing
CREATE DATABASE <db_name>
.
- create a new python virtual environment
python3 -m venv drf-task
and activate itsource drf-task/bin/activate
. - Clone this repo by runnin
git clone https://github.com/0xSaltyHash/demo-task
. - move to the project directory
cd demo-task
. - install the dependencies by running
pip install -r requiremnets.txt
. - create empty file and name it .env
touch .env
- run
python -c "import secrets; print(secrets.token_urlsafe())"
to generate a secret key and copy the output. - open the
.env
file and add this lineSECRET_KEY = <token_you_got_from_step_6>
note: add the token with no quotes. - add
DB_USER=<your_database_user_name>
, addDB_PASS=<your_db_password>
, addDB_NAME=<name_of_db>
.
Note: if you are hosting your psql DB on a server you should edit demo-task/settings.py and change HOST and PORT of the DB server
- now run
python manage.py makemigrations
then runpython manage.py migrate
- to deploy the application locally run python manage.py runserver
api/register
is used to register new users accepts onlyPOST
requests and expects a json body, to test it run
curl -X POST http://127.0.0.1:8000/api/register \
-H 'Content-Type: application/json' \
-d '{"username":"username1", "email":"testmail@mail.com", "password":"pass2311", "password2":"pass2311"}'
and it should return
{"username":"username1","email":"testmail@mail.com"}
api/login
will generate authorization token for a registered user to be able to interact with the API, and also it accepts onlyPOST
requests, to test it run
curl -X POST http://127.0.0.1:8000/api/login \
-H 'Content-Type: application/json' \
-d '{"username":"username1", "password":"pass2311"}'
it should return
{"token":"<random_token>"}
api/products/
is the api endpoint used to list and create products, to interact with it you should pass the authentication token in http Authorization Header, to test it:
- list products:
curl -X GET http://127.0.0.1:8000/api/products/ \
-H 'Authorization: token <your_token>'
will give a json response containing all products stored in our DB ordered by price.
- filter by username:
curl -X GET http://127.0.0.1:8000/api/products/?username=<username> \
-H 'Authorization: token <your_token>'
this should return a json object containing all products created by a user but if a user exists but no he didn'g create any order it will return empty list, and if the username doesn't exist it will return Not found
error.
- Create a new product:
curl -X POST http://127.0.0.1:8000/api/products/ \
-H 'Authorization: token <your_token>' \
-H 'Content-Type: application/json' \
-d '{"product_name":"prodname", "price":100}'
it will return
{"seller":"username1","product_name":"prodname","price":100.0}
python manage.py test api