-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy_atimelogger.py
545 lines (473 loc) · 19.8 KB
/
py_atimelogger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
import re
import html
from datetime import datetime, tzinfo
from warnings import warn
from typing import (
Any,
SupportsInt,
Iterable,
Mapping,
Callable,
Optional,
TypeAlias,
Literal,
)
import requests
records: TypeAlias = dict[str, Any]
def prepare_timestamp(time: str | datetime | SupportsInt) -> int:
"""
Converts the given time value to a timestamp.
Args:
time (str | datetime | SupportsInt): The time value to be converted.
Returns:
int: The converted timestamp.
Raises:
TypeError: If the type of time is not supported.
"""
if isinstance(time, (str, datetime)):
if isinstance(time, str):
dt = datetime.fromisoformat(time)
elif isinstance(time, datetime):
dt = time
ts = dt.timestamp()
elif isinstance(time, SupportsInt):
ts = time
else:
raise TypeError(f"unsupported type for time: {type(time)}")
return int(ts)
def timestamp_helper(
dt: datetime | str,
tz: Optional[tzinfo] = None
) -> tuple[datetime, Callable[[float], datetime]]:
"""
Convert the given datetime to a datetime with timezone and a converter function.
Args:
dt (datetime | str): The datetime to convert.
tz (tzinfo, optional): The timezone to use. Defaults to None, resulting in local timezone.
Returns:
A tuple containing 2 elements:
datetime: The converted datetime with timezone.
Callable[[float], datetime]: The converter function to convert timestamps to datetime.
"""
if isinstance(dt, str):
dt = datetime.fromisoformat(dt)
dt = dt.replace(tzinfo=tz)
converter: Callable[[float], datetime] = lambda ts: datetime.fromtimestamp(ts, tz)
return dt, converter
class aTimeLogger:
"""
A Python wrapper for aTimeLogger REST-API.
aTimeLogger API info: http://blog.timetrack.io/rest-api
aTimeLogger API endpoint: https://app.atimelogger.com/api/v2
Parameters:
----------
- username: (bytes | str) The username of aTimeLogger account.
- password: (bytes | str) The password of aTimeLogger account.
Attributes:
----------
- _username: (bytes | str) The username of aTimeLogger account.
- _password: (bytes | str) The password of aTimeLogger account.
- session: (requests.Session) The session object for the API requests.
- auth_header: (requests.auth.HTTPBasicAuth) The authentication header for the request.
Notes:
----------
The datetime without time zone is assumed to be in local time zone.
The time-limited step is `curl_request`.
"""
LIMIT_MAX = 0x7FFF_FFFF
MODELS = (
'intervals',
'types',
'activities',
'goals',
'statistics',
)
ENDPOINT = 'https://app.atimelogger.com/api/v2'
def __init__(
self,
username: bytes | str,
password: bytes | str,
):
self._username = username
self._password = password
self.session = requests.Session()
self.session.auth = self.auth_header = self.username, self.password
@property
def username(self):
return self._username
@property
def password(self):
return self._password
def __enter__(self):
return self
def __exit__(self, *exc):
self.close()
def close(self):
self.session.close()
def __repr__(self) -> str:
return f"{self.__class__.__name__}({self._username}, {self._password})"
def __str__(self) -> str:
return f"aTimeLogger user {self._username}"
def prepare_params(
self,
offset: int = 0,
limit: int = 100,
order: Literal['desc', 'asc'] = 'desc',
datetime_range: tuple[Optional[str | datetime | SupportsInt], Optional[str | datetime | SupportsInt]] = (None, None),
types: Optional[Iterable[str]] = None,
state: Optional[str] = None,
**kwargs,
) -> dict:
"""
Prepare the parameters for the API request.
Args:
offset (int): The offset value for pagination. Defaults to 0.
limit (int): The limit value for pagination. Defaults to 100.
order (Literal['desc', 'asc']): The order of the results. Defaults to 'desc'.
datetime_range (tuple[str | datetime | SupportsInt | None, str | datetime | SupportsInt | None]): \
The datetime range in which intervals are included. None means no limit. Default is (None, None).
types (Iterable[str], optional): The type guids of the intervals to retrieve. Default is None, meaning all types.
state (str, optional): The state of the activities to include. Defaults to None, meaning all states.
**kwargs: Additional parameters for the API request.
Returns:
dict: The prepared parameters for the API request.
"""
dt_lower_bound, dt_upper_bound = datetime_range
params = {
'limit': limit,
'offset': offset,
'order': order,
}
if dt_lower_bound:
params['from'] = prepare_timestamp(dt_lower_bound)
if dt_upper_bound:
params['to'] = prepare_timestamp(dt_upper_bound)
if types:
params['types'] = ','.join(types)
if state:
params['state'] = state
params.update(kwargs)
return params
def request(
self,
method: str,
model: str,
guid: str = '',
params: Optional[Mapping] = None,
json: Optional[Any] = None,
**kwargs,
) -> requests.Response:
"""
Send a request to the aTimeLogger API.
Args:
method (str): The HTTP method to use.
model (str): The model to request.
guid (str): The guid of the model to request. Defaults to ''.
params (Mapping, optional): The parameters for the request. Defaults to None.
json (Any, optional): The JSON data for the request. Defaults to None.
**kwargs: Additional keyword arguments for the request.
Returns:
requests.Response: The response object from the request.
"""
url = f"{self.ENDPOINT}/{model}/{guid}"
response = self.session.request(
method,
url,
params=params,
json=json,
**kwargs
)
return response
def check_response(self, response: requests.Response) -> None:
"""
Check the response status code and raise an `HTTPError` if it indicates an error.
Args:
response (requests.Response): The response object to check.
Raises:
`HTTPError`: If the response status code indicates an error.
"""
if 400 <= response.status_code < 600:
request_info = f"{response.request.method} {response.request.url}"
try:
title = re.search(
r"<title>(.*)</title>",
response.text,
re.IGNORECASE
).group(1)
reasons_match = re.search(
r"<p><b>Message<\/b> (.*?)<\/p>",
response.text,
re.IGNORECASE
)
reasons = html.unescape(reasons_match.group(1)) if reasons_match else ''
details_match = re.search(
r"<p><b>Description<\/b> (.*?)<\/p>",
response.text,
re.IGNORECASE
)
details = html.unescape(details_match.group(1)) if details_match else ''
error_msg = f"{title}: {reasons} for {request_info}.\n{details}"
except AttributeError:
error_type = ("Client Error" if response.status_code < 500
else "Server Error")
try:
error_msg = (f"{response.status_code} {error_type}: for {request_info}.\n{response.json()}")
except requests.exceptions.JSONDecodeError:
error_msg = f"{response.status_code} {error_type}: for {request_info}.\n{response.text}"
raise requests.HTTPError(error_msg, response=response)
def _object_hook(self, dct: dict, tz: Optional[tzinfo] = None) -> dict:
if 'from' in dct:
dct['from'] = datetime.fromtimestamp(dct['from'], tz)
if 'to' in dct:
dct['to'] = datetime.fromtimestamp(dct['to'], tz)
if ('comment' in dct) and dct['comment'] == '':
dct['comment'] = None
if 'type' in dct:
dct['typeGuid'] = dct.pop('type')['guid']
return dct
def decode_response(
self,
response: requests.Response,
**kwargs,
) -> dict[str, Any]:
"""
Decode the response from the API and return it as a dictionary.
Args:
response (requests.Response): The response object from the API.
**kwargs: Additional keyword arguments that `json.loads` takes.
Returns:
dict[str, Any]: The decoded response as a dictionary.
"""
decoded_text = response.json(**kwargs)
if ('' in decoded_text) and not decoded_text['']:
del decoded_text['']
return decoded_text
def get_types(
self,
guid: str = '',
order: Literal['desc', 'asc'] = 'desc',
**kwargs,
) -> dict[str, list[records] | bool]:
"""
Retrieve a dict containing a list of types with optional filtering and pagination.
Args:
guid (str): The GUID of the type to retrieve. Default is ''.
order (Literal['desc', 'asc']): The order in which the types should be sorted. Default is 'desc'.
**kwargs: Additional keyword arguments for the request.
Returns:
dict[str, list[records] | bool]:
"types" (list[records]): A list of dictionaries (with string keys) representing the types.
"success" (bool): A boolean indicating the success of the porcess in server.
Raises:
Exception: If the API response is not successful.
"""
params = self.prepare_params(order=order)
del params['limit']
del params['offset']
response = self.request(
method='get',
model='types',
guid=guid,
params=params,
**kwargs
)
self.check_response(response)
return self.decode_response(response)
def get_activities(
self,
offset: int = 0,
limit: int = LIMIT_MAX,
state: Optional[str] = None,
order: Literal['desc', 'asc'] = 'desc',
**kwargs,
) -> dict[str, list[records] | dict[str, str | int] | list[str] | int]:
"""
Retrieve a dict containing a list of activities with optional filtering and pagination.
Args:
offset (int): The offset for pagination. Default is 0.
limit (int): The maximum number of activities to retrieve.\
Default is 2147483647:=0x7FFF_FFFF (`LIMIT_MAX`).
state (str, optional): The state of the activities to retrieve. Default is None.
order (Literal['desc', 'asc']): The order in which the activities should be sorted. Default is 'desc'.
**kwargs: Additional keyword arguments for the request.
Returns:
dict[str, list[records] | dict[str, str | int] | list[str] | int]:
"activities" (list[records]): A list of dictionaries (with string keys) representing the activities.
"types" (list[records]): A list of dictionaries (with string keys) representing the types.
"account" (dict[str, str | int]): A dictionary representing the account.
"guid" (list[str]): A list of GUIDs.
"revision" (int): The revision number.
Raises:
Exception: If the API response is not successful.
"""
params = self.prepare_params(
offset=offset,
limit=limit,
state=state,
order=order
)
response = self.request(
method='get',
model='activities',
params=params,
**kwargs
)
self.check_response(response)
return self.decode_response(response)
def _extract_tzinfo_4decode(
self,
datetime_range: tuple[Optional[str | datetime | SupportsInt], Optional[str | datetime | SupportsInt]]
) -> tzinfo | None:
dt_lower_bound, dt_upper_bound = datetime_range
if isinstance(dt_lower_bound, str):
tz1 = datetime.fromisoformat(dt_lower_bound).tzinfo
elif isinstance(dt_lower_bound, datetime):
tz1 = dt_lower_bound.tzinfo
else:
tz1 = None
if isinstance(dt_upper_bound, str):
tz2 = datetime.fromisoformat(dt_upper_bound).tzinfo
elif isinstance(dt_upper_bound, datetime):
tz2 = dt_upper_bound.tzinfo
else:
tz2 = None
if tz1 and tz2:
if tz1 != tz2:
warn("The timezones of the datetime range are different. Using the timezone of the lower bound.")
return tz1
else:
return tz1 or tz2
def get_intervals(
self,
offset: int = 0,
limit: int = LIMIT_MAX,
datetime_range: tuple[Optional[str | datetime | SupportsInt], Optional[str | datetime | SupportsInt]] = (None, None),
types: Optional[Iterable[str]] = None,
order: Literal['desc', 'asc'] = 'desc',
**kwargs,
) -> dict[str, list[records] | dict[str, int]]:
"""
Retrieve a dict containing a list of intervals with optional filtering and pagination.
Args:
offset (int): The offset for pagination. Default is 0.
limit (int): The maximum number of intervals to retrieve.\
Default is 2147483647:=0x7FFF_FFFF (`LIMIT_MAX`).
datetime_range (tuple[str | datetime | SupportsInt | None, str | datetime | SupportsInt | None]): \
The datetime range in which intervals are included. None means no limit. Default is (None, None).
types (Iterable[str], optional): The type guids of the intervals to retrieve. Default is None.
order (Literal['desc', 'asc']): The order in which the intervals should be sorted. Default is 'desc'.
**kwargs: Additional keyword arguments for the request.
Returns:
dict[str, list[records] | dict[str, int]]:
"intervals" (list[records]): A list of dictionaries (with string keys) representing the intervals.
"meta" (dict[str, int]): A dictionary representing the meta information.
Raises:
Exception: If the API response is not successful.
"""
params = self.prepare_params(
offset=offset,
limit=limit,
datetime_range=datetime_range,
types=types,
order=order
)
response = self.request(
method='get',
model='intervals',
params=params,
**kwargs
)
self.check_response(response)
tz = self._extract_tzinfo_4decode(datetime_range)
return self.decode_response(
response,
object_hook=lambda dct: self._object_hook(dct, tz=tz)
)
def get_types(
username: bytes | str,
password: bytes | str,
guid: str = '',
order: Literal['desc', 'asc'] = 'desc',
**kwargs,
) -> dict[str, list[records] | bool]:
"""
Retrieve a dict containing a list of types with optional filtering and pagination.
Args:
username (bytes | str): The username of aTimeLogger account.
password (bytes | str): The password of aTimeLogger account.
guid (str): The GUID of the type to retrieve. Default is an empty string.
order (Literal['desc', 'asc']): The order in which the types should be sorted. Default is 'desc'.
**kwargs: Additional keyword arguments for the request.
Returns:
dict[str, list[records] | bool]:
"types" (list[records]): A list of dictionaries (with string keys) representing the types.
"success" (bool): A boolean indicating the success of the porcess in server.
Raises:
Exception: If the API response is not successful.
"""
with aTimeLogger(username, password) as atl:
return atl.get_types(guid, order, **kwargs)
def get_activities(
username: bytes | str,
password: bytes | str,
offset: int = 0,
limit: int = aTimeLogger.LIMIT_MAX,
state: Optional[str] = None,
order: Literal['desc', 'asc'] = 'desc',
**kwargs,
) -> dict[str, list[records] | dict[str, str | int] | list[str] | int]:
"""
Retrieve a dict containing a list of activities with optional filtering and pagination.
Args:
username (bytes | str): The username of aTimeLogger account.
password (bytes | str): The password of aTimeLogger account.
offset (int): The offset for pagination. Default is 0.
limit (int): The maximum number of activities to retrieve.\
Default is 2147483647:=0x7FFF_FFFF (`LIMIT_MAX`).
state (str, optional): The state of the activities to retrieve. Default is None.
order (Literal['desc', 'asc']): The order in which the activities should be sorted. Default is 'desc'.
**kwargs: Additional keyword arguments for the request.
Returns:
dict[str, list[records] | dict[str, str | int] | list[str] | int]:
"activities" (list[records]): A list of dictionaries (with string keys) representing the activities.
"types" (list[records]): A list of dictionaries (with string keys) representing the types.
"account" (dict[str, str | int]): A dictionary representing the account.
"guid" (list[str]): A list of GUIDs.
"revision" (int): The revision number.
Raises:
Exception: If the API response is not successful.
"""
with aTimeLogger(username, password) as atl:
return atl.get_activities(offset, limit, state, order, **kwargs)
def get_intervals(
username: bytes | str,
password: bytes | str,
offset: int = 0,
limit: int = aTimeLogger.LIMIT_MAX,
datetime_range: tuple[Optional[str | datetime | SupportsInt], Optional[str | datetime | SupportsInt]] = (None, None),
types: Optional[Iterable[str]] = None,
order: Literal['desc', 'asc'] = 'desc',
**kwargs,
) -> dict[str, list[records] | dict[str, int]]:
"""
Retrieve a dict containing a list of intervals with optional filtering and pagination.
Args:
username (bytes | str): The username of aTimeLogger account.
password (bytes | str): The password of aTimeLogger account.
offset (int): The offset for pagination. Default is 0.
limit (int): The maximum number of intervals to retrieve.\
Default is 2147483647:=0x7FFF_FFFF (`LIMIT_MAX`).
datetime_range (tuple[str | datetime | SupportsInt | None, str | datetime | SupportsInt | None]): \
The datetime range in which intervals are included. None means no limit. Default is (None, None).
types (Iterable[str], optional): The type guids of the intervals to retrieve. Default is None.
order (Literal['desc', 'asc']): The order in which the intervals should be sorted. Default is 'desc'.
**kwargs: Additional keyword arguments for the request.
Returns:
dict[str, list[records] | dict[str, int]]:
"intervals" (list[records]): A list of dictionaries (with string keys) representing the intervals.
"meta" (dict[str, int]): A dictionary representing the meta information.
Raises:
Exception: If the API response is not successful.
"""
with aTimeLogger(username, password) as atl:
return atl.get_intervals(offset, limit, datetime_range, types, order, **kwargs)