hamster-tools/hamstertools/kimai.py

113 lines
3.2 KiB
Python
Raw Normal View History

2023-10-27 20:02:17 +00:00
import requests
import requests_cache
2023-10-27 20:02:17 +00:00
import os
class NotFound(Exception):
pass
2023-10-27 20:02:17 +00:00
class KimaiAPI(object):
# temporary hardcoded config
2023-10-29 21:56:39 +00:00
KIMAI_API_URL = "https://kimai.autonomic.zone/api"
2023-10-27 20:02:17 +00:00
2023-10-29 21:56:39 +00:00
KIMAI_USERNAME = "3wordchant"
KIMAI_API_KEY = os.environ["KIMAI_API_KEY"]
2023-10-27 20:02:17 +00:00
2023-10-29 21:56:39 +00:00
auth_headers = {"X-AUTH-USER": KIMAI_USERNAME, "X-AUTH-TOKEN": KIMAI_API_KEY}
2023-10-27 20:02:17 +00:00
def __init__(self):
2023-10-29 21:56:39 +00:00
requests_cache.install_cache("kimai", backend="sqlite", expire_after=1800)
self.customers_json = requests.get(
2023-10-29 21:56:39 +00:00
f"{self.KIMAI_API_URL}/customers?visible=3", headers=self.auth_headers
).json()
self.projects_json = requests.get(
2023-10-29 21:56:39 +00:00
f"{self.KIMAI_API_URL}/projects?visible=3", headers=self.auth_headers
).json()
self.activities_json = requests.get(
2023-10-29 21:56:39 +00:00
f"{self.KIMAI_API_URL}/activities?visible=3", headers=self.auth_headers
).json()
2023-10-27 20:02:17 +00:00
class BaseAPI(object):
def __init__(self, api, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
class Customer(BaseAPI):
def __init__(self, api, id, name):
super().__init__(api, id=id, name=name)
@staticmethod
def list(api):
2023-10-29 21:56:39 +00:00
return [Customer(api, c["id"], c["name"]) for c in api.customers_json]
2023-10-27 20:02:17 +00:00
@staticmethod
def get_by_id(api, id):
for value in api.customers_json:
2023-10-29 21:56:39 +00:00
if value["id"] == id:
return Customer(api, value["id"], value["name"])
raise NotFound()
2023-10-27 20:02:17 +00:00
def __repr__(self):
2023-10-29 21:56:39 +00:00
return f"Customer (id={self.id}, name={self.name})"
2023-10-27 20:02:17 +00:00
class Project(BaseAPI):
def __init__(self, api, id, name, customer):
super().__init__(api, id=id, name=name, customer=customer)
@staticmethod
def list(api):
return [
2023-10-29 21:56:39 +00:00
Project(api, p["id"], p["name"], Customer.get_by_id(api, p["customer"]))
for p in api.projects_json
2023-10-27 20:02:17 +00:00
]
@staticmethod
def get_by_id(api, id, none=False):
for value in api.projects_json:
2023-10-29 21:56:39 +00:00
if value["id"] == id:
return Project(
api,
value["id"],
value["name"],
Customer.get_by_id(api, value["customer"]),
)
if not none:
raise NotFound()
2023-10-27 20:02:17 +00:00
def __repr__(self):
2023-10-29 21:56:39 +00:00
return f"Project (id={self.id}, name={self.name}, customer={self.customer})"
2023-10-27 20:02:17 +00:00
class Activity(BaseAPI):
def __init__(self, api, id, name, project):
super().__init__(api, id=id, name=name, project=project)
@staticmethod
def list(api):
return [
2023-10-29 21:56:39 +00:00
Activity(
api, a["id"], a["name"], Project.get_by_id(api, a["project"], none=True)
)
for a in api.activities_json
]
@staticmethod
def get_by_id(api, id, none=False):
for value in api.activities_json:
2023-10-29 21:56:39 +00:00
if value["id"] == id:
return Activity(
api,
value["id"],
value["name"],
Project.get_by_id(api, value["project"]),
)
if not none:
raise NotFound()
def __repr__(self):
2023-10-29 21:56:39 +00:00
return f"Activity (id={self.id}, name={self.name}, project={self.project})"