def push_session(self, session: dict, name: str, entry_id: str = '', project_id: str = None): headers = { "Content-Type": "application/json" } data = { 'time_entry': { "description": name, "start": session['start_time'].isoformat() + "+00:00", "duration": int((session['end_time'] - session['start_time']).total_seconds()), "created_with": "thyme-toggl-cli" } } if project_id: data['time_entry']['pid'] = project_id if entry_id: return self.update_time_entry(entry_id, data) response = self.provider.request( 'time_entries', data=json.dumps(data), headers=headers, ) print(u'Pushed session to toggl: {}'.format(response)) entry = response['data'] entry['start_time'] = parse_time(entry['start']) entry['end_time'] = parse_time(entry['stop']) return entry
def update_time_entry(self, entry_id: str, data: dict): response = self.provider.request( 'time_entries/{}'.format(entry_id), data=json.dumps(data), method='PUT' ) print(u'Updated session to toggl: {}'.format(response)) entry = response['data'] entry['start_time'] = parse_time(entry['start']) entry['end_time'] = parse_time(entry['stop']) return entry
def create_entry(self, new_entry: Entry, issue: Optional[Issue]) -> Entry: entry = self.push_session(session={ 'start_time': new_entry.start_time, 'end_time': new_entry.end_time, }, name=new_entry.title, project_id=new_entry.project) return Entry( id=_str(entry['id']), start_time=parse_time(entry['start']), end_time=parse_time(entry['stop']), title=entry['description'], project=entry.get('pid', None), )
def _parse_snapshot_entry(self, entry): active_window = get_window(entry, entry['Active']) if active_window is None: return None return { 'active_window': active_window, 'time': parse_time(entry['Time']), 'category': 'work', }
def update_entry(self, entry_id: str, new_entry: Entry, issue: Optional[Issue]) -> Entry: updated_entry = self.push_session( session={ 'start_time': new_entry.start_time, 'end_time': new_entry.end_time, }, entry_id=entry_id, name=new_entry.title, project_id=new_entry.project ) return Entry( id=_str(updated_entry['id']), start_time=parse_time(updated_entry['start']), end_time=parse_time(updated_entry['stop']), title=updated_entry['description'], project=_str(updated_entry.get('pid', None)), group=new_entry.group, )
def get_entries(self) -> List[Entry]: if self.start_date: params = {'start_date': self.start_date.isoformat() + "+00:00", 'end_date': self.end_date.isoformat() + "+00:00"} else: params = {} data = self.provider.request( 'time_entries', params=params, method='GET' ) time_entries = [] for entry in data: time_entries.append(Entry( id=entry['id'], start_time=parse_time(entry['start']), end_time=parse_time(entry['stop']), title=entry.get('description', ''), project=entry.get('pid', None), )) return time_entries
def create_entry(self, new_entry: Entry, issue: Optional[Issue]) -> Entry: if not new_entry.project or new_entry.project == '0': new_entry.project = None if new_entry.end_time is None: raise ValueError("No end_time") entry = self.provider.workspace_request( 'time-entries', data=json.dumps({ 'start': new_entry.start_time.isoformat() + 'Z', 'end': new_entry.end_time.isoformat() + 'Z', 'description': new_entry.title, 'projectId': new_entry.project, }), method='POST' ) return Entry( id=_str(entry['id']), start_time=parse_time(entry['timeInterval']['start']), end_time=parse_time(entry['timeInterval']['end']), title=entry['description'], project=entry['projectId'], )
def get_entries(self) -> List[Entry]: if self.start_date: params = {'start': self.start_date.isoformat() + 'Z', 'end': self.end_date.isoformat() + 'Z'} else: params = {} time_entries = [] data = self.provider.workspace_request( 'user/{user_id}/time-entries'.format( user_id=self.user['id'] ), params=params, method='GET' ) for entry in data: time_entries.append(Entry( id=entry['id'], start_time=parse_time(entry['timeInterval']['start']), end_time=parse_time(entry['timeInterval']['end']), title=entry['description'], project=entry['projectId'], )) return time_entries
def test_request(self, endpoint: str, **kwargs) -> Union[List[dict], dict, str]: method = kwargs.get('method', 'POST').lower() if endpoint == "time_entries" and method == 'get': return [ { "id": "1", "pid": "10", "start": "2019-05-09T08:00:00+00:00", "stop": "2019-05-09T09:00:00+00:00", "description": "Toggl entry 1", }, { "id": "2", "pid": "11", "start": "2019-05-13T07:42:55+00:00", "stop": "2019-05-13T08:34:52+00:00", "description": "Toggl entry 2", }, { "id": "3", "pid": "20", "start": "2019-05-13T09:35:11+00:00", "stop": "2019-05-13T10:34:02+00:00", "description": "Toggl entry 3", } ] elif endpoint == "clients" and method == 'get': return [ { "id": "1", "name": "First Client", }, { "id": "2", "name": "Second Client", }, ] elif endpoint.startswith("clients") and method == 'get': _clid = endpoint[8] return [ { "id": str(int(_clid) * 10), "name": "Development" }, { "id": str(int(_clid) * 10 + 1), "name": "Bug fixing" }, ] elif endpoint == "time_entries" and method == 'post': entry = json.loads(kwargs['data'])['time_entry'] entry['stop'] = ( parse_time(entry['start']) + timedelta(seconds=entry['duration']) ).isoformat() + "+00:00" entry['id'] = self.id_counter self.id_counter += 1 return {'data': entry} elif endpoint.startswith("time_entries") and method == 'put': entry = json.loads(kwargs['data'])['time_entry'] entry['stop'] = ( parse_time(entry['start']) + timedelta(seconds=entry['duration']) ).isoformat() + "+00:00" entry['id'] = endpoint[13:] return {'data': entry} elif endpoint.startswith("time_entries") and method == 'delete': return endpoint[13:] return [{}]