[Python] REST API essential, convenient library summary

Introduction

An article about useful frameworks or libraries needed to create REST APIs in Python. This article uses Django, the Python web framework. I think Flask will be released separately. Also, to use each library and framework with Django, you need to install and edit settings.py.

Let's take a look! !!

Django Web framework. It has all the modules you need for your web application. For example, handling directory paths and databases effortlessly. It won't start without this.

Installation method

terminal


$ pip install django

Django Rest framework A collection of modules required for the REST API. Function to convert DB data to JSON format, etc. If you mention it, it will not be sharp. This is also essential.

Installation method

terminal


$ pip install djangorestframework

settings.py

settings.py


INSTALLED_APPS = [
    'rest_framework',
]

Add one line to INSTALLED_APPS as above. INSTALLED_APPS You can make django aware by defining an application. Parameters used when creating a migration file.

The explanation of INSTALLED_APPS is omitted hereafter.

SimpleJwt JWT (Json Web Token) can detect data tampering. Mainly used for authentication tokens. It's a security category, so it's sober, but I think it's better to use it properly in the portfolio.

Installation method

$ pip install djangorestframework-simplejwt

settings.py

settings.py


from datetime import timedelta

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ],
    'DEFAULT_AUTHENTICATION_CLASSES':[
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ],
}

SIMPLE_JWT = {
    'AUTH_HEADER_TYPES':('JWT'),
    'ACCESS_TOKEN_LIFETIME':timedelta(minutes=30),
}

REST_FRAMEWORK Make the djangorest framework recognize by defining a library for API. DEFAULT_PERMISSION_CLASSES Specify the class that determines the access permission. DEFAULT_AUTHENTICATION_CLASSES Specify the class to use for authentication. rest_framework.permissions.IsAuthenticated Request required for all requests. rest_framework_simplejwt.authentication.JWTAuthentication Define SIMPLE JWT. SIMPLE_JWT You can make settings related to SIMPLE JWT. AUTH_HEADER_TYPES You can specify a token. Specify JWT this time. ACCESS_TOKEN_LIFETIME Set the duration of the token. The timedelta object adds and subtracts the date and time.

djoser django's third party module. A module that supports authentication such as registration, login, logout, and password reset. As the name suggests, it's only for django, so it only works with django's REST API.

Installation method

$ pip install djoser

settings.py

settings.py


INSTALLED_APPS = [
    'djoser',
]

pillow Image processing library. Used when dealing with images. Simple processing such as resizing and rotation can be performed easily. Please note that advanced processing such as face recognition is not possible.

Installation method

$ pip install pillow

CORS A library that controls access from the front side. For example, the function to limit the request source. Resource sharing between origins in Japanese.

Installation method

$ pip install django-cors-headers

settings.py

settings.py


INSTALLED_APPS = [
    'corsheaders',
]
MIDDLEWARE = [
    #django.middleware.common.Write above Common Middleware.
    'corsheaders.middleware.CorsMiddleware',
]
CORS_ORIGIN_WHITELIST=[
    #Origin to allow
    "http://localhost:3000"
]

MIDDLEWARE By defining a list of middleware, django will execute it sequentially after HTTP processing. Since it is ** sequential **, the order of writing is important. CORS_ORIGIN_WHITELIST By defining the origin to allow, other requests will be played.

Finally

If you use these, you can implement REST API for general purposes. If you have any other necessary or useful libraries, please let us know. In addition, the construction of these development environments assumes a virtual environment. If you are interested in how to build a virtual environment, please try building it quickly from the following article. Create a virtual environment with Anaconda and work with PyCharm. Thank you for reading until the end.

References

Implementing API authentication function by JWT in Django Rest Framework

Recommended Posts

[Python] REST API essential, convenient library summary
Quickly implement REST API in Python
Python summary
Python 3.6 email library
Library comparison summary to generate PDF with Python
LINQ library summary
Python tutorial summary
Python Library notes
python related summary
[WP REST API v2] Upload images in Python
Touch the sample v20-python-samples of the OANDA v20 REST API wrapper library for Python
Python basics summary
Python beginners tried implementing REST API in one day
Hit Watson's REST API from Python on IBM Bluemix
[Python] A convenient library that converts kanji to hiragana
Using the National Diet Library Search API in Python
Summary about Python scraping
Python Django tutorial summary
python algorithmic trading library
Convenient Python methods, etc.
Summary about Python3 + OpenCV3
Evernote API in Python
Python function argument summary
Install python external library
OpenCV3 Python API list
Python directory operation summary
Python AI framework summary
Python iteration related summary
Python optimization library Pulp
Summary of Python arguments
C API in Python 3
TensorFlow API memo (Python)
kabu StationĀ® API-I made a Python wrapper for REST API
Specification generation and code generation in REST API development (Python edition)