mirror of
https://github.com/LibreTranslate/LibreTranslate.git
synced 2024-11-09 01:10:14 +01:00
Merge pull request #96 from pierotofy/limitsfix
Fix daily limits for API key holders
This commit is contained in:
commit
9648f4ad33
41
app/app.py
41
app/app.py
@ -27,32 +27,39 @@ def get_remote_address():
|
|||||||
return ip
|
return ip
|
||||||
|
|
||||||
|
|
||||||
|
def get_req_limits(default_limit, api_keys_db, multiplier = 1):
|
||||||
|
req_limit = default_limit
|
||||||
|
|
||||||
|
if api_keys_db:
|
||||||
|
if request.is_json:
|
||||||
|
json = get_json_dict(request)
|
||||||
|
api_key = json.get("api_key")
|
||||||
|
else:
|
||||||
|
api_key = request.values.get("api_key")
|
||||||
|
|
||||||
|
if api_key:
|
||||||
|
db_req_limit = api_keys_db.lookup(api_key)
|
||||||
|
if db_req_limit is not None:
|
||||||
|
req_limit = db_req_limit
|
||||||
|
|
||||||
|
return req_limit
|
||||||
|
|
||||||
|
|
||||||
def get_routes_limits(default_req_limit, daily_req_limit, api_keys_db):
|
def get_routes_limits(default_req_limit, daily_req_limit, api_keys_db):
|
||||||
if default_req_limit == -1:
|
if default_req_limit == -1:
|
||||||
# TODO: better way?
|
# TODO: better way?
|
||||||
default_req_limit = 9999999999999
|
default_req_limit = 9999999999999
|
||||||
|
|
||||||
def limits():
|
def minute_limits():
|
||||||
req_limit = default_req_limit
|
return "%s per minute" % get_req_limits(default_req_limit, api_keys_db)
|
||||||
|
|
||||||
if api_keys_db:
|
def daily_limits():
|
||||||
if request.is_json:
|
return "%s per day" % get_req_limits(daily_req_limit, api_keys_db, 1440)
|
||||||
json = get_json_dict(request)
|
|
||||||
api_key = json.get("api_key")
|
|
||||||
else:
|
|
||||||
api_key = request.values.get("api_key")
|
|
||||||
|
|
||||||
if api_key:
|
res = [minute_limits]
|
||||||
db_req_limit = api_keys_db.lookup(api_key)
|
|
||||||
if db_req_limit is not None:
|
|
||||||
req_limit = db_req_limit
|
|
||||||
|
|
||||||
return "%s per minute" % req_limit
|
|
||||||
|
|
||||||
res = [limits]
|
|
||||||
|
|
||||||
if daily_req_limit > 0:
|
if daily_req_limit > 0:
|
||||||
res.append("%s per day" % daily_req_limit)
|
res.append(daily_limits)
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user