-
Notifications
You must be signed in to change notification settings - Fork 1
/
APICounter.py
67 lines (55 loc) · 2.56 KB
/
APICounter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import datetime
class APICounter:
"""
APICounter is a Python class that implements a simple counter system to track and limit the number of requests
allowed per day for each user.
Attributes:
max_requests_per_day (int): The maximum number of requests allowed per day.
requests (dict): A dictionary that stores the request count for each user, keyed by user ID.
Methods:
__init__(self, max_requests_per_day):
Initializes a new instance of the APICounter class with the specified maximum limit of requests per day.
check_limit(self, user_id):
Checks if the user has exceeded the maximum number of requests allowed per day based on the current date.
If the user has not exceeded the limit, updates the request count for the user and returns True. If the user
has exceeded the limit, returns False.
Usage:
# Create an instance of APICounter with a maximum limit of 5 requests per day
counter = APICounter(5)
# Example usage in your API endpoint
user_id = "user123"
if counter.check_limit(user_id):
# Process the API request for the user
print("API request processed.")
else:
print("API request limit exceeded.")
"""
def __init__(self, max_requests_per_day):
"""
Initializes a new instance of the APICounter class with the specified maximum limit of requests per day.
Args:
max_requests_per_day (int): The maximum number of requests allowed per day.
"""
self.max_requests_per_day = int(max_requests_per_day)
self.requests = {}
def check_limit(self, user_id):
"""
Checks if the user has exceeded the maximum number of requests allowed per day based on the current date.
If the user has not exceeded the limit, updates the request count for the user and returns True. If the user
has exceeded the limit, returns False.
Args:
user_id (str): The ID of the user making the API request.
Returns:
bool: True if the user has not exceeded the request limit, False otherwise.
"""
today = datetime.date.today()
if user_id not in self.requests:
self.requests[user_id] = {"date": today, "count": 0}
elif self.requests[user_id]["date"] != today:
self.requests[user_id]["date"] = today
self.requests[user_id]["count"] = 0
if self.requests[user_id]["count"] < self.max_requests_per_day:
self.requests[user_id]["count"] += 1
return True
else:
return False