Skip to content

Commit 0b9cca8

Browse files
Merge pull request #3 from LCAS/copilot/fix-json-decode-error
Add response validation before JSON parsing to prevent decode errors
2 parents 7322338 + 8b13ec8 commit 0b9cca8

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

figshare.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,14 @@ def shorten(self, doi):
5353
quoted_doi = urllib.request.quote(doi)
5454
url = 'http://shortdoi.org/{}?format=json'.format(quoted_doi)
5555
try:
56-
response = requests.get(url).json()
57-
short_doi = response['ShortDOI']
56+
response = requests.get(url)
57+
# Check if response is valid and contains JSON
58+
if response.ok and response.headers.get('Content-Type', '').lower().startswith('application/json') and response.text.strip():
59+
result = response.json()
60+
short_doi = result['ShortDOI']
61+
else:
62+
self.logger.warning(f"Received empty or invalid JSON response for {doi} from {url} (status: {response.status_code})")
63+
return None
5864
except Exception as e:
5965
self.logger.warning(f"failed to get short doi for {doi}: {e}")
6066
return None
@@ -150,21 +156,33 @@ def __get(self, url, params=None, use_cache=True):
150156
return self.__cache[hash_key]
151157
else:
152158
headers = { "Authorization": "token " + self.token } if self.token else {}
153-
result = get(self.base_url + url, headers=headers, params=params).json()
154-
self.__cache[hash_key] = result
155-
self.save_cache()
156-
return result
159+
response = get(self.base_url + url, headers=headers, params=params)
160+
# Check if response is valid and contains JSON
161+
if response.ok and response.headers.get('Content-Type', '').lower().startswith('application/json') and response.text.strip():
162+
result = response.json()
163+
self.__cache[hash_key] = result
164+
self.save_cache()
165+
return result
166+
else:
167+
self.logger.warning(f"Received empty or invalid JSON response for GET {self.base_url + url} (status: {response.status_code})")
168+
return {}
157169

158170
def __post(self, url, params=None, use_cache=True):
159171
hash_key = f"POST{url}?{params}"
160172
if hash_key in self.__cache and use_cache:
161173
return self.__cache[hash_key]
162174
else:
163175
headers = { "Authorization": "token " + self.token } if self.token else {}
164-
result = post(self.base_url + url, headers=headers, json=params).json()
165-
self.__cache[hash_key] = result
166-
self.save_cache()
167-
return result
176+
response = post(self.base_url + url, headers=headers, json=params)
177+
# Check if response is valid and contains JSON
178+
if response.ok and response.headers.get('Content-Type', '').lower().startswith('application/json') and response.text.strip():
179+
result = response.json()
180+
self.__cache[hash_key] = result
181+
self.save_cache()
182+
return result
183+
else:
184+
self.logger.warning(f"Received empty or invalid JSON response for POST {self.base_url + url} (status: {response.status_code})")
185+
return []
168186

169187

170188
def articles_by_user_name(self, user_name, use_cache=True):
@@ -276,7 +294,13 @@ def _guess_doi(self, article):
276294
self.logger.debug(f"Querying Crossref for title: {title}")
277295
response = requests.get(base_url, params=params)
278296
response.raise_for_status()
279-
data = response.json()
297+
298+
# Check if response is valid and contains JSON
299+
if response.ok and response.headers.get('Content-Type', '').lower().startswith('application/json') and response.text.strip():
300+
data = response.json()
301+
else:
302+
self.logger.warning(f"Received empty or invalid JSON response from Crossref API (status: {response.status_code})")
303+
return None
280304

281305
if data["message"]["total-results"] == 0:
282306
self.logger.debug(f"No DOI found for: {title}")

0 commit comments

Comments
 (0)