urschrei / pyzotero

Pyzotero: a Python client for the Zotero API
https://pyzotero.readthedocs.org
Other
911 stars 100 forks source link

add authorization header iff self.api_key exists #46

Closed alee closed 9 years ago

alee commented 9 years ago

We were trying to use pyzotero to access a public group zotero library and without setting the third api_key parameter but it errors out with the following:

In [1]: from pyzotero import zotero

In [2]: z = zotero.Zotero(284000, 'group')

In [3]: z.top(limit=5)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-39727ccebd40> in <module>()
----> 1 z.top(limit=5)

~/.virtualenvs/catalog/local/lib/python2.7/site-packages/pyzotero/zotero.pyc in wrapped_f(self, *args, **kwargs)
    118         if kwargs:
    119             self.add_parameters(**kwargs)
--> 120         retrieved = self._retrieve_data(func(self, *args))
    121         # we now always have links in the header response
    122         self.links = self._extract_links()

~/.virtualenvs/catalog/local/lib/python2.7/site-packages/pyzotero/zotero.pyc in _retrieve_data(self, request)
    252         self.request = requests.get(
    253             url=full_url,
--> 254             headers=self.default_headers())
    255         try:
    256             self.request.raise_for_status()

~/.virtualenvs/catalog/local/lib/python2.7/site-packages/pyzotero/zotero.pyc in default_headers(self)
    218         return {
    219             "User-Agent": "Pyzotero/%s" % __version__,
--> 220             "Authorization": "Bearer %s" % self.api_key,
    221             "Zotero-API-Version": "%s" % __api_version__,
    222             }

AttributeError: 'Zotero' object has no attribute 'api_key'

This PR attempts to fix that by only adding the "Authorization: Bearer ..." header if self.api_key is set. I also added a very simple test.

Thanks for providing this library, and please let me know if you need me to adjust the PR in any way.

urschrei commented 9 years ago

Merged and released. Thanks!