Permalink
Browse files

Ajustes para efetuar requisições utilizando api_key

  • Loading branch information...
1 parent 2a2a48e commit fd0aa04bae01753e627c082b9eb3f601956b528a @gustavofonseca gustavofonseca committed Jan 14, 2013
Showing with 54 additions and 8 deletions.
  1. +37 −5 delorean/domain.py
  2. +1 −1 delorean/tests.py
  3. +8 −2 delorean/views.py
  4. +4 −0 development.ini
  5. +4 −0 production.ini
View
@@ -142,7 +142,12 @@ class DataCollector(object):
"""
__metaclass__ = ABCMeta
- def __init__(self, resource_url, slumber_lib=slumber, collection=None):
+ def __init__(self,
+ resource_url,
+ slumber_lib=slumber,
+ collection=None,
+ username=None,
+ api_key=None):
self._resource_url = resource_url
self._slumber_lib = slumber_lib
@@ -151,6 +156,9 @@ def __init__(self, resource_url, slumber_lib=slumber, collection=None):
self._collection = collection
+ self._username = username
+ self._api_key = api_key
+
# memoization to avoid unecessary field lookups
# Ex.: _memo['publishers']['1'] = 'Unesp'
self._memo = {}
@@ -162,6 +170,10 @@ def fetch_data(self, offset, limit, collection=None):
if collection:
kwargs['collection'] = collection
+ if all([self._username, self._api_key]):
+ kwargs['username'] = self._username
+ kwargs['api_key'] = self._api_key
+
return self.resource.get(offset=offset, limit=limit, **kwargs)
def __iter__(self):
@@ -206,9 +218,16 @@ def http_lookup():
"""
res_lookup_key = '%s-%s' % (endpoint, res_id)
if res_lookup_key not in self._last_resource:
+
+ # authorization params
+ kwargs = {}
+ if all([self._username, self._api_key]):
+ kwargs['username'] = self._username
+ kwargs['api_key'] = self._api_key
+
self._last_resource = {} # release the memory
self._last_resource[res_lookup_key] = getattr(
- self._api, endpoint)(res_id).get()
+ self._api, endpoint)(res_id).get(**kwargs)
return self._last_resource[res_lookup_key]
@@ -441,6 +460,8 @@ class DeLorean(object):
"""
def __init__(self,
api_uri,
+ username=None,
+ api_key=None,
datetime_lib=datetime,
titlecollector=TitleCollector,
issuecollector=IssueCollector,
@@ -453,6 +474,8 @@ def __init__(self,
self._issuecollector = issuecollector
self._sectioncollector = sectioncollector
self._transformer = transformer
+ self.username = username
+ self.api_key = api_key
def _generate_filename(self,
prefix,
@@ -474,7 +497,10 @@ def generate_title(self, target='/tmp/', collection=None):
expected_resource_name = self._generate_filename('title')
# data generator
- iter_data = self._titlecollector(self._api_uri, collection=collection)
+ iter_data = self._titlecollector(self._api_uri,
+ collection=collection,
+ username=self.username,
+ api_key=self.api_key)
# id file rendering
transformer = self._transformer(filename=os.path.join(HERE,
@@ -497,7 +523,10 @@ def generate_issue(self, target='/tmp/', collection=None):
expected_resource_name = self._generate_filename('issue')
# data generator
- iter_data = self._issuecollector(self._api_uri, collection=collection)
+ iter_data = self._issuecollector(self._api_uri,
+ collection=collection,
+ username=self.username,
+ api_key=self.api_key)
# id file rendering
transformer = self._transformer(filename=os.path.join(HERE,
@@ -520,7 +549,10 @@ def generate_section(self, target='/tmp/', collection=None):
expected_resource_name = self._generate_filename('section')
# data generator
- iter_data = self._sectioncollector(self._api_uri, collection=collection)
+ iter_data = self._sectioncollector(self._api_uri,
+ collection=collection,
+ username=self.username,
+ api_key=self.api_key)
# id file rendering
transformer = self._transformer(filename=os.path.join(HERE,
View
@@ -70,7 +70,7 @@ def test_generate_title_bundle(self):
dummy_datetime.strftime(ANY, ANY)
self.mocker.result('20120712-10:07:34:803942')
- dummy_titlecollector(ANY, collection=ANY)
+ dummy_titlecollector(ANY, collection=ANY, username=None, api_key=None)
self.mocker.result(dummy_titlecollector)
dummy_transformer(filename=ANY)
View
@@ -8,7 +8,6 @@
from pyramid import httpexceptions
HERE = os.path.abspath(os.path.dirname(__file__))
-SCIELOMANAGER_API_URI = 'http://localhost:8000/api/v1/'
RESOURCE_HANDLERS = {
'title': 'generate_title',
'issue': 'generate_issue',
@@ -27,8 +26,15 @@ def bundle_generator(request):
start_time = time.time()
resource_name = request.matchdict.get('resource')
collection = request.GET.get('collection', None)
+ username = request.registry.settings.get('delorean.manager_access_username', None)
+ api_key = request.registry.settings.get('delorean.manager_access_api_key', None)
+ api_uri = request.registry.settings.get('delorean.manager_access_uri', None)
- dl = DeLorean(SCIELOMANAGER_API_URI)
+ if not all([username, api_key, api_uri]):
+ raise httpexceptions.HTTPInternalServerError(
+ comment='missing configuration')
+
+ dl = DeLorean(api_uri, username=username, api_key=api_key)
try:
bundle_url = getattr(dl, RESOURCE_HANDLERS[resource_name])(
View
@@ -9,6 +9,10 @@ pyramid.default_locale_name = en
pyramid.includes =
pyramid_debugtoolbar
+delorean.manager_access_uri =
+delorean.manager_access_username =
+delorean.manager_access_api_key =
+
[server:main]
use = egg:waitress#main
host = 0.0.0.0
View
@@ -7,6 +7,10 @@ pyramid.debug_notfound = false
pyramid.debug_routematch = false
pyramid.default_locale_name = en
+delorean.manager_access_uri =
+delorean.manager_access_username =
+delorean.manager_access_api_key =
+
[server:main]
use = egg:waitress#main
host = 0.0.0.0

0 comments on commit fd0aa04

Please sign in to comment.