This is an API built using djangorestframework that allows only an admin user to create, update and delete products, while allowing anyone to view available products.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
- Install Python
- Install a virtual environment
- Clone the repo to your local machine
git clone https://github.com/suleyunus/products-api.git
- cd into the project
cd products-api
- Activate the virtual environment. Check this link on how to activate a virtual environment depending on your OS
- Install dependencies
pip install -r requirements.txt
- Create Database
python manage.py migrate
- Create Admin(superuser)
python manage.py createsuperuser
- Run the server
python manage.py runserver
POST /api/v1/authtoken/
Generate an auth token
Request body:
{
"username": "your_admin_username",
"password": "your_admin_password"
}
Response:
{
"token": "generated_token"
}
POST /api/v1/products/
Create a product, requires authorisation
Request body:
{
"name": "name_of_product",
"description:: "description_of_product",
"price": "product_price"
}
Headers:
Authorization: Token your_generated_token
PUT /api/v1/products/{id}
Update a product by ID, requires authorisation
Request body:
{
"name": "new_name_of_product",
"description:: "new_description_of_product",
"price": "new_product_price"
}
Headers:
Authorization: Token your_generated_token
DELETE /api/v1/products/{id}
Delete product by ID, requires authorisation
Headers:
Authorization: Token your_generated_token
GET /api/v1/products
Get all productsGET /api/v1/products/{id}
Get product by ID