hamster-tools/hamstertools/kimai.py
2023-10-27 21:02:17 +01:00

60 lines
1.6 KiB
Python

import requests
import requests_cache
import os
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
}
class BaseAPI(object):
def __init__(self, api, **kwargs):
requests_cache.install_cache('kimai', backend='sqlite', expire_after=1800)
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):
response = requests.get(
f'{api.KIMAI_API_URL}/customers?visible=3', headers=api.auth_headers).json()
return [
Customer(api, c['id'], c['name']) for c in response
]
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):
response = requests.get(
f'{api.KIMAI_API_URL}/projects?visible=3', headers=api.auth_headers).json()
return [
Project(api, p['id'], p['name'], p['customer']) for p in response
]
def __repr__(self):
return f'Project (id={self.id}, name={self.name}, customer={self.customer})'
customers = Customer.list(KimaiAPI())
projects = Project.list(KimaiAPI())
from pdb import set_trace; set_trace()