BUILD THE REST API USING PYTHON DJANGO - PART 1 🐍
Tags: javascript, django, programming, python
Published: 2021-02-19 22:08:59

Table Of Contents
        * 🤓[INTRODUCTION](#intro)
        * 📦[WHAT IS AN API](#api)
        * 🕸[SEMANTICS OF HTTP METHODS](#http)
        * 🤔[HTTP METHODS](#methods)
        * 🤠[ABOUT DJANGO & DJANGO REST FRAMEWORK](#django)
        * ▶[GETTING STARTED](#started)
        * 🙏[THANK YOU](#thank-you)


## 🤓 INTRODUCTION <a name="intro"></a>
Hello, my dear hackers! I hope you are all having a great end of the working week. Today we are starting the new series where we are going to learn how to use Python and Django Rest Framework to build the REST **A**pplication **P**rogramming **I**nterface.

This series requires you to be in a good mood, be prepared to read, and have fun, we learn together! 🎉 

Please feel free to connect with me via [Twitter](https://twitter.com/lazar_dev), [Instagram](https://www.instagram.com/codespressoweb/) or [LinkedIn](https://www.linkedin.com/in/lazar-stankovic-44a199177/)

Let's do this!

![happy](https://media.giphy.com/media/ynRrAHj5SWAu8RA002/giphy.gif)

## 📦 WHAT IS AN API <a name="api"></a>
An API stands for an application programming interface. It represents a set of definitions and protocols for building and integrating application software. In other words, it is a way to programmatically interact with a separate software component or resource.
Web services were originally designed to communicate using **SOAP** or Simple Object Access Protocol. Which is a messaging protocol that sends XML documents over HTTP. But, today, most web-based APIs use the REST - Representational State Transfer - as an architectural style.

You might ask what the REST actually is?

REST was formally introduced in 2000 by [Roy Fielding in his doctoral dissertation](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#fig_5_8) where he explains the REST architecture in the following manner:

> The Representational State Transfer (REST) style is an abstraction of the architectural elements within a distributed hypermedia system. REST ignores the details of component implementation and protocol syntax in order to focus on the roles of components, the constraints upon their interaction with other components, and their interpretation of significant data elements. It encompasses the fundamental constraints upon components, connectors, and data that define the basis of the Web architecture, and thus the essence of its behavior as a network-based application.

Benefits of using the REST architecture:
- performance in component interactions, which can be the dominant factor in user-perceived performance and network efficiency;
- scalability
- simplicity of a uniform interface
- modifiability of components to meet changing needs (even while the application is running);
- visibility of communication between components by service agents;
- portability of components by moving program code with the data;
- reliability in the resistance to failure at the system level in the presence of failures within components, connectors, or data.


## 🕸 SEMANTICS OF HTTP METHODS <a name="http"></a>
Since we are building REST API, specifically HTTP-based REST APIs, the basic structure of the REST APIs is:
- a base URI, for example http://my-api.example.com/
- standard [HTTP request methods](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods) (GET, POST, PUT and DELETE)
- a media type that defines state transition data elements. The current representation tells the client how to compose requests for transitions to all the next available application states.

## 🤔 HTTP METHODS <a name="methods"></a>
### GET
The GET method requests a representation of the specified resource. Requests using GET should only retrieve data.

### HEAD
The HEAD method asks for a response identical to that of a GET request, but without the response body.

### POST
The POST method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server.

### PUT
The PUT method replaces all current representations of the target resource with the request payload.

### DELETE
The DELETE method deletes the specified resource.

### PATCH
The PATCH method is used to apply partial modifications to a resource.

## 🤠 ABOUT DJANGO & DJANGO REST FRAMEWORK <a name="django"></a>
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.
Django REST framework is a powerful and flexible toolkit for building Web APIs. Some reasons you might want to use the REST framework:
- The Web browsable API is a huge usability win for your developers.
- Authentication policies including packages for OAuth1a and OAuth2.
- Serialization that supports both ORM and non-ORM data sources.
- Customizable all the way down - just use regular function-based views if you don't need the more powerful features.
- Extensive documentation, and great community support.
- Used and trusted by internationally recognized companies including Mozilla, Red Hat, Heroku, and Eventbrite.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g0ogplq9y9rw9rl9y7ss.gif)

**Don't give up, we are getting somewhere!** 


## ▶ GETTING STARTED <a name="started"></a>
### REQUIREMENTS
[Python](https://www.python.org/) - v3.9.2
[PostgreSQL](https://www.postgresql.org/) - v12.2 (pgAdmin 4 -optional)
Django - v3.1.7
Django Rest Framework - v3.12.2
psycopg2 - v2.8.6

You will also need the IDE of your choice, I am using the [Jet Brains PyCharm Community Edition](https://www.jetbrains.com/pycharm/) and I strongly recommend it!

❗ You should probably add Python to your Environment variables if you don't have it already

So, this is what you need to be familiar with, of course, an actual topic of the REST paradigm, as well as HTTP, is much, much broader. Because of that, I **encourage** you to read a little bit more about it. Here are some useful links:

[REST](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#fig_5_8)
[HTTP](https://developer.mozilla.org/en-US/docs/Web/HTTP)

In the next chapter, we are going to **create the project**, setup **Django** and **Django rest framework**, and start writing our models, views, serializers, etc...

Stay tuned! 🚀

# 🙏 THANK YOU FOR READING!<a name="thank-you"></a>
References:
School notes...
School books...

Please leave a comment, tell me about you, about your work, comment your thoughts, connect with me! 

☕ SUPPORT ME AND KEEP ME FOCUSED!
<a href='https://ko-fi.com/Z8Z33D1HJ' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://cdn.ko-fi.com/cdn/kofi4.png?v=2' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>

Have a nice time hacking! 😊