#drf#rest#api

RestFul Apis

  • Introduction to RestFul APIS

  • Installing Django Rest Framework

  • Creating Api View

  • Creating Serializers

  • Serializing and deserializing

WHAT IS A REST Framework

API : Application Proframming interface

Building an api is essetialy building an interface that client apps can use to get and save data

we can have endpoints like

  • /Products

  • /Carts

  • /Orders

Clients apps can send reqeusts to these endpoints to get or save products orders carts etc..

 What makes an api Restful ?

REST is short for : Representational State Transfer

In short, Rest defines a bunch of rules for client and server to communicate over the web

Following this way, it helps us build systems that are

  • Fast

  • Scalable

  • Reliable

  • Easy to understand

  • Easy to change

An API that confirms to these rules is called Restful

What are these rules or concepts?

  • Resources

  • Resource Representation

  • HTTP Methods

Resources(1)

The frist concept is recources, A resource in a restful api is like an object,    in our app , product,collection,cart etc…    these are on the web and client can access them using url

    http://daniel.com/products

    http://daniel.com/products/1

    http://daniel.com/products/1/reviews

    http://daniel.com/products/1/review/1

So we can Identify a recource Using its url

Resource Representation(2)

when you hit that resource aka url the server is going to return data in some sort of representation, may return it as html, XML(old), JSON(new) cause that is the format a client understand not a freaking python class , Rest Does not dicatate what to use , it is up to use

JSON:Javascript object Notation

 
    #Example of json
 
    {
 
        "name":'Daniel',
 
        "age":32,
 
        "is_online":true,
 
    }
 
  
 

HTTP Methods(3)

Each endpoint support various kind of operations some endpoint may only allow reading Data while may allow modifying data as well that is where http comes to picture Using HTTP the clinent can tell the http server what it wants to tdo with the resource

it represent methods like

  • Get : Getting resources

  • Post: Creating recourses

  • Put: Updating it

  • Patch: For updating part of it

  • Delete : for Deleting a resource

Installing Django Rest Framework

 
# To install DRF You simply Type the command
 
pipenv install djangorestframework
 
or
 
    pip install djangorestframework
 
    or
 
        python3 -m pip install djangorestframework
 

After installing it, then put it in your installed apps

 
INSTALLED_APPS = [
 
    'django.contrib.admin',
 
        ......
 
    'django.contrib.staticfiles',
 
    'rest_framework',
 
        ......
 
    ]
 

Serializers

DEPLOYMENT ON HEROKU

Step 1: Collect Statics

Everytime you want to deploy you want to run the collect static command to collect all static files.

 
python manage.py collectstatic
 

Step 2: Collect Statics

Django Does not support serving static files in production , even though we have static files we can not serve them , to add this feature we must install whitenoise

step3 Install White Noise

step4 Install guinicorn

Setting Environment variables on heroku

 
heroku config:set SECRET_KEY='59JO-q-n!2pyy-l4m05bpma26@qx3!nz#f5ezp&bnzoou#gmp@'
 

Show configurations

 
heroku config
 

Show configurations of remote servers for heroku

 
git remore -vv
 

Pushing to Heroku

Everytime you want to push to heroku, you create commits to your local repo then commit and then push the branch you want to heroku using

 

git push heroku main

Running Heroku Bash Commands from heroku

What if you want to access the shell or what to access your files using bash on heroku?

To access Bash simply

 

heroku run bash

Running Commands to our production server

 
heroku run COMMAND_YOU_WANT_TO_RUN