Skip to content

Commit

Permalink
Updated client
Browse files Browse the repository at this point in the history
  • Loading branch information
romanchyla committed Jun 4, 2020
1 parent 8197c80 commit 6993cf2
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions biblib/client.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 6993cf2

Please sign in to comment.