hamster-tools/hamstertools/kimai.py

106 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
KIMAI_API_URL = 'https://kimai.autonomic.zone/api'
KIMAI_USERNAME = '3wordchant'
KIMAI_API_KEY = os.environ['KIMAI_API_KEY']
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)
self.customers_json = requests.get(
f'{self.KIMAI_API_URL}/customers?visible=3', headers=self.auth_headers).json()
self.projects_json = requests.get(
f'{self.KIMAI_API_URL}/projects?visible=3', headers=self.auth_headers).json()
self.activities_json = requests.get(
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):
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:
if value['id'] == id:
return Customer(api, value['id'], value['name'])
raise NotFound()
2023-10-27 20:02:17 +00:00
def __repr__(self):
return f'Customer (id={self.id}, name={self.name})'
class Project(BaseAPI):
def __init__(self, api, id, name, customer):
super().__init__(api, id=id, name=name, customer=customer)
@staticmethod
def list(api):
return [
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:
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):
return f'Project (id={self.id}, name={self.name}, customer={self.customer})'
class Activity(BaseAPI):
def __init__(self, api, id, name, project):
super().__init__(api, id=id, name=name, project=project)
@staticmethod
def list(api):
return [
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:
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):
return f'Activity (id={self.id}, name={self.name}, project={self.project})'