-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8197c80
commit 6993cf2
Showing
1 changed file
with
22 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|