diff --git a/biblib/client.py b/biblib/client.py index ad3eb74..ceec6c5 100644 --- a/biblib/client.py +++ b/biblib/client.py @@ -1,25 +1,36 @@ -from flask import current_app +import requests +from flask import current_app, request +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 """ def __init__(self, config): """ Constructor - - :param config: configuration dictionary of the client + :param client_config: configuration dictionary of the client """ - self.session = current_app.client # Use HTTP pool provided by adsmutils ADSFlask - self.token = config.get('BIBLIB_CLIENT_ADSWS_API_TOKEN') - if self.token: - self.session.headers.update( - {'Authorization': 'Bearer {0}'.format(self.token)} - ) + 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', request.headers.get('X-Forwarded-Authorization', request.headers.get('Authorization', None))) + kwargs['headers'] = headers + + def get(self, *args, **kwargs): + self._sanitize(*args, **kwargs) + return self.session.get(*args, **kwargs) + + def post(self, *args, **kwargs): + self._sanitize(*args, **kwargs) + return self.session.post(*args, **kwargs) +