API improvements
This commit is contained in:
@ -21,16 +21,22 @@ class KimaiAPI(object):
|
||||
auth_headers = {"X-AUTH-USER": KIMAI_USERNAME, "X-AUTH-TOKEN": KIMAI_API_KEY}
|
||||
|
||||
def __init__(self):
|
||||
requests_cache.install_cache("kimai", backend="sqlite", expire_after=1800)
|
||||
# requests_cache.install_cache("kimai", backend="sqlite", expire_after=1800)
|
||||
self.customers_json = self.get("customers", {"visible": 3})
|
||||
self.projects_json = self.get("projects", {"visible": 3, "ignoreDates": 1})
|
||||
self.activities_json = self.get("activities", {"visible": 3})
|
||||
self.user_json = self.get("users/me")
|
||||
|
||||
def get(self, endpoint, params=None):
|
||||
return requests.get(
|
||||
result = requests.get(
|
||||
f"{self.KIMAI_API_URL}/{endpoint}", params=params, headers=self.auth_headers
|
||||
).json()
|
||||
try:
|
||||
if result["code"] != 200:
|
||||
raise NotFound()
|
||||
except (KeyError, TypeError):
|
||||
pass
|
||||
return result
|
||||
|
||||
def post(self, endpoint, data):
|
||||
return requests.post(
|
||||
@ -133,7 +139,7 @@ class Activity(BaseAPI):
|
||||
api,
|
||||
a["id"],
|
||||
a["name"],
|
||||
Project.get_by_id(api, a["project"]),
|
||||
Project.get_by_id(api, a["project"], none),
|
||||
a["visible"],
|
||||
)
|
||||
if not none:
|
||||
@ -169,6 +175,26 @@ class Timesheet(BaseAPI):
|
||||
)
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def list_by(api, **kwargs):
|
||||
kwargs['size'] = 10000
|
||||
return [
|
||||
Timesheet(
|
||||
api,
|
||||
Activity.get_by_id(api, t["activity"], none=True),
|
||||
Project.get_by_id(api, t["project"], none=True),
|
||||
t["begin"],
|
||||
t["end"],
|
||||
t["id"],
|
||||
t["description"],
|
||||
t["tags"],
|
||||
)
|
||||
for t in api.get(
|
||||
"timesheets",
|
||||
params=kwargs
|
||||
)
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def get_by_id(api, id, none=False):
|
||||
t = api.get(
|
||||
|
Reference in New Issue
Block a user