Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit 666826a

Browse files
author
Mark Johnson
committed
Added version-safe method for getting json from responses
1 parent dccc02b commit 666826a

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

btsyncindicator.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def setup_session(self):
251251
params = {'token': self.token, 'action': a}
252252
response = requests.get(self.urlroot, params=params, cookies=self.cookies, auth=self.auth)
253253
response.raise_for_status()
254-
self.info[a] = response.json()
254+
self.info[a] = self.get_response_json(response)
255255

256256
self.clear_error()
257257

@@ -309,7 +309,7 @@ def check_status(self):
309309

310310
self.clear_error()
311311

312-
status = response.json()
312+
status = self.get_response_json(response)
313313

314314
for folder in status['folders']:
315315
folder['name'] = self.fix_encoding(folder['name'])
@@ -641,6 +641,17 @@ def get_response_text(self, response):
641641
"""
642642
return response.text if hasattr(response, "text") else response.content
643643

644+
def get_response_json(self, response):
645+
"""
646+
Version-safe way to parse json from request module response object
647+
The version in the Ubuntu 12.04 LTS repositories doesnt have .json()
648+
"""
649+
try:
650+
response_json = response.json()
651+
except AttributeError:
652+
response_json = json.loads(self.get_response_text(response))
653+
return response_json
654+
644655
def fix_encoding(self, text):
645656
return text.encode('latin-1').decode('utf-8')
646657

0 commit comments

Comments
 (0)