-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclicktime.py
executable file
·694 lines (558 loc) · 19 KB
/
clicktime.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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
#!/usr/bin/env python
#
# A simple library to interfaces with the ClickTime API as documented
# at http://app.clicktime.com/api/1.3/help
#
# Copyright 2012 Michael Ihde
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import http.client
import base64
import copy
import json
import datetime
import urllib.parse
import logging
import contextlib
class Connection(object):
"""
A basic ClickTime connection
"""
SERVER = "api.clicktime.com"
URL_BASE = "v2"
def __init__(self, username=None, password=None, token=None):
if username and password:
auth = base64.encodestring(
f"{username}:{password}".encode("utf-8")
).strip() # remove the extra newline
self.__headers = {"Authorization": "Basic %s" % auth.decode("utf-8")}
elif token:
self.__headers = {"Authorization": "Token %s" % token}
else:
raise AttributeError("either username/password or token must be provided")
def get(self, url, *path, **params):
"""
Helper function to make a generic GET request
"""
# Prepare the headers, but take care not to manipulate the member varible
if self.__headers is not None:
headers = copy.copy(self.__headers)
else:
headers = {}
headers["Content-Type"] = "application/json"
# Create the connection
with contextlib.closing(http.client.HTTPSConnection(self.SERVER)) as connection:
# Encoding the query params
q = urllib.parse.urlencode(params)
# Build the full URL and log it
if path:
path = "/" + "/".join(path)
else:
path = ""
full_url = f"/{self.URL_BASE}/{url}{path}?{q}"
logging.debug(f"GET {full_url}")
# Make the request
connection.request("GET", full_url, headers=headers)
# Get the response
resp = connection.getresponse()
data = resp.read()
# Parse the data
data = self._parse(data)
# Return everything
return data, resp.status, resp.reason
def scroll(self, url, *path, **params):
"""
Helper function to automatically scroll all documents
"""
if "offset" in params:
raise AttributeError("offset cannot be used with scroll_reports")
while True:
result, _, _ = self.get(url, *path, **params)
if not result:
logging.debug("no result returned")
break
if not result.get("data"):
logging.debug("data is empty")
break
count = result.get("page", {}).get("count")
limit = result.get("page", {}).get("limit")
_next = result.get("page", {}).get("links", {}).get("next")
# yeild every item in data
for d in result.get("data"):
yield d
# see if there is a next page
if _next is None:
logging.debug("no next page")
break
if count == 0:
break
params["offset"] = params.get("offset", 0) + limit
def post(self, url, data=None):
"""
Internal helper method for POST requests.
"""
if self.headers is not None:
headers = copy.copy(self.headers)
else:
headers = {}
headers["content-type"] = "application/json; charset=utf-8"
connection = http.client.HTTPSConnection(self.SERVER)
connection.request(
"POST", "%s/%s" % (self.URL_BASE, url), headers=headers, body=data
)
resp = connection.getresponse()
data = resp.read()
connection.close()
return data, resp.status, resp.reason
def _parse(self, json_str, default=None):
try:
return json.loads(json_str)
except ValueError:
logging.error("Error parsing JSON '%s'", json_str)
return default
##############################################################################
class Resolver(object):
"""
The Resolver takes entires like "JobID" and then does
a look up using the endpoint "Jobs/{identifer}" and placed
the result into "Job"
"""
def __init__(self, ct, *resolvers):
self.ct = ct
self.resolvers = resolvers
self.cache = {}
def nested_set(self, dic, value, *keys):
for key in keys[:-1]:
dic = dic.setdefault(key, {})
dic[keys[-1]] = value
def nested_get(self, dic, *keys):
for key in keys[:-1]:
dic = dic.get(key, {})
return dic.get(keys[-1])
def _resolve(self, ct, resolver, d):
resolver_fields = tuple(resolver.split("."))
identifier = self.nested_get(d, *resolver_fields)
endpoint = resolver_fields[-1][0:-2]
keys = resolver_fields[0:-1] + (endpoint,)
cache_key = (keys, identifier)
if cache_key in self.cache:
data = self.cache[cache_key]
else:
value, _, _ = self.ct.connection.get(f"{endpoint}s/{identifier}")
data = value.get("data")
self.cache[cache_key] = data
self.nested_set(d, data, *keys)
return d
def resolve_all(self, ct, d):
for resolver in self.resolvers:
self._resolve(ct, resolver, d)
def _prefetch(self, ct, resolver):
resolver_fields = tuple(resolver.split("."))
endpoint = resolver_fields[-1][0:-2]
keys = resolver_fields[0:-1] + (endpoint,)
callable_endpoint = getattr(ct, f'{endpoint.lower()}s', None)
if not callable(callable_endpoint):
# Cannot find endpoint for pre-fetch
logging.warning("Cannot prefetch %s", endpoint)
return
for vv in callable_endpoint().scroll():
identifier = self.nested_get(vv, 'ID')
cache_key = (keys, identifier)
self.cache[cache_key] = vv
def prefetch(self):
for resolver in self.resolvers:
self._prefetch(self.ct, resolver)
##############################################################################
class Result(object):
"""
Result object
"""
class ResultIterator(object):
"""
Internal result iterator
"""
def __init__(self, data):
self.data = data
self._index = 0
def __next__(self):
if self._index < len(self.data):
v = self.data[self._index]
self._index += 1
return v
raise StopIteration
def __init__(self, result, status, reason):
self.result = result
self.status = status
self.reason = reason
def resolve(self, *resolvers):
return Resolver(self.ct, self, *resolvers)
@property
def isiterable(self):
return isinstance(self.data, list)
@property
def page(self):
return self.result.get("page")
@property
def data(self):
return self.result.get("data")
def __iter__(self):
if self.isiterable:
return Result.ResultIterator(self.data)
else:
raise TypeError("result is not iterable")
class Endpoint(object):
def __init__(self, ct, url, valid_params=None):
self.ct = ct
self.url = url
if valid_params:
self.valid_params = set(valid_params)
else:
self.valid_params = set()
self.resolver = None
self.parameters = {}
self.path = []
def resolve(self, *resolvers):
self.resolver = Resolver(self.ct, *resolvers)
return self
def params(self, **params):
invalid_params = set(params.keys()) - self.valid_params
if invalid_params:
raise ValueError(f"invalid params: {invalid_params}")
self.parameters = dict(params)
return self
def check_params(self, params):
invalid_params = set(params.keys()) - self.valid_params
return invalid_params
def execute(self):
result, status, reason = self.ct.connection.get(
self.url, *self.path, **self.parameters
)
result = Result(result, status, reason)
if self.resolver:
if result.isiterable:
for d in result:
self.resolver.resolve_all(ct, d)
else:
self.resolver.resolve_all(ct, result.data)
return result
def get(self):
return self.execute().data
def scroll(self, *path, **params):
raise TypeError("endpoint is not scrollable")
class ScrollableEndpoint(Endpoint):
def scroll(self, **params):
for d in self.ct.connection.scroll(self.url, *self.path, **self.parameters):
if self.resolver:
self.resolver.resolve_all(self.ct, d)
yield d
##############################################################################
# Endpoints
##############################################################################
class AllocationsEndpoint(ScrollableEndpoint):
def __init__(self, ct):
super().__init__(
ct,
"Allocations",
(
"JobID",
"UserID",
"DivisionID",
"StartMonth",
"EndMonth",
"JobIsActive",
"UserIsActive",
"limit",
"offset",
),
)
class CustomFieldsEndpoint(ScrollableEndpoint):
def __init__(self, ct, base_url):
super().__init__(ct, f"{base_url}/CustomFieldDefinitions", ("limit", "offset"))
def params(self, **params):
customFieldDefinitionID = params.pop("customFieldDefinitionID", None)
if customFieldDefinitionID is not None:
self.path = [customFieldDefinitionID]
else:
self.path = []
return super().params(**params)
class ClientEndpoint(ScrollableEndpoint):
def __init__(self, ct):
super().__init__(
ct,
"Clients",
("ID", "IsActive", "Name", "ShortName", "ClientNumber", "limit", "offset"),
)
def custom_fields(self):
return CustomFieldsEndpoint(self.ct, "Clients")
def params(self, **params):
clientID = params.pop("clientID", None)
if clientID is not None:
self.path = [clientID]
else:
self.path = []
return super().params(**params)
class CompanyEndpoint(Endpoint):
def __init__(self, ct):
super().__init__(ct, "Company")
class CustomMessagesEndpoint(Endpoint):
def __init__(self, ct):
super().__init__(ct, "CustomMessages")
def params(self, **params):
customMessageID = params.pop("customMessageID", None)
if customMessageID is not None:
self.path = [customMessageID]
else:
self.path = []
return super().params(**params)
class DivisionsEndpoint(ScrollableEndpoint):
def __init__(self, ct):
super().__init__(ct, "Divisions", ("ID", "IsActive", "Name", "limit", "offset"))
def custom_fields(self):
return CustomFieldsEndpoint(self.ct, "Divisions")
def params(self, **params):
clientID = params.pop("divisionID", None)
if clientID is not None:
self.path = [clientID]
else:
self.path = []
return super().params(**params)
class ReportsEndpoint(ScrollableEndpoint):
def __init__(self, ct):
super().__init__(
ct,
"Reports/Time",
(
"JobID",
"ClientID",
"UserID",
"TaskID",
"DivisionID",
"LabelID",
"TimesheetID",
"TimesheetStatus",
"StartDate",
"EndDate",
"IsBillable",
"limit",
"offset",
"verbose",
),
)
class JobsEndpoint(ScrollableEndpoint):
def __init__(self, ct):
super().__init__(
ct,
"Jobs",
(
"ID",
"ClientID",
"Name",
"JobNumber",
"IsActive",
"ProjectManagerID",
"limit",
"offset",
),
)
class TimeEntriesEndpoint(ScrollableEndpoint):
def __init__(self, ct):
super().__init__(
ct,
"TimeEntries",
("JobID", "UserID", "TaskID", "StartDate", "EndDate", "limit", "offset"),
)
class UsersEndpoint(ScrollableEndpoint):
def __init__(self, ct):
super().__init__(
ct,
"Users",
(
"ID",
"IsActive",
"Name",
"Email",
"DivisionID",
"TimesheetApproverID",
"ExpenseApproverID",
"EmploymentTypeID",
"JobID",
"SecurityLevel",
"ManagerPermission",
"limit",
"offset",
),
)
def custom_fields(self):
return CustomFieldsEndpoint(self.ct, "Users")
def params(self, **params):
userID = params.pop("userID", None)
if userID is not None:
self.path = [userID]
else:
self.path = []
return super().params(**params)
class TimeOffEndpoint(ScrollableEndpoint):
def __init__(self, ct):
super().__init__(
ct,
"TimeOff",
(
"ID",
"TimeOffTypeID",
"UserID",
"FromDate",
"ToDate",
"Date",
"limit",
"offset",
),
)
def custom_fields(self):
return CustomFieldsEndpoint(self.ct, "Users")
def params(self, **params):
timeOffID = params.pop("timeOffID", None)
if timeOffID is not None:
self.path = [timeOffID]
else:
self.path = []
return super().params(**params)
class ExpenseSheetEndpoint(ScrollableEndpoint):
def __init__(self, ct):
super().__init__(
ct,
"ExpenseSheets",
(
"ID",
"FromExpenseSheetDate",
"ToExpenseSheetDate",
"Status",
"Paid",
"ExpenseApproverID",
"UserID",
"UserIsActive",
"DivisionID",
"EmploymentTypeID",
"ExpenseDate",
"SortBy",
),
)
class ExpenseItemsEndpoint(ScrollableEndpoint):
def __init__(self, ct):
super().__init__(
ct,
"ExpenseItems",
(
"ID",
"ExpenseDate",
"ExpenseSheetID",
"ExpenseTypeID",
"JobID",
"UserID",
"SortBy"
),
)
##############################################################################
# ClickTime
##############################################################################
class ClickTime(object):
"""
Primary object to interact with the ClickTime API.
Instantiate the object:
>>> ct = ClickTime(token="your_api_token")
Access an endpoint:
>>> ct.reports().get()
Or to scoll through all the records:
>>> ct.reports().scroll()
If you want to resolve identifiers (i.e. JobID, UserID)
>>> ct.reports().resolve("JobID", "UserID").scroll()
"""
def __init__(self, username=None, password=None, token=None):
self.username = username
self.password = password
self.token = token
@property
def connection(self):
return Connection(self.username, self.password, self.token)
def allocations(self):
return AllocationsEndpoint(self)
def clients(self):
"""
Also known as Project Groups
"""
return ClientEndpoint(self)
def company(self):
return CompanyEndpoint(self)
def custom_messages(self):
return CustomMessagesEndpoint(self)
def divisions(self):
return DivisionsEndpoint(self)
def jobs(self):
return JobsEndpoint(self)
def reports(self):
return ReportsEndpoint(self)
def time_entries(self):
return TimeEntriesEndpoint(self)
def users(self):
return UsersEndpoint(self)
def timeoff(self):
return TimeOffEndpoint(self)
##############################################################################
if __name__ == "__main__":
"""
Example implementation using the ClickTime class
"""
from optparse import OptionParser
from pprint import pprint
import logging
import os
try:
import elasticsearch
except ImportError:
pass
logging.basicConfig()
parser = OptionParser()
parser.add_option("-u", "--username")
parser.add_option("-p", "--password")
parser.add_option("-t", "--token")
parser.add_option("--debug", action="store_true", default=False)
parser.add_option("--scroll", action="store_true", default=False)
parser.add_option("--resolve", action="append", default=[], dest="resolvers")
parser.add_option("--param", action="append", default=[], dest="params")
opts, args = parser.parse_args()
# If we are debugging, turn extra debugging on
if opts.debug:
logging.getLogger().setLevel(logging.DEBUG)
# If authentication wasn't provided, look for '.token'
if (opts.username is None) and (opts.password is None) and (opts.token is None):
if os.path.exists(".token"):
with open(".token") as f:
opts.token = f.read().strip()
# Create the API class
ct = ClickTime(opts.username, opts.password, opts.token)
# See what Endpoint the user wants to calls
if args:
actions = args[0].lower().split(".")
params = dict([p.split("=") for p in opts.params])
endpoint = ct
for action in actions:
if hasattr(endpoint, action):
endpoint_factory = getattr(endpoint, action, None)
endpoint = endpoint_factory()
else:
print(f"invalid action {action}")
raise SystemExit()
if opts.scroll:
for d in endpoint.params(**params).resolve(*opts.resolvers).scroll():
pprint(d)
else:
pprint(endpoint.params(**params).resolve(*opts.resolvers).get())