From d88098dd30ed097f991e9501ecf3a0d4f230dd4e Mon Sep 17 00:00:00 2001 From: 3wc <3wc@doesthisthing.work> Date: Fri, 27 Oct 2023 21:02:17 +0100 Subject: [PATCH] Initial Kimai API --- hamstertools/kimai.py | 59 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 hamstertools/kimai.py diff --git a/hamstertools/kimai.py b/hamstertools/kimai.py new file mode 100644 index 0000000..5d2f84c --- /dev/null +++ b/hamstertools/kimai.py @@ -0,0 +1,59 @@ +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()