BUILD THE REST API USING PYTHON DJANGO - PART 1 🐍

devlazar

devlazar

Posted on February 19, 2021

BUILD THE REST API USING PYTHON DJANGO - PART 1 🐍

Table Of Contents
* 🤓INTRODUCTION
* 📦WHAT IS AN API
* 🕸SEMANTICS OF HTTP METHODS
* 🤔HTTP METHODS
* 🤠ABOUT DJANGO & DJANGO REST FRAMEWORK
* ▶GETTING STARTED
* 🙏THANK YOU

🤓 INTRODUCTION

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 Application Programming Interface.

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, Instagram or LinkedIn

Let's do this!

happy

📦 WHAT IS AN API

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 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

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 (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

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

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

Don't give up, we are getting somewhere!

▶ GETTING STARTED

REQUIREMENTS

Python - v3.9.2
PostgreSQL - 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 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
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!

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!
Buy Me a Coffee at ko-fi.com

Have a nice time hacking! 😊

💖 💪 🙅 🚩
devlazar
devlazar

Posted on February 19, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related