'Django frequently used api
I created a project and 5 applications
Now the problem is how many times the particular api is used by all or any particular user
Can any one help me
We can create new api or anything but I need output for above
Solution 1:[1]
Try to use request/response signals.
Signals sent by the core framework when processing a request.
django.core.signals.request_started
Then add signals receiver like this
from django.dispatch import receiver
@receiver(request_finished)
def my_callback(sender, **kwargs):
print("Request finished!")```
Solution 2:[2]
You need to create a model to save the API counts of the user.
from django.db import models
from django.contrib.auth.models import User
class ApiCounts(models.Model):
api_name = models.CharField(max_length=1024)
user = models.ForeignKey(
User, on_delete=models.CASCADE, related_name="api_counts", blank=True
)
total_count = models.PositiveBigIntegerField(default=0)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
Using the django middlewares, inside the process_request method, you can increment the count of API (URL and view_name are inside the request object) for a user if request is authenticated, otherwise use null in the user field.
So, you can show per-user API counts as well as total-counts of a particular API by querying that model.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Y U |
| Solution 2 | Abhishek Baghel |
