Skip to content

Commit

Permalink
Made the service configurable; to handle backend requests with a serv…
Browse files Browse the repository at this point in the history
…ice token
  • Loading branch information
romanchyla committed Jun 2, 2020
1 parent 0962406 commit 7407c8e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
2 changes: 0 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
VIS_SERVICE_TVRH_PATH = 'http://ecs-staging-elb-2044121877.us-east-1.elb.amazonaws.com/v1/search/tvrh'
VIS_SERVICE_SOLR_PATH = 'http://ecs-staging-elb-2044121877.us-east-1.elb.amazonaws.com/v1/search/query'

#This section configures this application to act as a client, for example to query solr via adsws
VIS_SERVICE_API_TOKEN = None

#word cloud config
VIS_SERVICE_WC_MAX_RECORDS = 500
Expand Down
32 changes: 22 additions & 10 deletions vis_services/client.py
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)

10 changes: 4 additions & 6 deletions vis_services/tests/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,17 @@ def create_app(self):
'''Create the wsgi application'''
app_ = app.create_app(**{
'FOO': ['bar', {}],
'VIS_SERVICE_API_TOKEN': 'secret_token',
'SERVICE_TOKEN': 'secret_token',
})

return app_

def test_solr_client(self):
from vis_services.client import client
from vis_services.client import client,Client

cl = client()
# Check whether the client is an instance of the correct type
self.assertIsInstance(cl, requests.sessions.Session)
# Check whether the client carries the API token
self.assertEqual(cl.headers['Authorization'], 'Bearer secret_token')
self.assertIsInstance(cl, Client)

def test_author_helper_functions(self):
import vis_services.lib.author_network as AN
Expand Down Expand Up @@ -353,4 +351,4 @@ def test_views_helper_functions(self):
pass
self.assertEqual(str(err), 'Nothing to calculate network!')



0 comments on commit 7407c8e

Please sign in to comment.