Skip to content

Commit

Permalink
Merge pull request #152 from TailorDev/add-crick-sync
Browse files Browse the repository at this point in the history
Make the sync command works with the new Crick API
  • Loading branch information
willdurand authored Jun 9, 2017
2 parents 4145a62 + fcfab23 commit a747b2c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 51 deletions.
40 changes: 26 additions & 14 deletions tests/test_watson.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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') == []


Expand Down Expand Up @@ -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:
Expand All @@ -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())
Expand All @@ -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
Expand Down
58 changes: 21 additions & 37 deletions watson/watson.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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')
Expand All @@ -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

Expand All @@ -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
})

Expand All @@ -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
Expand Down

0 comments on commit a747b2c

Please sign in to comment.