-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made the service configurable; to handle backend requests with a serv…
…ice token
- Loading branch information
1 parent
0962406
commit 7407c8e
Showing
3 changed files
with
26 additions
and
18 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
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,24 +1,36 @@ | ||
from flask import current_app | ||
import requests | ||
from flask import current_app, request | ||
|
||
client = lambda: Client(current_app.config).session | ||
requests.packages.urllib3.disable_warnings() | ||
|
||
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 client_config: configuration dictionary of the client | ||
""" | ||
|
||
self.session = current_app.client # Use HTTP pool provided by adsmutils ADSFlask | ||
self.token = config.get('VIS_SERVICE_API_TOKEN') | ||
if self.token: | ||
self.session.headers.update( | ||
{'Authorization': 'Bearer %s' % 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) | ||
|
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