API improvements

This commit is contained in:
3wc
2023-11-17 22:54:25 +00:00
parent a01652f301
commit 1145f5e806
3 changed files with 58 additions and 3 deletions

View File

@ -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(