From fa585ea87afc01e0bd367f162d7ee12a31e50850 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20H=C3=BCgel?= Date: Fri, 24 May 2019 16:15:37 +0100 Subject: [PATCH] Add support for localised item types / fields Only en-US is currently supported, but it will eventually just work. --- doc/index.rst | 4 ++-- pyzotero/zotero.py | 30 ++++++++++++++++++++---------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/doc/index.rst b/doc/index.rst index 6e047c6..bc12eb5 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -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() diff --git a/pyzotero/zotero.py b/pyzotero/zotero.py index 2a96879..8bcaeb8 100644 --- a/pyzotero/zotero.py +++ b/pyzotero/zotero.py @@ -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 @@ -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 @@ -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() @@ -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