def get_timer(user): res = auth(user.token).get('{}timetracking/business/{}/timers'.format( FRESHBOOKS_BASE_URL, user.business_id)).json() if len(res.get('timers')) == 0: return None log.debug(res) return Timer(res.get('timers')[0])
def get_recent_clients(user): res = auth(user.token).get( '{}accounting/account/{}/users/clients?page=1&per_page=5' '&search[vis_state]=0&sort=updated_desc'.format(FRESHBOOKS_BASE_URL, user.account_id) ).json() log.debug(res) clients = res['response']['result'].get('clients') if len(clients) == 0: return None return [Client(a) for a in clients]
def get_business(token): res = auth(token).get('{}{}'.format( FRESHBOOKS_BASE_URL, 'auth/api/v1/users/me')).json()['response'] business_list = [] for index, business_membership in enumerate(res['business_memberships'], start=1): business = business_membership['business'] business_list.append( (business['name'], business['id'], business['account_id'])) print('{}. {}'.format(index, business['name'])) selected = input('Which business would you like to sign into: ') return business_list[int(selected) - 1]
def pause_time_entry(user, timer): active_time_entry = timer.active_time_entry active_time_entry.update( is_logged=False, duration=(utcnow_aware() - parse_datetime_to_utc( active_time_entry.get('started_at'))).total_seconds(), timer={'id': timer.id}) time_entry = {'time_entry': active_time_entry} res = auth(user.token).put( '{}timetracking/business/{}/time_entries/{}'.format( FRESHBOOKS_BASE_URL, user.business_id, active_time_entry.get('id')), data=json.dumps(time_entry)) log.debug(res.text) res.raise_for_status() return res.json()
def update_time_entry(user, timer, client_id=None, internal_client=False): active_time_entry = timer.active_time_entry if client_id: active_time_entry['client_id'] = client_id elif internal_client: active_time_entry['client_id'] = None active_time_entry['internal'] = True time_entry = {'time_entry': active_time_entry} res = auth(user.token).put( '{}timetracking/business/{}/time_entries/{}'.format( FRESHBOOKS_BASE_URL, user.business_id, active_time_entry.get('id')), data=json.dumps(time_entry)) log.debug(res.text) res.raise_for_status() return res.json()
def create_new_time_entry(user, timer=None): time_entry = { 'time_entry': { 'is_logged': False, # 'note': 'Stuff', # 'client_id': '2149780',\ # 'project_id': '153125'\ 'started_at': zulu_time(datetime.utcnow()) } } if timer: time_entry['time_entry'].update(timer={'id': timer.id}) res = auth(user.token).post( '{}timetracking/business/{}/time_entries'.format( FRESHBOOKS_BASE_URL, user.business_id), data=json.dumps(time_entry)) log.debug(res.text) res.raise_for_status() return res.json()
def log_timer(user, timer): for entry in timer.raw_data.get('time_entries'): del entry['active'] del entry['identity_id'] del entry['created_at'] timer.raw_data.get('time_entries')[-1].update( duration=(utcnow_aware() - parse_datetime_to_utc( timer.raw_data.get('time_entries')[-1].get('started_at')) ).total_seconds(), timer={"id": timer.id}) res = auth(user.token).put('{}timetracking/business/{}/timers/{}'.format( FRESHBOOKS_BASE_URL, user.business_id, timer.id), data=json.dumps({ 'timer': { 'time_entries': timer.raw_data.get('time_entries') } })) log.debug(res.text) res.raise_for_status()
def delete_timer(user, timer): res = auth(user.token).delete( '{}timetracking/business/{}/timers/{}'.format(FRESHBOOKS_BASE_URL, user.business_id, timer.id)) res.raise_for_status()