Skip to content

Commit

Permalink
Bugfix for the client
Browse files Browse the repository at this point in the history
  • Loading branch information
romanchyla committed Jun 5, 2020
1 parent 7ec1878 commit 31859e6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
4 changes: 4 additions & 0 deletions config.py
@@ -1,4 +1,8 @@

# must be here for adsmutils to override it using env vars
# but if left empty (resolving to False) it won't be used
SERVICE_TOKEN = None

LOG_STDOUT = True
VAULT_QUERY_URL = 'https://api.adsabs.harvard.edu/v1/vault/query'
BUMBLEBEE_URL = 'https://qa.adsabs.harvard.edu/'
26 changes: 22 additions & 4 deletions tugboat/client.py
@@ -1,12 +1,14 @@
import requests
from flask import current_app, request

from flask import current_app
requests.packages.urllib3.disable_warnings()

client = lambda: Client(current_app.config).session
client = lambda: Client(current_app.config)


class Client:
"""
The Client class is a thin wrapper around adsmutils ADSFlask client; Use it as a centralized
The Client class is a thin wrapper around requests; Use it as a centralized
place to set application specific parameters, such as the oauth2
authorization header
"""
Expand All @@ -16,4 +18,20 @@ def __init__(self, config):
:param client_config: configuration dictionary of the client
"""

self.session = current_app.client # Use HTTP pool provided by adsmutils ADSFlask
self.session = requests.Session()

def _sanitize(self, args, kwargs):
headers = kwargs.get('headers', {})
if 'Authorization' not in headers:
headers['Authorization'] = current_app.config.get('SERVICE_TOKEN', None) or request.headers.get('X-Forwarded-Authorization', request.headers.get('Authorization', None))
kwargs['headers'] = headers
return (args, kwargs)

def get(self, *args, **kwargs):
args, kwargs = self._sanitize(args, kwargs)
return self.session.get(*args, **kwargs)

def post(self, *args, **kwargs):
args, kwargs = self._sanitize(args, kwargs)
return self.session.post(*args, **kwargs)

1 change: 0 additions & 1 deletion tugboat/views.py
Expand Up @@ -1425,7 +1425,6 @@ def post(self):
'fq': ['{!bitset}']
}

headers={'Authorization': current_app.config.get('SERVICE_TOKEN', request.headers.get('X-Forwarded-Authorization', request.headers.get('Authorization', '')))}

# POST the query
# https://api.adsabs.harvard.edu/v1/vault/query
Expand Down

0 comments on commit 31859e6

Please sign in to comment.