Skip to content

whooaami/RestaurantAPI

Repository files navigation

Restaurant service using Django REST framework.

Client server logic

client-server

Database architecture

db

Structure

In a RESTful API, endpoints (URLs) define the structure of the API and how end users access data from our application using the HTTP methods - GET, POST, PUT, DELETE.

Endpoint HTTP Method CRUD Method Result
api/v1/employee GET READ Get all employees
api/v1/employee/<int:pk> GET READ Get a single employee
api/v1/employee POST CREATE Create a new employee
api/v1/employee/<int:pk> PUT UPDATE Update a employee
api/v1/employee/<int:pk> DELETE DELETE Delete a employee
api/v1/menu GET READ Get all menus
api/v1/menu/<int:pk> GET READ Get a single menu
api/v1/restaurant/<int:pk>/menus/ POST CREATE Upload a new menu for restaurant
api/v1/restaurant/<int:restaurant_id>/menu/ GET READ Get current day menu
api/v1/menu/<int:pk> PUT UPDATE Update a meu
api/v1/menu/<int:pk> DELETE DELETE Delete a menu
api/v1/restaurant GET READ Get all restaurants
api/v1/restaurant/<int:pk> GET READ Get a single restaurant
api/v1/restaurant/ POST CREATE Create a new restaurant
api/v1/restaurant/<int:pk> PUT UPDATE Update a restaurant
api/v1/restaurant/<int:pk> DELETE DELETE Delete a restaurant
api/v1/votes/<int:menu_id>/results/ GET READ Get results for current day

Installation

  1. Clone my repository to install project:
git clone https://github.com/whooaami/RestaurantAPI
  1. Open this project in your IDE.

  2. Create virtual environment:

python3.9 -m venv .venv
  1. Activate virtual environment:
source .venv/bin/activate
  1. Install all required libraries:
pip install -r requirements.txt
  1. To run server:
python manage.py runserver
  1. To run tests:
pytest
  1. To run docker:
docker-compose -d --build
  1. To make migrations on docker:
docker-compose exec web python manage.py migrate --noinput

Use

We can test the API using curl or httpie, or we can use Postman

First, we have to start up Django's development server.

python manage.py runserver

Only authenticated users can use the API services, for that reason if we try this:

http  http://127.0.0.1:8000/api/v1/employee/

we get:

{
    "detail": "Authentication credentials were not provided."
}

Instead, if we try to access with credentials:

http http://127.0.0.1:8000/api/v1/employee/3 "Authorization: JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjE2MjA4Mjk1LCJqdGkiOiI4NGNhZmMzMmFiZDA0MDQ2YjZhMzFhZjJjMmRiNjUyYyIsInVzZXJfaWQiOjJ9.NJrs-sXnghAwcMsIWyCvE2RuGcQ3Hiu5p3vBmLkHSvM"

we get the employee with id = 3

Create users and Tokens

First we need to create a user, so we can log in

http POST http://127.0.0.1:8000/api/v1/auth/register/ email="[email protected]" username="USERNAME" password1="PASSWORD" password2="PASSWORD"

After we create an account we can use those credentials to get a token

To get a token first we need to request

http http://127.0.0.1:8000/api/v1/auth/token/ username="username" password="password"

after that, we get the token

{
    "refresh": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTYxNjI5MjMyMSwianRpIjoiNGNkODA3YTlkMmMxNDA2NWFhMzNhYzMxOTgyMzhkZTgiLCJ1c2VyX2lkIjozfQ.hP1wPOPvaPo2DYTC9M1AuOSogdRL_mGP30CHsbpf4zA",
    "access": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjE2MjA2MjIxLCJqdGkiOiJjNTNlNThmYjE4N2Q0YWY2YTE5MGNiMzhlNjU5ZmI0NSIsInVzZXJfaWQiOjN9.Csz-SgXoItUbT3RgB3zXhjA2DAv77hpYjqlgEMNAHps"
}

We got two tokens, the access token will be used to authenticated all the requests we need to make, this access token will expire after some time. We can use the refresh token to request a need access token.

requesting new access token

http http://127.0.0.1:8000/api/v1/auth/token/refresh/ refresh="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTYxNjI5MjMyMSwianRpIjoiNGNkODA3YTlkMmMxNDA2NWFhMzNhYzMxOTgyMzhkZTgiLCJ1c2VyX2lkIjozfQ.hP1wPOPvaPo2DYTC9M1AuOSogdRL_mGP30CHsbpf4zA"

and we will get a new access token

{
    "access": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjE2MjA4Mjk1LCJqdGkiOiI4NGNhZmMzMmFiZDA0MDQ2YjZhMzFhZjJjMmRiNjUyYyIsInVzZXJfaWQiOjJ9.NJrs-sXnghAwcMsIWyCvE2RuGcQ3Hiu5p3vBmLkHSvM"
}

About

Restaurant service using Django REST framework

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published