From 62c089a09570a596ef69cb965cfaa931e1bf6db7 Mon Sep 17 00:00:00 2001 From: Andrew Williams Date: Fri, 24 Jun 2011 09:15:12 +0100 Subject: [PATCH] Better handle invalid returns from the API, and recall if we have no messages in the cache --- app/reddit/api.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/app/reddit/api.py b/app/reddit/api.py index 23e792d..ba3bd7d 100644 --- a/app/reddit/api.py +++ b/app/reddit/api.py @@ -108,12 +108,15 @@ class Inbox(): if not self.login_cookie: raise NotLoggedIn - if not hasattr(self, '__inbox_cache'): - inbox = json.load(self._opener.open(self.REDDIT_API_INBOX))['data'] - - self.__inbox_cache = [] - for msg in inbox['children']: - self.__inbox_cache.append(Message(msg['data'])) + if not hasattr(self, '__inbox_cache') or not len(self.__inbox_cache): + inbox = json.load(self._opener.open(self.REDDIT_API_INBOX)) + + if inbox and 'data' in inbox: + self.__inbox_cache = [] + for msg in inbox['data']['children']: + self.__inbox_cache.append(Message(msg['data'])) + else: + self.__inbox_cache = [] return self.__inbox_cache