import urllib2 from framework.utils import notify try: import simplejson as json except: import json from etvnet.auth import Auth # in order to use this hendles_http_server_errors decorator # self suppose to have 'access_token' attribute def hendles_http_server_errors(request): def changed_request(self, *args, **kwargs): # if HTTP error with status 401 end responce error is invalid token, # then we need refresh the access token try: resp = request(self, *args, **kwargs) except urllib2.HTTPError, e: print e print e.code if e.code == 401 and json.loads(e.read())['error'] == 'invalid_token': auth = Auth() success, resp = auth.get_token(refresh=True) if success == auth.SUCCESS: self.access_token = auth.access_token return request(self, *args, **kwargs) else: self.close_window() notify(str(e)) return elif e.code not in [201, 204, 206, 404]: print "HELLO ERRORRRRRRRR" # temporary print error for user SERVER_ERROR_MESSAGE notify(str(e)) return e.code return e.code return resp return changed_request