diff --git a/config.py b/config.py index 5b252bc..a91037b 100644 --- a/config.py +++ b/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/' diff --git a/tugboat/client.py b/tugboat/client.py index e3d1a01..9e46b57 100644 --- a/tugboat/client.py +++ b/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 """ @@ -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) + diff --git a/tugboat/views.py b/tugboat/views.py index 6d4b442..f91a606 100644 --- a/tugboat/views.py +++ b/tugboat/views.py @@ -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