Django REST Framework

Due Date

Introduction

What is REST and what are RESTful APIs?

Representational State Transfer (REST) is an architectural style that specifies a particular style (and a few constraints), that if applied to a web service render desirable services to work best on the Web , such as performance, scalability, and modifiability. 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.

Here you can learn more about RESTful Web Services

Introduction to REST APIs - RESTful Web Services - DZone Integration
Intro to RESTful Web Services REST stands for REpresentational State T ransfer. It is a popular architectural approach to create your API's in today's world. You may also like: Get Plenty of REST: REST API Tutorials What is REST? What are the fundamentals of REST APIs?
https://dzone.com/articles/introduction-to-rest-api-restful-web-services

Status codes are issued by a server in response to a client's request made to the server. The server always returns a message for every request, whether it is successful or not. Thus they are helpful to know the state of your code.

HTTP Status Codes
REST APIs use the Status-Line part of an HTTP response message to inform clients of their request's overarching result. RFC 2616 defines the Status-Line syntax as shown below: Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF HTTP defines forty standard status codes that can be used to convey the results of a client's request.
https://restfulapi.net/http-status-codes/

What is DRF? Why should I learn it?

Consider that after having completed the CSOC Django Assignment, you now built your own webapp backed with Django as the backend. Overnight your webapp became a star success!! Congrats! They love it so much, that they want a mobile phone version! Time to create an Android Version! And an iOs version....

And then you have to replicate 2 times the same code to Add, View, Remove, Update and Delete data, with different languages.

That opens the door to many possible mistakes… And that is where DRF shines out.....

In short, as DRF uses URIs for accessing the DataBase, we can provide access to the database to any application that can read from, generate and send an URI. May that be an android app, webapp, iOS app etc..

Consider this against using Django without DRF library, where to provide access to Android, iOS in addition to web, you have to replicate 2 times the same code to Add, View, Remove, Update and Delete data, with different languages.

All that DRF provides can be implemented through Django without explicitly using DRF, but it is tedious.

Now that was a good start to understanding DRF, but a developer's understanding and terminologies goes beyond... Offering you this possibility, have a go through the documentation.

Documentation and Installation

Home - Django REST framework
Django REST framework is a powerful and flexible toolkit for building Web APIs. Some reasons you might want to use REST framework: REST framework is a collaboratively funded project. If you use REST framework commercially we strongly encourage you to invest in its continued development bysigning up for a paid plan .
https://www.django-rest-framework.org/

Architecture

A DRF API is composed of 3 layers: the serializer, the viewset, and the router.

Quickstart

After having installed

Quickstart
We're going to create a simple API to allow admin users to view and edit the users and groups in the system. Create a new Django project named tutorial, then start a new app called quickstart.
https://www.django-rest-framework.org/tutorial/quickstart/

Searializers

Django models intuitively represent data stored in your database, but an API will need to transmit information in a less complex structure. While your data will be represented as instances of your Model classes in your Python code, it needs to be translated into a format like JSON in order to be communicated over an API.

You can learn about serializers here

1 - Serialization - Django REST framework
This tutorial will cover creating a simple pastebin code highlighting Web API. Along the way it will introduce the various components that make up REST framework, and give you a comprehensive understanding of how everything fits together. The tutorial is fairly in-depth, so you should probably get a cookie and a cup of your favorite brew before getting started.
https://www.django-rest-framework.org/tutorial/1-serialization/

Requests and Responses

Well aware that you are about the REST framework, there is nothing better than getting started right away with Django's Request and Responses.

2 - Requests and responses - Django REST framework
From this point we're going to really start covering the core of REST framework. Let's introduce a couple of essential building blocks. REST framework introduces a Request object that extends the regular HttpRequest, and provides more flexible request parsing.
https://www.django-rest-framework.org/tutorial/2-requests-and-responses/

ViewSets and Routers

A given serializer will parse information in both directions (reads and writes), but the ViewSet is where the available operations are defined. The most common ViewSet is the ModelViewSet, which has the following built-in operations:

6 - Viewsets and routers - Django REST framework
REST framework includes an abstraction for dealing with ViewSets, that allows the developer to concentrate on modeling the state and interactions of the API, and leave the URL construction to be handled automatically, based on common conventions.
https://www.django-rest-framework.org/tutorial/6-viewsets-and-routers/

Tutorial

Official Django REST Framework Tutorial - A Beginners Guide | LearnDjango
A beginner-friendly guide to the official Django REST Framework This is a beginner-friendly guide to the official Django Rest Framework tutorial. If you have struggled to complete the official tutorial on your own, consider this guide a good place to start instead. The final code is exactly the same and is available on Github.
https://wsvincent.com/official-django-rest-framework-tutorial-beginners-guide/

Django docs contain a tutorial to build Polls API. You can try implementing the same app using DRF. Follow below tutorials:

Introductions - Building API Django 2.0 documentation
Building APIs with Django and Django Rest Framework starts where the Django "Polls" tutorial stops, and takes you through building the polls app, but this time using APIs. You will learn the basics of Django Rest Framework including serialization, views, generic views, viewsets, testing, access control.
https://books.agiliq.com/projects/django-api-polls-tutorial/en/latest/introduction.html

Other resources

You can refer to other articles in these websites for better understanding of Django and DRF:

Categories
https://www.agiliq.com/categories/#drf
Will Vincent
Will is a developer and educator.
https://wsvincent.com/
Article Archive - Simple is Better Than Complex
This is a blog about Python, Django and Web Development. New posts every week.
https://simpleisbetterthancomplex.com/archive/

Clean Code

One should always use sensible names for views, variables etc. and write clean code. Follow this guide to understand how to name your urls.

REST API Naming Conventions and Best Practices - REST API Tutorial
In REST, primary data representation is called Resource. Having a strong and consistent REST resource naming strategy - will definitely prove one of the best design decisions in the long term. The key abstraction of information in REST is a resource. Any information that can be named can be a resource: a document or image, a temporal service (e.g.
https://restfulapi.net/resource-naming/

Python naming guidelines

PEP 8 -- Style Guide for Python Code
The official home of the Python Programming Language
https://www.python.org/dev/peps/pep-0008/

Task and Submission

So much for learning, here is your next Assignment!

All the details of the task are provided in the README.md file.

As you are aware, you need to fork from and the repository, clone the forked repository, complete the task, commit and push your changes and finally open the pull request back here.

https://github.com/COPS-IITBHU/csoc-2020-task-3/