2023-10-27 20:02:17 +00:00
|
|
|
import requests
|
2023-10-28 00:21:23 +00:00
|
|
|
import requests_cache
|
2023-10-27 20:02:17 +00:00
|
|
|
import os
|
|
|
|
|
2023-10-27 20:11:29 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-27 20:11:29 +00:00
|
|
|
def __init__(self):
|
2023-10-28 00:21:23 +00:00
|
|
|
requests_cache.install_cache('kimai', backend='sqlite', expire_after=1800)
|
2023-10-27 20:11:29 +00:00
|
|
|
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()
|
2023-10-28 00:21:23 +00:00
|
|
|
self.activities_json = requests.get(
|
|
|
|
f'{self.KIMAI_API_URL}/activities?visible=3', headers=self.auth_headers).json()
|
2023-10-27 20:11:29 +00:00
|
|
|
|
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 [
|
2023-10-27 20:11:29 +00:00
|
|
|
Customer(api, c['id'], c['name']) for c in api.customers_json
|
2023-10-27 20:02:17 +00:00
|
|
|
]
|
|
|
|
|
2023-10-27 20:11:29 +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 [
|
2023-10-27 20:11:29 +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
|
|
|
]
|
|
|
|
|
2023-10-27 20:11:29 +00:00
|
|
|
@staticmethod
|
2023-10-28 00:21:23 +00:00
|
|
|
def get_by_id(api, id, none=False):
|
2023-10-27 20:11:29 +00:00
|
|
|
for value in api.projects_json:
|
|
|
|
if value['id'] == id:
|
|
|
|
return Project(api, value['id'], value['name'],
|
|
|
|
Customer.get_by_id(api, value['customer']))
|
2023-10-28 00:21:23 +00:00
|
|
|
if not none:
|
|
|
|
raise NotFound()
|
2023-10-27 20:11:29 +00:00
|
|
|
|
2023-10-27 20:02:17 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return f'Project (id={self.id}, name={self.name}, customer={self.customer})'
|
|
|
|
|
|
|
|
|
2023-10-28 00:21:23 +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 [
|
|
|
|
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})'
|