Skip to content

Commit

Permalink
manubot#337 Add timeout to zotero searches.
Browse files Browse the repository at this point in the history
  • Loading branch information
xihh87 committed Jun 15, 2022
1 parent 58fb8ab commit 63452fa
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions manubot/cite/zotero.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
"""URL that provides access to the Zotero translation-server API"""


def web_query(url: str) -> ZoteroData:
def web_query(url: str, timeout=3) -> ZoteroData:
"""
Return Zotero citation metadata for a URL as a list containing a single element that
is a dictionary with the URL's metadata.
"""
headers = {"User-Agent": get_manubot_user_agent(), "Content-Type": "text/plain"}
params = {"single": 1}
api_url = f"{base_url}/web"
response = requests.post(api_url, params=params, headers=headers, data=str(url))
response = requests.post(api_url, params=params, headers=headers, data=str(url), timeout=3)
try:
zotero_data = response.json()
except Exception as error:
Expand All @@ -52,7 +52,7 @@ def web_query(url: str) -> ZoteroData:
return zotero_data


def search_query(identifier: str) -> ZoteroData:
def search_query(identifier: str, timeout=3) -> ZoteroData:
"""
Retrive Zotero metadata for a DOI, ISBN, PMID, or arXiv ID.
Example usage:
Expand All @@ -66,7 +66,7 @@ def search_query(identifier: str) -> ZoteroData:
"""
api_url = f"{base_url}/search"
headers = {"User-Agent": get_manubot_user_agent(), "Content-Type": "text/plain"}
response = requests.post(api_url, headers=headers, data=str(identifier))
response = requests.post(api_url, headers=headers, data=str(identifier), timeout=timeout)
try:
zotero_data = response.json()
except Exception as error:
Expand All @@ -93,7 +93,7 @@ def _passthrough_zotero_data(zotero_data: ZoteroData) -> ZoteroData:
return zotero_data


def export_as_csl(zotero_data: ZoteroData) -> CSLItems:
def export_as_csl(zotero_data: ZoteroData, timeout=3) -> CSLItems:
"""
Export Zotero JSON data to CSL JSON using a translation-server /export query.
Performs a similar query to the following curl command:
Expand All @@ -107,7 +107,7 @@ def export_as_csl(zotero_data: ZoteroData) -> CSLItems:
api_url = f"{base_url}/export"
params = {"format": "csljson"}
headers = {"User-Agent": get_manubot_user_agent()}
response = requests.post(api_url, params=params, headers=headers, json=zotero_data)
response = requests.post(api_url, params=params, headers=headers, json=zotero_data, timeout=timeout)
if not response.ok:
message = f"export_as_csl: translation-server returned status code {response.status_code}"
logging.warning(f"{message} with the following output:\n{response.text}")
Expand All @@ -131,14 +131,14 @@ def get_csl_item(identifier: str) -> CSLItem:
return csl_item


def search_or_web_query(identifier: str) -> ZoteroData:
def search_or_web_query(identifier: str, timeout=3) -> ZoteroData:
"""
Detect whether `identifier` is a URL. If so,
retrieve zotero metadata using a /web query.
Otherwise, retrieve zotero metadata using a /search query.
"""
if is_http_url(identifier):
zotero_data = web_query(identifier)
zotero_data = web_query(identifier, timeout=timeout)
else:
zotero_data = search_query(identifier)
zotero_data = search_query(identifier, timeout=timeout)
return zotero_data

0 comments on commit 63452fa

Please sign in to comment.