refactor handling server responses

This commit is contained in:
Alan Hamlett 2017-11-22 14:01:02 -08:00
parent 019b97dfe7
commit aa58524f66
2 changed files with 47 additions and 34 deletions

View file

@ -137,6 +137,7 @@ class LoggingTestCase(utils.TestCase):
logfile = os.path.realpath(os.path.expanduser('~/.wakatime.log')) logfile = os.path.realpath(os.path.expanduser('~/.wakatime.log'))
self.assertEquals(logfile, logging.getLogger('WakaTime').handlers[0].baseFilename) self.assertEquals(logfile, logging.getLogger('WakaTime').handlers[0].baseFilename)
output = [u(' ').join(x) for x in logs.actual()] output = [u(' ').join(x) for x in logs.actual()]
expected = u('WakaTime WARNING Regex error (unbalanced parenthesis) for include pattern: \\(invalid regex)') expected = u('WakaTime WARNING Regex error (unbalanced parenthesis) for include pattern: \\(invalid regex)')
if self.isPy35OrNewer: if self.isPy35OrNewer:
expected = u('WakaTime WARNING Regex error (unbalanced parenthesis at position 15) for include pattern: \\(invalid regex)') expected = u('WakaTime WARNING Regex error (unbalanced parenthesis at position 15) for include pattern: \\(invalid regex)')
@ -147,7 +148,7 @@ class LoggingTestCase(utils.TestCase):
self.assertEquals(output[1], expected) self.assertEquals(output[1], expected)
self.assertEquals(output[2], u('WakaTime DEBUG Sending heartbeats to api at https://api.wakatime.com/api/v1/users/current/heartbeats.bulk')) self.assertEquals(output[2], u('WakaTime DEBUG Sending heartbeats to api at https://api.wakatime.com/api/v1/users/current/heartbeats.bulk'))
self.assertIn('Python', output[3]) self.assertIn('Python', output[3])
self.assertIn('response_code', output[5]) self.assertIn('response_code', output[4])
@log_capture() @log_capture()
def test_exception_traceback_logged_in_debug_mode(self, logs): def test_exception_traceback_logged_in_debug_mode(self, logs):

View file

@ -138,13 +138,23 @@ def send_heartbeats(heartbeats, args, configs, use_ntlm_proxy=False):
else: else:
code = response.status_code if response is not None else None code = response.status_code if response is not None else None
content = response.text if response is not None else None content = response.text if response is not None else None
try:
results = response.json() if response is not None else [] if _success(code):
except: results = _get_results(response)
if log.isEnabledFor(logging.DEBUG): _process_server_results(heartbeats, code, content, results, args, configs)
log.traceback(logging.WARNING) session_cache.save(session)
results = [] return SUCCESS
if code == requests.codes.created or code == requests.codes.accepted:
if should_try_ntlm:
return send_heartbeats(heartbeats, args, configs, use_ntlm_proxy=True)
_handle_unsent_heartbeats(heartbeats, code, content, args, configs)
session_cache.delete()
return AUTH_ERROR if code == 401 else API_ERROR
def _process_server_results(heartbeats, code, content, results, args, configs):
log.debug({ log.debug({
'response_code': code, 'response_code': code,
}) })
@ -164,24 +174,11 @@ def send_heartbeats(heartbeats, args, configs, use_ntlm_proxy=False):
if log.isEnabledFor(logging.DEBUG): if log.isEnabledFor(logging.DEBUG):
log.traceback(logging.WARNING) log.traceback(logging.WARNING)
text = '' text = ''
handle_result([heartbeats[i]], c, text, args, configs) if not _success(c):
_handle_unsent_heartbeats([heartbeats[i]], c, text, args, configs)
session_cache.save(session)
return SUCCESS
if should_try_ntlm:
return send_heartbeats(heartbeats, args, configs, use_ntlm_proxy=True)
else:
handle_result(heartbeats, code, content, args, configs)
session_cache.delete()
return AUTH_ERROR if code == 401 else API_ERROR
def handle_result(h, code, content, args, configs): def _handle_unsent_heartbeats(heartbeats, code, content, args, configs):
if code == requests.codes.created or code == requests.codes.accepted:
return
if args.offline: if args.offline:
if code == 400: if code == 400:
log.error({ log.error({
@ -195,9 +192,24 @@ def handle_result(h, code, content, args, configs):
'response_content': content, 'response_content': content,
}) })
queue = Queue(args, configs) queue = Queue(args, configs)
queue.push_many(h) queue.push_many(heartbeats)
else: else:
log.error({ log.error({
'response_code': code, 'response_code': code,
'response_content': content, 'response_content': content,
}) })
def _get_results(response):
results = []
if response is not None:
try:
results = response.json()
except:
if log.isEnabledFor(logging.DEBUG):
log.traceback(logging.WARNING)
return results
def _success(code):
return code == requests.codes.created or code == requests.codes.accepted