Skip to content

Commit a10118e

Browse files
Updated client initialization.
1 parent 9be382c commit a10118e

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ This is a Tempo API client library to simplify the interaction with Tempo timesh
1010
from tempoapiclient import client
1111
1212
tempo = client.Tempo(
13-
"https://api.tempo.io/core/3",
14-
"<your_tempo_api_key>")
13+
auth_token="<your_tempo_api_key>",
14+
base_url="https://api.tempo.io/core/3")
1515
1616
worklogs = tempo.get_worklogs("2019-11-10", "2019-11-11")
1717
@@ -22,6 +22,7 @@ for i in worklogs:
2222
## Changelog
2323

2424
- 2019-12-17: Initial version with /worklogs, /work-attributes and /user-schedule
25+
- 2019-12-18: Updated client initialization, internal code reorganization
2526

2627
## Contributing
2728

tempoapiclient/client.py

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,18 @@
2424

2525

2626
class Tempo(object):
27+
"""
28+
Basic Client for accessing Tempo Rest API as provided by api.tempo.io.
29+
"""
30+
2731
# Maximum number of result in single response (pagination)
2832
# NOTE: maximum number allowed by API is 1000
2933
MAX_RESULTS = 1000
3034

31-
def __init__(self, base_url, auth_token):
35+
def __init__(self, auth_token, base_url="https://api.tempo.io/core/3"):
3236
self._token = auth_token
3337
self.BASE_URL = base_url
38+
self.work_attributes = None
3439

3540
def _resolve_date(self, value):
3641
if isinstance(value, datetime):
@@ -66,41 +71,57 @@ def _list(self, url, **params):
6671
if "next" not in metadata or metadata.get("count", 0) < metadata.get("limit", 0):
6772
break
6873

69-
def _get_work_attributes(self):
74+
75+
def get_work_attributes(self):
76+
"""
77+
Returns work attributeslogs inside ```date_from``` and ```date_to```,
78+
for particular ```user```, adding work attributes if ```add_work_attributes```.
79+
"""
7080
return { x["key"]: x for x in self._list("/work-attributes") }
7181

72-
def _iterate_worklogs(self, date_from, date_to, user=None):
82+
83+
def get_worklogs(self, date_from, date_to, user=None, add_work_attributes=False):
84+
"""
85+
Returns worklogs inside ```date_from``` and ```date_to```,
86+
for particular ```user```, adding work attributes if ```add_work_attributes```.
87+
"""
88+
7389
work_attributes = None
7490
date_from = self._resolve_date(date_from).isoformat()
7591
date_to = self._resolve_date(date_to).isoformat()
7692
url = "/worklogs"
7793
if user is not None:
7894
url += "/user/{}".format(user)
7995
params = { "from": date_from, "to": date_to, "limit": self.MAX_RESULTS }
80-
for worklog in self._list(url, **params):
81-
attributes = (worklog.get("attributes") or {}).get("values") or []
96+
l = self._list(url, **params)
97+
98+
# restructure workattributes
99+
100+
if not self.work_attributes:
101+
self.work_attributes = self.get_work_attributes()
102+
103+
for worklog in l:
104+
attributes = (worklog.get("attributes") or {}).get("values") or {}
82105
resolved_attributes = {}
83-
if attributes:
84-
if work_attributes is None:
85-
work_attributes = self._get_work_attributes()
86-
for attribute in attributes:
87-
key = attribute["key"]
88-
name = work_attributes.get(key, {}).get("name", key)
89-
resolved_attributes[name] = attribute["value"]
90-
worklog["attributes"] = resolved_attributes
106+
107+
for attribute in attributes:
108+
key = attribute["key"]
109+
name = self.work_attributes.get(key, {}).get("name", key)
110+
resolved_attributes[name] = attribute["value"]
111+
112+
worklog["attributes"] = resolved_attributes
91113
yield worklog
92114

93-
def get_worklogs(self, date_from, date_to, user=None):
94-
return list(self._iterate_worklogs(date_from, date_to, user))
95115

96-
def _iterate_user_schedule(self, date_from, date_to, user):
116+
def get_user_schedule(self, date_from, date_to, user=None):
117+
"""
118+
Returns user schedule inside ```date_from``` and ```date_to```,
119+
for particular ```user```.
120+
"""
97121
date_from = self._resolve_date(date_from).isoformat()
98122
date_to = self._resolve_date(date_to).isoformat()
99123
url = "/user-schedule"
100124
if user is not None:
101125
url += "/{}".format(user)
102126
params = { "from": date_from, "to": date_to, "limit": self.MAX_RESULTS }
103-
return self._list(url, **params)
104-
105-
def get_user_schedule(self, date_from, date_to, user):
106-
return list(self.iterate_schedule(date_from, date_to, user))
127+
return list(self._list(url, **params))

0 commit comments

Comments
 (0)