@@ -54,12 +54,12 @@ def shorten(self, doi):
5454 url = 'http://shortdoi.org/{}?format=json' .format (quoted_doi )
5555 try :
5656 response = requests .get (url )
57- # Check if response is valid JSON
58- if response .headers .get ('Content-Type' , '' ).startswith ('application/json' ) and response .text .strip ():
57+ # Check if response is valid and contains JSON
58+ if response .ok and response . headers .get ('Content-Type' , '' ).startswith ('application/json' ) and response .text .strip ():
5959 result = response .json ()
6060 short_doi = result ['ShortDOI' ]
6161 else :
62- self .logger .warning (f"Received empty or invalid JSON response for { doi } from { url } " )
62+ self .logger .warning (f"Received empty or invalid JSON response for { doi } from { url } (status: { response . status_code } ) " )
6363 return None
6464 except Exception as e :
6565 self .logger .warning (f"failed to get short doi for { doi } : { e } " )
@@ -157,14 +157,14 @@ def __get(self, url, params=None, use_cache=True):
157157 else :
158158 headers = { "Authorization" : "token " + self .token } if self .token else {}
159159 response = get (self .base_url + url , headers = headers , params = params )
160- # Check if response is valid JSON
161- if response .headers .get ('Content-Type' , '' ).startswith ('application/json' ) and response .text .strip ():
160+ # Check if response is valid and contains JSON
161+ if response .ok and response . headers .get ('Content-Type' , '' ).startswith ('application/json' ) and response .text .strip ():
162162 result = response .json ()
163163 self .__cache [hash_key ] = result
164164 self .save_cache ()
165165 return result
166166 else :
167- self .logger .warning (f"Received empty or invalid JSON response for GET { self .base_url + url } " )
167+ self .logger .warning (f"Received empty or invalid JSON response for GET { self .base_url + url } (status: { response . status_code } ) " )
168168 return {}
169169
170170 def __post (self , url , params = None , use_cache = True ):
@@ -174,14 +174,14 @@ def __post(self, url, params=None, use_cache=True):
174174 else :
175175 headers = { "Authorization" : "token " + self .token } if self .token else {}
176176 response = post (self .base_url + url , headers = headers , json = params )
177- # Check if response is valid JSON
178- if response .headers .get ('Content-Type' , '' ).startswith ('application/json' ) and response .text .strip ():
177+ # Check if response is valid and contains JSON
178+ if response .ok and response . headers .get ('Content-Type' , '' ).startswith ('application/json' ) and response .text .strip ():
179179 result = response .json ()
180180 self .__cache [hash_key ] = result
181181 self .save_cache ()
182182 return result
183183 else :
184- self .logger .warning (f"Received empty or invalid JSON response for POST { self .base_url + url } " )
184+ self .logger .warning (f"Received empty or invalid JSON response for POST { self .base_url + url } (status: { response . status_code } ) " )
185185 return []
186186
187187
0 commit comments