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/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
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
After installing it, then put it in your installed apps
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.
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
Show configurations
Show configurations of remote servers for heroku
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