Browse Source

Rate limit methods

master
venkataakhil 2 years ago
parent
commit
c4a74cc81f
  1. 27
      smart_service/apis/master_api.py

27
smart_service/apis/master_api.py

@ -13,8 +13,28 @@ current_db_name = frappe.conf.get("db_name")
@frappe.whitelist(allow_guest=1)
@rate_limit(limit=5, seconds=15, methods=["POST"],ip_based=True)
def masters1(args=None, LSD=None):
def masters1():
val = rate_limit(limit=5, seconds=10)
if val != 1:
return val
return 1
def rate_limit(limit, seconds):
ip_based = True
ip = frappe.local.request_ip if ip_based is True else None
identity = None
identity = ip
cache_key = f"rl:{frappe.form_dict.cmd}:{identity}"
value = frappe.cache().get(cache_key) or 0
if not value:
frappe.cache().setex(cache_key, seconds, 0)
value = frappe.cache().incrby(cache_key, 1)
if value > limit:
frappe.local.response["http_status_code"] = 429
return "You hit the rate limit because of too many requests. Please try after sometime."
return 1
@ -134,7 +154,8 @@ def masters(args=None, LSD=None):
elif args == "fuel":
api = frappe.db.get_list(
"Fuel",
fields=["name as fuel", "active_status","myid as fuel_id", "modified"],
fields=["name as fuel", "active_status",
"myid as fuel_id", "modified"],
filters={"modified": [">", LSD]},
)
for i in api:

Loading…
Cancel
Save