This repository is a project demanded by an interviewer at Yara Corp. to implement a simple and basic RESTful server using the following stacks:
The so-called DB in this project is a single table containing the following fields:
| Field Name | Data Type | Constraint |
|---|---|---|
| purchase_date | datetime |
|
| purchase_name | string |
max_length=150 |
| user_id | integer |
|
| username | string (max_length=150) |
max_length=150 |
| phone_number | string (max_length=20) |
max_length=20 |
string (max_length=150) |
max_length=150 | |
| address | text |
This project was written using python3.7.2, so you might as well install that before going any further.
Python Official Website
You'll have to run this project in an environment, so I recommend installing them first hand.
pip install -U pip # install the latest pip, as it is updating frequently
pip install virtualenvYou can clone the repository using the following comands in your terminal:
git clone git@github.com:meysam81/yara.gitAnd then:
cd yaraNow you're gonna have to create a new virtual environment inside the project's root directory.
virtualenv venv
source ./venv/bin/activateNow install the requirements:
pip install -r requirements.txtNow you are good to go, just run the server and have fun 😃
python manage.py runserverThis is what you should prepend to every requesting URL:
localhost:8000
And you can login to the server using the following endpoint:
POST /api/login/With the body:
{
"username": "<username>",
"password": "<password>"
}Of course you're gonna have to insert some username & password in the DB so here's how you can do that. Enter the following command in your terminal:
python manage.py shellAnd then:
from django.contrib.auth.models import User
u = User(username="<username>", password="<password>")
u.save()After logging in from the login endpoint, a token is given back to you in a json format:
{
"token": "<token>"
}Include that token in every of your request's Header:
Authorization: Bearer <token>
And now you have access to not only reading the database, but also insert, update & deleting an object from the database:
| Method | Endpoint | Permission |
|---|---|---|
| GET | /purchases/ | anonymous access available |
| GET | /purchases/{ID}/ | anonymous access available |
| POST | /purchases/ | authenticated access only |
| DELETE | /purchases/{ID}/ | authenticated access only |
| PUT | /purchases/{ID}/ | authenticated access only |
| PATCH | /purchases/{ID}/ | authenticated access only |
Cheers! 🥂 And have fun. 💯
I don't know why you'd wanna do that, but PR's are welcomed anytime