Skip to content

Commit

Permalink
Api gateway patch (#45)
Browse files Browse the repository at this point in the history
* Update expires_in to expires_at

* Fixes #42.

* Updated expires_at from timestamp to Unix Epoch seconds.
  • Loading branch information
tjacovich authored Nov 22, 2024
1 parent 1de353d commit 0513518
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions adscore/api/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ def _bootstrap(self):
params = None
bootstrap_response = self.request(current_app.config['BOOTSTRAP_SERVICE'], params,
method="GET", retry_counter=0)
if 'access_token' not in bootstrap_response or 'expire_in' not in bootstrap_response:
if 'access_token' not in bootstrap_response or 'expires_at' not in bootstrap_response:
abort(500, "Bootstrap returned invalid data")
current_app.logger.info("Bootstrapped access token '%s'", bootstrap_response['access_token'])
self.auth = { 'access_token': bootstrap_response['access_token'], 'expire_in': bootstrap_response['expire_in'], 'bot': False }
self.auth = { 'access_token': bootstrap_response['access_token'], 'expires_at': bootstrap_response['expires_at'], 'bot': False }

def request(self, endpoint, params, method="GET", headers=None, retry_counter=0, json_format=True):
"""
Expand Down
8 changes: 4 additions & 4 deletions adscore/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ def before_request():
evaluation = crawlers.evaluate(remote_ip, user_agent)
if evaluation == crawlers.VERIFIED_BOT:
# Extremely high rate limit
RequestsManager.init(auth={'access_token': app.config['VERIFIED_BOTS_ACCESS_TOKEN'], 'expire_in': "2050-01-01T00:00:00", 'bot': True}, cookies={})
RequestsManager.init(auth={'access_token': app.config['VERIFIED_BOTS_ACCESS_TOKEN'], 'expires_at': "16725225600", 'bot': True}, cookies={})
elif evaluation == crawlers.UNVERIFIABLE_BOT:
# Slightly higher rate limit
RequestsManager.init(auth={'access_token': app.config['UNVERIFIABLE_BOTS_ACCESS_TOKEN'], 'expire_in': "2050-01-01T00:00:00", 'bot': True}, cookies={})
RequestsManager.init(auth={'access_token': app.config['UNVERIFIABLE_BOTS_ACCESS_TOKEN'], 'expires_at': "16725225600", 'bot': True}, cookies={})
elif evaluation == crawlers.POTENTIAL_MALICIOUS_BOT:
# Rate limits as a regular user with the advantage that there is no bootstrap
RequestsManager.init(auth={'access_token': app.config['MALICIOUS_BOTS_ACCESS_TOKEN'], 'expire_in': "2050-01-01T00:00:00", 'bot': True}, cookies={})
RequestsManager.init(auth={'access_token': app.config['MALICIOUS_BOTS_ACCESS_TOKEN'], 'expires_at': "16725225600", 'bot': True}, cookies={})

if not RequestsManager.is_initialized():
if request.cookies.get('session'):
Expand Down Expand Up @@ -115,7 +115,7 @@ def ratelimit_handler(e):
if e.description.endswith('per 1 day'):
# ADS Core limit hit (to limit too many bootstraps)
remote_ip = get_remote_address()
description = "We have received too many requests from your IP ({}).".format(remote_ip)
description = "We have received too many requests from your IP ({}) for the current 24 hour period. Please try again later.".format(remote_ip)
else:
# API ratelimit hit
description = e.description
Expand Down
10 changes: 5 additions & 5 deletions adscore/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

def is_expired(auth):
try:
expire_in = datetime.strptime(auth['expire_in'], "%Y-%m-%dT%H:%M:%S")
expires_at = datetime.strptime(auth['expires_at'], "%Y-%m-%dT%H:%M:%S")
except (ValueError, KeyError):
try:
expire_in = datetime.strptime(auth['expire_in'], "%Y-%m-%dT%H:%M:%S.%f")
expires_at = datetime.strptime(auth['expires_at'], "%Y-%m-%dT%H:%M:%S.%f")
except (ValueError, KeyError):
expire_in = None
if expire_in:
delta = expire_in - datetime.now()
expires_at = None
if expires_at:
delta = expires_at - datetime.now()
return delta.total_seconds() < 0
else:
return True
Expand Down

0 comments on commit 0513518

Please sign in to comment.