I tried saving the DRF API request history with django-request

It's a story of using django-request in DRF.

What you can do

--Save API request history --Items that can be saved --Status code --Method --Path --Request date and time --Whether SSL --Whether it is ajax communication --IP address (** problem, see below **) --Referer --User agent --Language

Things impossible

--History of the contents of the request body such as POST parameters ――Since there is also a security point of view, saving POST parameters is risky, isn't it?

procedure

1 Install with pip etc.

pip install django-request

2 Add request to ʻINSTALLED_APPS`

3 Add request.middleware.RequestMiddleware to MIDDLEWARE

4 Migration execution

manage.py migrate

The API request is now stored in the request_request table.

Customize

Exclusion path

Even the request of django-admin will be saved, so I think it is better to exclude it I made it like this (health is a pass for health check)

REQUEST_IGNORE_PATHS = (
    r'^admin/', r'^docs/', r'^health$', r'\.ico$', r'^static/'
)

About IP address

Since the IP address uses REMOTE_ADDR, it cannot be obtained correctly if there is a load balancer. I made it like this with a bittersweet measure (Please tell me if there is a better way)

from request.middleware import RequestMiddleware

class XRequestMiddleware(RequestMiddleware):
    def process_response(self, request, response):
        # django-REMOTE in request_I have saved ADDR as IP, so I'm forced to HTTP_X_FORWARDED_Change to FOR
        http_x_forwarded_for_ip = request.META.get('HTTP_X_FORWARDED_FOR')
        remote_addr_ip = request.META.get('REMOTE_ADDR')
        if http_x_forwarded_for_ip:
            request.META['REMOTE_ADDR'] = http_x_forwarded_for_ip

        response = super().process_response(request, response)

        if http_x_forwarded_for_ip:
            request.META['REMOTE_ADDR'] = remote_addr_ip

        return response

By the way, in HTTP_X_FORWARDED_FOR, IP addresses may be entered separated by commas, so if you use REQUEST_IGNORE_IP, you should do as follows.

http_x_forwarded_for_ip = request.META.get('HTTP_X_FORWARDED_FOR').split(',')[0]

Impressions

It's easy to incorporate, and it's essential for data-driven. Another issue is backup when data is accumulated. However, I want you to update the IP address ...

Recommended Posts

I tried saving the DRF API request history with django-request
I tried hitting the API with echonest's python client
I tried the Naro novel API 2
I tried the Naruro novel API
I tried using the checkio API
I tried to touch the COTOHA API
I tried playing with the image with Pillow
I tried using the BigQuery Storage API
I tried to get the movie information of TMDb API with Python
I tried "smoothing" the image with Python + OpenCV
I tried hitting the Qiita API from go
I tried to save the data with discord
I tried using the Google Cloud Vision API
I tried to touch the API of ebay
I tried "binarizing" the image with Python + OpenCV
I tried "Receipt OCR" with Google Vision API
I tried playing with the calculator on tkinter
I tried to learn the sin function with chainer
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I tried to uncover our darkness with Chatwork API
I tried using the API of the salmon data project
I tried to solve the problem with Python Vol.1
I moved the automatic summarization API "summpy" with python3.
I tried to find the entropy of the image with python
I tried fp-growth with python
I tried scraping with Python
I tried "gamma correction" of the image with Python + OpenCV
I tried to simulate how the infection spreads with Python
I tried to analyze the whole novel "Weathering with You" ☔️
[First COTOHA API] I tried to summarize the old story
I tried to automatically post to ChatWork at the time of deployment with fabric and ChatWork Api
I tried using the Python library from Ruby with PyCall
I tried to notify the train delay information with LINE Notify
I tried Learning-to-Rank with Elasticsearch!
I tried Google Sign-In with Spring Boot + Spring Security REST API
I tried clustering with PyCaret
I tried follow management with Twitter API and Python (easy)
I tried replacing the Windows 10 HDD with a smaller SSD
I touched the Qiita API
I tried the changefinder library!
I tried using the DS18B20 temperature sensor with Raspberry Pi
I tried touching the multi-vendor router control API library NAPALM
[Python] I tried collecting data using the API of wikipedia
I tried the Google Cloud Vision API for the first time
Call the API with python3.
I tried gRPC with Python
I tried scraping with python
I tried to divide the file into folders with Python
I tried to get various information from the codeforces API
[For beginners] I tried using the Tensorflow Object Detection API
I tried to verify the speaker identification by the Speaker Recognition API of Azure Cognitive Services with Python. # 1
I tried hitting the Google API with Ruby and Python-Make the database a Spreadsheet and manage it with Google Drive
I tried to verify the speaker identification by the Speaker Recognition API of Azure Cognitive Services with Python. # 2
PySpark learning record ② Kaggle I tried the Titanic competition with PySpark binding
I tried scraping the ranking of Qiita Advent Calendar with Python
[AWS / Tello] I tried operating the drone with my voice Part2
I tried to describe the traffic in real time with WebSocket
I tried to solve the ant book beginner's edition with python
I tried to automate the watering of the planter with Raspberry Pi
I tried cross-validation based on the grid search results with scikit-learn