Skip to content

Commit

Permalink
Merge pull request #20 from golnazads/master
Browse files Browse the repository at this point in the history
added user_id instead of session_id to the log
  • Loading branch information
golnazads authored May 7, 2018
2 parents acc061e + 9857e3e commit a21e684
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
6 changes: 3 additions & 3 deletions resolverway/tests/unittests/test_resolver_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,18 @@ def test_redis_put_get(self):
# verify that when the same session id is passed as cookie, the entry was fetched from redis
header = {'cookie': 'session=key1'}
r = self.client.get('/link_gateway/2018AAS...23130709A/ABSTRACT/https://ui.adsabs.harvard.edu/#abs/2018AAS...23130709A/ABSTRACT', headers=header)
self.assertEqual(r.headers['session_id'], 'sessionIDTestExample1')
self.assertEqual(r.headers['user_id'], '1')

# verify that when no cookie is send, session_id is None
r = self.client.get('/link_gateway/2018AAS...23130709A/ABSTRACT/https://ui.adsabs.harvard.edu/#abs/2018AAS...23130709A/ABSTRACT')
self.assertEqual(r.headers['session_id'], 'None')
self.assertEqual(r.headers['user_id'], 'None')

def test_adsws_call(self):
"""
:return:
"""
account = LinkRequest('2018AAS...23130709A', 'ABSTRACT', 'https://ui.adsabs.harvard.edu/#abs/2018AAS...23130709A/ABSTRACT').get_user_info_from_adsws('')
account = LinkRequest('2018AAS...23130709A', 'ABSTRACT', 'https://ui.adsabs.harvard.edu/#abs/2018AAS...23130709A/ABSTRACT').get_user_info_from_adsws(None)
self.assertEqual(account, None)


Expand Down
36 changes: 19 additions & 17 deletions resolverway/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,19 @@ def __init__(self, bibcode, link_type, url=None, id=None):
def redirect(self, link):
response = redirect(link, 302)
response.autocorrect_location_header = False
response.headers['session_id'] = self.user_id
response.headers['user_id'] = self.user_id
return response, 302

def process_resolver_response(self, the_json_response):
current_app.logger.info('from service got: %s'%(the_json_response))

action = the_json_response.get('action', '')

# when action is to redirect, there is only one link, so redirect to link
if (action == 'redirect'):
link = the_json_response.get('link', None)
if link:
# gunicorn does not like / so it is passed as , and returned back to / here if need to (i.e. for DOI and associated links)
# gunicorn does not like / so it is passed as , and returned back to / here if need to (i.e. for DOI links)
link = link.replace(',', '/')

current_app.logger.info('redirecting to %s' %(link))
Expand Down Expand Up @@ -73,17 +75,18 @@ def get_user_info_from_adsws(self, cookies):
:param request:
:return:
"""
try:
current_app.logger.info('getting user info from adsws with session=%s' % (cookies))
headers = {'Authorization': 'Bearer ' + current_app.config['RESOLVER_SERVICE_ADSWS_API_INFO_TOKEN']}
r = requests.get(url=current_app.config['RESOLVER_SERVICE_ACCOUNT_TOKEN_URL'], headers=headers,
cookies=cookies)
if r.status_code == 200:
return r.json()
except HTTPError as e:
current_app.logger.error("Http Error: %s" % (e))
except ConnectionError as e:
current_app.logger.error("Error Connecting: %s" % (e))
if cookies:
try:
current_app.logger.info('getting user info from adsws with session=%s' % (cookies))
headers = {'Authorization': 'Bearer ' + current_app.config['RESOLVER_SERVICE_ADSWS_API_INFO_TOKEN']}
r = requests.get(url=current_app.config['RESOLVER_SERVICE_ACCOUNT_TOKEN_URL'], headers=headers,
cookies=cookies)
if r.status_code == 200:
return r.json()
except HTTPError as e:
current_app.logger.error("Http Error: %s" % (e))
except ConnectionError as e:
current_app.logger.error("Error Connecting: %s" % (e))
return None

def set_user_info(self, request):
Expand All @@ -108,7 +111,7 @@ def set_user_info(self, request):
redis_db.set(session, account)
else:
return False
self.user_id = account['session_id']
self.user_id = account['user_id']
self.client_id = account['client_id']
self.access_token = account['access_token']
return True
Expand All @@ -124,14 +127,13 @@ def process_request(self):
current_app.logger.info('received request with bibcode=%s and link_type=%s' %(self.bibcode, self.link_type))
# fetch and log user info
if self.set_user_info(request):
current_app.logger.info('click logging info: user_id=%s, client_id=%s, access_token=%s' %(self.user_id, self.client_id, self.access_token))
current_app.logger.info('click logging info: user_id=%s, client_id=%s, access_token=%s'
%(self.user_id, self.client_id, self.access_token))
if self.referrer:
current_app.logger.info('also referrer=%s' %(self.referrer))

# if there is a url we need to log the request and redirect
if (self.url != None):
# gunicorn does not like / so it is passed as , and returned back to / here if need to (i.e. for DOI and associated links)
self.url = self.url.replace(',', '/')
current_app.logger.debug('received to redirect to %s' %(self.url))
log_request(self.bibcode, self.user_id, self.link_type, self.url, self.referrer, self.client_id, self.access_token)
return self.redirect(self.url)
Expand Down

0 comments on commit a21e684

Please sign in to comment.