Skip to content

Commit c11785f

Browse files
committed
refactor
1 parent 5d80627 commit c11785f

File tree

10 files changed

+107
-61
lines changed

10 files changed

+107
-61
lines changed

clockify_api_client/abstract_clockify.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,9 @@ def put(self, url, payload):
2727
if response.status_code in [200, 201, 202]:
2828
return response.json()
2929
raise Exception(response.json())
30+
31+
def delete(self, url):
32+
response = requests.delete(url, headers=self.header)
33+
if response.status_code in [200, 201, 202, 204]:
34+
return response.json()
35+
raise Exception(response.json())

clockify_api_client/client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ def __init__(self):
1717
self.reports = None
1818

1919
def build(self, api_key, api_url):
20-
"""Builds services from available factories."""
20+
"""Builds services from available factories.
21+
:param api_key Clockify API key.
22+
:param api_url Clockify API url.
23+
"""
2124

2225
self.workspaces = WorkspaceFactory(api_key=api_key, api_url=api_url)
2326
self.projects = ProjectFactory(api_key=api_key, api_url=api_url)

clockify_api_client/models/client.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from urllib.parse import urlencode
23

34
from clockify_api_client.abstract_clockify import AbstractClockify
@@ -9,13 +10,18 @@ def __init__(self, api_key, api_url):
910
super(Client, self).__init__(api_key=api_key, api_url=api_url)
1011

1112
def get_clients(self, workspace_id, params=None):
12-
"""Returns all unarchived clients.
13+
"""Returns all clients.
1314
:param workspace_id Id of workspace to look for clients.
1415
:param params URL params of request.
15-
:return List of clients(dict objects)."""
16-
if params is None:
17-
params = {}
18-
params['archived'] = str(False).lower()
19-
url_params = urlencode(params, doseq=True)
20-
url = self.base_url + '/workspaces/' + workspace_id + '/clients?archived=false&' + url_params
21-
return self.get(url)
16+
:return List of clients(dict objects).
17+
"""
18+
try:
19+
if params:
20+
url_params = urlencode(params, doseq=True)
21+
url = self.base_url + '/workspaces/' + workspace_id + '/clients&' + url_params
22+
else:
23+
url = self.base_url + '/workspaces/' + workspace_id + '/clients/'
24+
return self.get(url)
25+
except Exception as e:
26+
logging.error("API error: {0}".format(e))
27+
raise e

clockify_api_client/models/project.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ def __init__(self, api_key, api_url):
1010
super(Project, self).__init__(api_key=api_key, api_url=api_url)
1111

1212
def get_projects(self, workspace_id, params=None):
13-
"""Returns projects from given workspace with applied params if provided"""
13+
"""Returns projects from given workspace with applied params if provided.
14+
:param workspace_id Id of workspace.
15+
:param params Dictionary with request parameters.
16+
:return List of projects.
17+
"""
1418
try:
1519
if params:
1620
url_params = urlencode(params, doseq=True)
@@ -22,7 +26,14 @@ def get_projects(self, workspace_id, params=None):
2226
logging.error("API error: {0}".format(e))
2327
raise e
2428

25-
def create_new_project(self, workspace_id, project_name, client_id, billable=False):
29+
def add_project(self, workspace_id, project_name, client_id, billable=False):
30+
"""Add new project into workspace.
31+
:param workspace_id Id of workspace.
32+
:param project_name Name of new project.
33+
:param client_id Id of client.
34+
:param billable Bool flag.
35+
:return Dictionary representation of new project.
36+
"""
2637
try:
2738
url = self.base_url + '/workspaces/' + workspace_id + '/projects/'
2839
data = {

clockify_api_client/models/report.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ def get_summary_report(self, workspace_id, payload):
1515
:return Dictionary with summary report.
1616
"""
1717
try:
18-
url = self.base_url + '/workspaces/' + workspace_id + '/reports/summary'
18+
url = self.base_url + '/workspaces/' + workspace_id + '/reports/summary/'
1919
return self.post(url, payload)
20-
2120
except Exception as e:
2221
logging.error("API error: {0}".format(e))
2322
raise e
@@ -29,9 +28,8 @@ def get_detailed_report(self, workspace_id, payload):
2928
:return Dictionary with detailed report.
3029
"""
3130
try:
32-
url = self.base_url + '/workspaces/' + workspace_id + '/reports/detailed'
31+
url = self.base_url + '/workspaces/' + workspace_id + '/reports/detailed/'
3332
return self.post(url, payload)
34-
3533
except Exception as e:
3634
logging.error("API error: {0}".format(e))
3735
raise e
@@ -43,9 +41,8 @@ def get_weekly_report(self, workspace_id, payload):
4341
:return Dictionary with weekly report.
4442
"""
4543
try:
46-
url = self.base_url + '/workspaces/' + workspace_id + '/reports/weekly'
44+
url = self.base_url + '/workspaces/' + workspace_id + '/reports/weekly/'
4745
return self.post(url, payload)
48-
4946
except Exception as e:
5047
logging.error("API error: {0}".format(e))
5148
raise e

clockify_api_client/models/task.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,41 @@ class Task(AbstractClockify):
99
def __init__(self, api_key, api_url):
1010
super(Task, self).__init__(api_key=api_key, api_url=api_url)
1111

12-
def create_task(self, workspace_id, project_id, task_name, request_data=None):
13-
"""Creates new task in clockify.
12+
def add_task(self, workspace_id, project_id, task_name, request_data=None):
13+
"""Creates new task in Clockify.
1414
:param workspace_id Id of workspace.
1515
:param request_data Dictionary with request data.
1616
:param project_id Id of project.
1717
:param task_name Name of new task.
1818
:return Dictionary with task object representation.
1919
"""
2020
try:
21-
2221
url = self.base_url + '/workspaces/' + workspace_id + '/projects/' + project_id + '/tasks/'
23-
2422
payload = {'name': task_name, 'projectId': project_id}
2523
if request_data:
2624
payload = {**payload, **request_data}
27-
2825
return self.post(url, payload)
2926
except Exception as e:
3027
logging.error("API error: {0}".format(e))
3128
raise e
3229

3330
def update_task(self, workspace_id, project_id, task_id, request_data=None):
34-
"""Updates task in clockify.
31+
"""Updates task in Clockify.
3532
:param workspace_id Id of workspace.
3633
:param project_id Id of project.
3734
:param task_id Id of task.
3835
:param request_data Dictionary with request data.
3936
:return Dictionary with task object representation.
4037
"""
4138
try:
42-
4339
url = self.base_url + '/workspaces/' + workspace_id + '/projects/' + project_id + '/tasks/' + task_id
44-
4540
return self.put(url, request_data)
4641
except Exception as e:
4742
logging.error("API error: {0}".format(e))
4843
raise e
4944

5045
def get_tasks(self, workspace_id, project_id, params=None):
51-
"""Gets list of tasks from clockify.
46+
"""Gets list of tasks from Clockify.
5247
:param workspace_id Id of workspace.
5348
:param project_id Id of project.
5449
:param params Request URL query parameters.
@@ -67,10 +62,10 @@ def get_tasks(self, workspace_id, project_id, params=None):
6762
raise e
6863

6964
def get_task(self, workspace_id, project_id, task_id):
70-
"""Gets task from clockify.
65+
"""Gets task from Clockify.
7166
:param workspace_id Id of workspace.
7267
:param project_id Id of project.
73-
:param task_id Request URL query parameters.
68+
:param task_id Request URL query parameters.
7469
:return List with dictionaries with task object representation.
7570
"""
7671
try:

clockify_api_client/models/time_entry.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,40 @@ class TimeEntry(AbstractClockify):
99
def __init__(self, api_key, api_url):
1010
super(TimeEntry, self).__init__(api_key=api_key, api_url=api_url)
1111

12-
def get_user_time_entries(self, workspace_id, user_id, params=None):
13-
"""Returns User time entries.
12+
def get_time_entries(self, workspace_id, user_id, params=None):
13+
"""Returns user time entries.
1414
:param workspace_id Id of workspace.
1515
:param user_id Id of user.
1616
:param params Request URL query params.
1717
:return List with dictionary representation of time entries from clockify.
1818
"""
1919
try:
20-
base_url = self.base_url.strip('v1/')
2120
if params:
2221
url_params = urlencode(params, doseq=True)
23-
url = base_url + '/workspaces/' + workspace_id + '/timeEntries/user/' + user_id + '?' + url_params
22+
url = self.base_url + '/workspaces/' + workspace_id + '/user/' + user_id + '/time-entries?' + url_params
2423
else:
25-
url = base_url + '/workspaces/' + workspace_id + '/timeEntries/user/' + user_id + '/'
24+
url = self.base_url + '/workspaces/' + workspace_id + '/user/' + user_id + '/time-entries/'
2625
time_entries_list = self.get(url)
2726
return time_entries_list
2827
except Exception as e:
2928
logging.error("API error: {0}".format(e))
3029
raise e
3130

31+
def get_time_entry(self, workspace_id, time_entry_id):
32+
"""Gets specific time entry.
33+
:param workspace_id Id of workspace.
34+
:param time_entry_id Id of time entry
35+
:return Dictionary representation of time entry.
36+
"""
37+
try:
38+
url = self.base_url + '/workspaces/' + workspace_id + '/time-entries/' + time_entry_id
39+
return self.get(url)
40+
except Exception as e:
41+
logging.error("API error: {0}".format(e))
42+
raise e
43+
3244
def update_time_entry(self, workspace_id, entry_id, payload):
33-
"""Updates time entry in clockify with provided payload data.
45+
"""Updates time entry in Clockify with provided payload data.
3446
:param workspace_id Id of workspace.
3547
:param entry_id Id of time entry.
3648
:param payload Dictionary with payload data for update.
@@ -44,15 +56,16 @@ def update_time_entry(self, workspace_id, entry_id, payload):
4456
logging.error("API error: {0}".format(e))
4557
raise e
4658

47-
def add_new_time_entry(self, workspace_id, user_id, payload):
48-
"""Adds time entry in clockify with provided payload data.
59+
def add_time_entry(self, workspace_id, user_id, payload):
60+
"""Adds time entry in Clockify with provided payload data.
61+
Paid feature, workspace need to have active paid subscription.
4962
:param workspace_id Id of workspace.
5063
:param user_id Id of workspace.
5164
:param payload Dictionary with payload data for update.
5265
:return Updated time entry.
5366
"""
5467
try:
55-
url = self.base_url + '/workspaces/' + workspace_id + '/user/' + user_id + '/time-entries'
68+
url = self.base_url + '/workspaces/' + workspace_id + '/user/' + user_id + '/time-entries/'
5669
time_entry = self.post(url, payload)
5770
return time_entry
5871
except Exception as e:

clockify_api_client/models/user.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,23 @@ class User(AbstractClockify):
99
def __init__(self, api_key, api_url):
1010
super(User, self).__init__(api_key=api_key, api_url=api_url)
1111

12-
def get_user(self, user_id):
13-
"""Get user by ID.
14-
:param user_id ID of user.
15-
:return User dictionary representation."""
12+
def get_current_user(self):
13+
"""Get user by paired with API key.
14+
:return User dictionary representation.
15+
"""
1616
try:
17-
url = self.base_url + '/users/' + str(user_id)
17+
url = self.base_url + '/user/'
1818
return self.get(url)
1919
except Exception as e:
2020
logging.error("API error: {0}".format(e))
2121
raise e
2222

23-
def get_all_workspace_users(self, workspace_id, params=None):
23+
def get_users(self, workspace_id, params=None):
2424
"""Returns list of all users in given workspace.
2525
:param workspace_id Id of workspace.
2626
:param params Request URL query params.
27-
:return List of Users dictionary representation."""
27+
:return List of Users dictionary representation.
28+
"""
2829
try:
2930
if params:
3031
params = urlencode(params, doseq=True)
@@ -36,17 +37,43 @@ def get_all_workspace_users(self, workspace_id, params=None):
3637
logging.error("API error: {0}".format(e))
3738
raise e
3839

39-
def add_new_user(self, workspace_id, email):
40+
def add_user(self, workspace_id, email):
4041
"""Adds new user into workspace.
4142
:param workspace_id Id of workspace.
4243
:param email Email of new user.
4344
:return Dictionary representation of user."""
4445
try:
45-
url = self.base_url + '/workspaces/' + workspace_id + '/users'
46+
url = self.base_url + '/workspaces/' + workspace_id + '/users/'
4647
emails = list()
4748
emails.append(email)
4849
data = {'emails': emails}
4950
return self.post(url, data)
5051
except Exception as e:
5152
logging.error("API error: {0}".format(e))
5253
raise e
54+
55+
def update_user(self, workspace_id, user_id, payload):
56+
"""Adds new user into workspace.
57+
:param workspace_id Id of workspace.
58+
:param user_id User Id.
59+
:param payload User data to update.
60+
:return Dictionary representation of user.
61+
"""
62+
try:
63+
url = self.base_url + '/workspaces/' + workspace_id + '/users/' + user_id
64+
return self.put(url, payload)
65+
except Exception as e:
66+
logging.error("API error: {0}".format(e))
67+
raise e
68+
69+
def remove_user(self, workspace_id, user_id):
70+
"""Removes user from workspace.
71+
:param workspace_id Id of workspace.
72+
:param user_id User Id.
73+
"""
74+
try:
75+
url = self.base_url + '/workspaces/' + workspace_id + '/users/' + user_id
76+
return self.delete(url)
77+
except Exception as e:
78+
logging.error("API error: {0}".format(e))
79+
raise e

clockify_api_client/models/workspace.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,11 @@ def __init__(self, api_key, api_url):
1010

1111
def get_workspaces(self):
1212
"""Returns all workspaces.
13-
:return List of Workspaces in dictionary representation."""
13+
:return List of Workspaces in dictionary representation.
14+
"""
1415
try:
1516
url = self.base_url + '/workspaces/'
1617
return self.get(url)
17-
18-
except Exception as e:
19-
logging.error("API error: {0}".format(e))
20-
raise e
21-
22-
def create_new_workspace(self, name):
23-
"""Creates new workspace with given name.
24-
:return Workspace dictionary representation."""
25-
try:
26-
url = self.base_url + '/workspaces/'
27-
data = {'name': name}
28-
return self.post(url, data)
29-
3018
except Exception as e:
3119
logging.error("API error: {0}".format(e))
3220
raise e

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name='clockify-api-client',
8-
version='0.0.11',
8+
version='0.1.0',
99
author="Michael Bláha",
1010
author_email="[email protected]",
1111
description="Simple python API client for clockify. Inspired by https://pypi.org/project/clockify/library.",

0 commit comments

Comments
 (0)