Skip to content

Commit

Permalink
Add support for localised item types / fields
Browse files Browse the repository at this point in the history
Only en-US is currently supported, but it will eventually just work.
  • Loading branch information
urschrei committed May 24, 2019
1 parent 9dbb541 commit fa585ea
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions doc/index.rst
Expand Up @@ -797,10 +797,10 @@ Pyzotero allows you to retrieve, delete, or modify saved searches:
Item Methods
=================

.. py:method:: Zotero.item_types()
.. py:method:: Zotero.item_types([locale])
Returns a dict containing all available item types

:param string locale: Clients can optionally request names in other languages by passing a locale parameter (e.g., "fr-FR"), however, only "en-US" is currently supported.
:rtype: dict

.. py:method:: Zotero.item_fields()
Expand Down
30 changes: 20 additions & 10 deletions pyzotero/zotero.py
Expand Up @@ -33,11 +33,12 @@
from __future__ import unicode_literals

__author__ = "Stephan Hügel"
__version__ = "1.3.15"
__version__ = "1.3.16"
__api_version__ = "3"

import sys
import requests
from requests import Request, Session
import socket
import feedparser
import bibtexparser
Expand Down Expand Up @@ -317,7 +318,7 @@ def _cleanup(self, to_clean, allow=()):
]
)

def _retrieve_data(self, request=None):
def _retrieve_data(self, request=None, params=None):
"""
Retrieve Zotero items via the API
Combine endpoint and request to access the specific resource
Expand All @@ -326,7 +327,7 @@ def _retrieve_data(self, request=None):
full_url = "%s%s" % (self.endpoint, request)
# The API doesn't return this any more, so we have to cheat
self.self_link = request
self.request = requests.get(url=full_url, headers=self.default_headers())
self.request = requests.get(url=full_url, headers=self.default_headers(), params=params)
self.request.encoding = "utf-8"
try:
self.request.raise_for_status()
Expand Down Expand Up @@ -1065,18 +1066,27 @@ def check_items(self, items):
)
return items

def item_types(self):
def item_types(self, locale="en-US"):
""" Get all available item types
"""
# Check for a valid cached version
if self.templates.get("item_types") and not self._updated(
"/itemTypes", self.templates["item_types"], "item_types"
):
return self.templates["item_types"]["tmplt"]
params = {"locale": locale}
query_string = "/itemTypes"
s = Session()
r = Request('GET', 'http://someurl.com', params=params).prepare()
# now split up the URL
result = urlparse(r.url)
# construct cache key
cachekey = result.path + "," + result.query
# the _updated call doesn't need the locale, since it's only checking for stale templates
if self.templates.get(cachekey) and not self._updated(
"/itemTypes", self.templates[cachekey], cachekey
):
return self.templates[cachekey]["tmplt"]
# otherwise perform a normal request and cache the response
retrieved = self._retrieve_data(query_string)
return self._cache(retrieved, "item_types")
retrieved = self._retrieve_data(query_string, params=params)

return self._cache(retrieved, cachekey)

def creator_fields(self):
""" Get localised creator fields
Expand Down

0 comments on commit fa585ea

Please sign in to comment.