From 41a0d98f49bb8e11332ba6c9443f733ec6aeec2e Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Thu, 9 Nov 2017 09:07:23 -0800 Subject: [PATCH] upgrade wakatime-cli to v10.0.1 --- plugin/packages/wakatime/__about__.py | 2 +- plugin/packages/wakatime/api.py | 78 ++++++++++++++++++--------- plugin/packages/wakatime/main.py | 5 +- 3 files changed, 54 insertions(+), 31 deletions(-) diff --git a/plugin/packages/wakatime/__about__.py b/plugin/packages/wakatime/__about__.py index cb3a038..033ec45 100644 --- a/plugin/packages/wakatime/__about__.py +++ b/plugin/packages/wakatime/__about__.py @@ -1,7 +1,7 @@ __title__ = 'wakatime' __description__ = 'Common interface to the WakaTime api.' __url__ = 'https://github.com/wakatime/wakatime' -__version_info__ = ('10', '0', '0') +__version_info__ = ('10', '0', '1') __version__ = '.'.join(__version_info__) __author__ = 'Alan Hamlett' __author_email__ = 'alan@wakatime.com' diff --git a/plugin/packages/wakatime/api.py b/plugin/packages/wakatime/api.py index 7696e58..a520959 100644 --- a/plugin/packages/wakatime/api.py +++ b/plugin/packages/wakatime/api.py @@ -16,12 +16,7 @@ import sys import traceback from .compat import u, is_py3, json -from .constants import ( - API_ERROR, - AUTH_ERROR, - SUCCESS, - UNKNOWN_ERROR, -) +from .constants import API_ERROR, AUTH_ERROR, SUCCESS, UNKNOWN_ERROR from .offlinequeue import Queue from .packages.requests.exceptions import RequestException @@ -54,7 +49,7 @@ def send_heartbeats(heartbeats, args, configs, use_ntlm_proxy=False): api_url = args.api_url if not api_url: - api_url = 'https://api.wakatime.com/api/v1/heartbeats.bulk' + api_url = 'https://api.wakatime.com/api/v1/users/current/heartbeats.bulk' log.debug('Sending heartbeats to api at %s' % api_url) timeout = args.timeout if not timeout: @@ -143,35 +138,66 @@ def send_heartbeats(heartbeats, args, configs, use_ntlm_proxy=False): else: code = response.status_code 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 [] + except: + if log.isEnabledFor(logging.DEBUG): + log.traceback(logging.WARNING) + results = [] if code == requests.codes.created or code == requests.codes.accepted: log.debug({ 'response_code': code, }) + + for i in range(len(results)): + if len(heartbeats) <= i: + log.debug('Results from server do not match heartbeats sent.') + break + + try: + c = results[i][1] + except: + c = 0 + try: + text = json.dumps(results[i][0]) + except: + if log.isEnabledFor(logging.DEBUG): + log.traceback(logging.WARNING) + text = '' + handle_result([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: - if args.offline: - if code == 400: - log.error({ - 'response_code': code, - 'response_content': content, - }) - else: - if log.isEnabledFor(logging.DEBUG): - log.warn({ - 'response_code': code, - 'response_content': content, - }) - queue = Queue(args, configs) - queue.push_many(heartbeats) - else: - log.error({ - 'response_code': code, - 'response_content': content, - }) + 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): + if code == requests.codes.created or code == requests.codes.accepted: + return + + if args.offline: + if code == 400: + log.error({ + 'response_code': code, + 'response_content': content, + }) + else: + if log.isEnabledFor(logging.DEBUG): + log.warn({ + 'response_code': code, + 'response_content': content, + }) + queue = Queue(args, configs) + queue.push_many(h) + else: + log.error({ + 'response_code': code, + 'response_content': content, + }) diff --git a/plugin/packages/wakatime/main.py b/plugin/packages/wakatime/main.py index 351f606..70dadfc 100644 --- a/plugin/packages/wakatime/main.py +++ b/plugin/packages/wakatime/main.py @@ -24,10 +24,7 @@ from .__about__ import __version__ from .api import send_heartbeats from .arguments import parse_arguments from .compat import u, json -from .constants import ( - SUCCESS, - UNKNOWN_ERROR, -) +from .constants import SUCCESS, UNKNOWN_ERROR from .logger import setup_logging log = logging.getLogger('WakaTime')