From fcfab23c3d31f43cd75a3e58767ef09388c7446c Mon Sep 17 00:00:00 2001 From: Julien Maupetit Date: Wed, 7 Jun 2017 15:14:39 +0200 Subject: [PATCH] Fix watson sync given the new crick backend API --- tests/test_watson.py | 40 +++++++++++++++++++----------- watson/watson.py | 58 ++++++++++++++++---------------------------- 2 files changed, 47 insertions(+), 51 deletions(-) diff --git a/tests/test_watson.py b/tests/test_watson.py index 3312e5ff..c7560776 100644 --- a/tests/test_watson.py +++ b/tests/test_watson.py @@ -518,9 +518,9 @@ def test_push(mock, watson): watson.frames.add('bar', 3, 4) mock.patch.object(watson, '_get_remote_projects', return_value=[ - {'name': 'foo', 'url': '/projects/1/'}, - {'name': 'bar', 'url': '/projects/2/'}, - {'name': 'lol', 'url': '/projects/3/'}, + {'name': 'foo', 'id': '08288b71-4500-40dd-96b1-a995937a15fd'}, + {'name': 'bar', 'id': 'f0534272-65fa-4832-a49e-0eedf68b3a84'}, + {'name': 'lol', 'id': '7fdaf65e-66bd-4c01-b09e-74bdc8cbe552'}, ]) class Response: @@ -544,10 +544,10 @@ def __init__(self): frames_sent = json.loads(mock_put.call_args[0][1]) assert len(frames_sent) == 2 - assert frames_sent[0].get('project') == '/projects/2/' + assert frames_sent[0].get('project') == 'bar' assert frames_sent[0].get('tags') == ['A', 'B'] - assert frames_sent[1].get('project') == '/projects/3/' + assert frames_sent[1].get('project') == 'lol' assert frames_sent[1].get('tags') == [] @@ -589,11 +589,13 @@ def test_pull(mock, watson): watson.last_sync = arrow.now() - watson.frames.add('foo', 1, 2, ['A', 'B'], id='1') + watson.frames.add( + 'foo', 1, 2, ['A', 'B'], id='1c006c6e6cc14c80ab22b51c857c0b06' + ) mock.patch.object(watson, '_get_remote_projects', return_value=[ - {'name': 'foo', 'url': '/projects/1/'}, - {'name': 'bar', 'url': '/projects/2/'}, + {'name': 'foo', 'id': '08288b71-4500-40dd-96b1-a995937a15fd'}, + {'name': 'bar', 'id': 'f0534272-65fa-4832-a49e-0eedf68b3a84'}, ]) class Response: @@ -602,10 +604,20 @@ def __init__(self): def json(self): return [ - {'project': '/projects/1/', 'start': 3, 'stop': 4, 'id': '1', - 'tags': ['A']}, - {'project': '/projects/2/', 'start': 4, 'stop': 5, 'id': '2', - 'tags': []} + { + 'id': '1c006c6e-6cc1-4c80-ab22-b51c857c0b06', + 'project': 'foo', + 'start_at': 3, + 'end_at': 4, + 'tags': ['A'] + }, + { + 'id': 'c44aa815-4d77-4a58-bddd-1afa95562141', + 'project': 'bar', + 'start_at': 4, + 'end_at': 5, + 'tags': [] + } ] mock.patch('requests.get', return_value=Response()) @@ -624,13 +636,13 @@ def json(self): assert len(watson.frames) == 2 - assert watson.frames[0].id == '1' + assert watson.frames[0].id == '1c006c6e6cc14c80ab22b51c857c0b06' assert watson.frames[0].project == 'foo' assert watson.frames[0].start.timestamp == 3 assert watson.frames[0].stop.timestamp == 4 assert watson.frames[0].tags == ['A'] - assert watson.frames[1].id == '2' + assert watson.frames[1].id == 'c44aa8154d774a58bddd1afa95562141' assert watson.frames[1].project == 'bar' assert watson.frames[1].start.timestamp == 4 assert watson.frames[1].stop.timestamp == 5 diff --git a/watson/watson.py b/watson/watson.py index 0166bdfd..6b6a4bd8 100644 --- a/watson/watson.py +++ b/watson/watson.py @@ -1,9 +1,11 @@ # -*- coding: utf-8 -*- -import os -import json import datetime +import json import operator +import os +import uuid + from functools import reduce try: @@ -330,7 +332,7 @@ def _get_remote_projects(self): "server: {}".format(response.json()) ) - return self._remote_projects + return self._remote_projects['projects'] def pull(self): dest, headers = self._get_request_info('frames') @@ -351,20 +353,13 @@ def pull(self): frames = response.json() or () for frame in frames: - try: - # Try to find the project name, as the API returns an URL - project = next( - p['name'] for p in self._get_remote_projects() - if p['url'] == frame['project'] - ) - except StopIteration: - raise WatsonError( - "Received frame with invalid project from the server " - "(id: {})".format(frame['project']['id']) - ) - - self.frames[frame['id']] = (project, frame['start'], frame['stop'], - frame['tags']) + frame_id = uuid.UUID(frame['id']).hex + self.frames[frame_id] = ( + frame['project'], + frame['start_at'], + frame['end_at'], + frame['tags'] + ) return frames @@ -375,25 +370,11 @@ def push(self, last_pull): for frame in self.frames: if last_pull > frame.updated_at > self.last_sync: - try: - # Find the url of the project - project = next( - p['url'] for p in self._get_remote_projects() - if p['name'] == frame.project - ) - except StopIteration: - raise WatsonError( - "The project {} does not exists on the remote server, " - "please create it or edit the frame (id: {})".format( - frame.project, frame.id - ) - ) - frames.append({ - 'id': frame.id, - 'start': str(frame.start), - 'stop': str(frame.stop), - 'project': project, + 'id': uuid.UUID(frame.id).urn, + 'start_at': str(frame.start.to('utc')), + 'end_at': str(frame.stop.to('utc')), + 'project': frame.project, 'tags': frame.tags }) @@ -404,8 +385,11 @@ def push(self, last_pull): raise WatsonError("Unable to reach the server.") except AssertionError: raise WatsonError( - "An error occured with the remote " - "server: {}".format(response.json()) + "An error occured with the remote server (status: {}). " + "Response was:\n{}".format( + response.status_code, + response.text + ) ) return frames