grocy-cli/grocy/entity.py

114 lines
3.6 KiB
Python
Raw Normal View History

2019-06-16 23:54:10 -05:00
from grocy.request import Request
import re
from copy import deepcopy
2019-06-16 23:54:10 -05:00
from grocy.conf import Configuration
import logging
class Entity(object):
RESOURCE_URL_TEMPLATE = '{domain}/api/objects/{entity}/{objectId}'
COLLECTION_URL_TEMPLATE = '{domain}/api/objects/{entity}'
SCHEMA_URL_TEMPLATE = '{domain}/api/openapi/specification'
SCHEMA_MODEL_MAP = {
'products': 'Product',
'stock': 'StockEntry',
'product_groups':'ProductGroup',
'locations': 'Location',
'quantity_units': 'QuantityUnit',
'recipes': 'Recipe'
}
2019-06-16 23:54:10 -05:00
def __init__(self, name, **props):
self.conf = Configuration()
self.conf.load()
self.name = name
self.__dict__.update(**props)
2019-06-16 23:54:10 -05:00
def get(self, id=None):
logger = logging.getLogger('entity.get')
if id:
url = self.RESOURCE_URL_TEMPLATE.format(domain=self.conf.domain, entity=self.name, objectId=id)
2019-06-16 23:54:10 -05:00
else:
url = self.COLLECTION_URL_TEMPLATE.format(domain=self.conf.domain, entity=self.name)
2019-06-16 23:54:10 -05:00
request = Request('get', url)
try:
return request.send()
except Exception as e:
logger.error(e)
raise e
def find(self, query):
logger = logging.getLogger('entity.find')
found_entities = []
try:
entities = self.get()
for entity in entities:
for prop, value in query.items():
regex = re.compile(r'{}'.format(value))
if regex.search(entity[prop]):
found_entities.append(entity)
if len(found_entities) == 0:
return []
return found_entities
except Exception as e:
logger.error(e)
raise e
2019-06-16 23:54:10 -05:00
def create(self, entity):
logger = logging.getLogger('entity.add')
url = self.COLLECTION_URL_TEMPLATE.format(domain=self.conf.domain, entity=self.name)
for key, value in entity.items():
if type(value) == bool:
entity[key] = '1' if value else '0'
request = Request('post', url, resource=entity)
2019-06-16 23:54:10 -05:00
try:
return request.send()
except Exception as e:
logger.error(e)
raise e
def update(self, entity, id=None):
if id is None:
raise Exception('id property is required to update entity')
logger = logging.getLogger('entity.update')
url = self.RESOURCE_URL_TEMPLATE.format(domain=self.conf.domain, entity=self.name, objectId=id)
request = Request('put', url, resource=entity)
try:
return request.send()
except Exception as e:
logger.error(e)
raise e
def delete(self, id=None):
if id is None:
raise Exception('id property is required to delete entity')
logger = logging.getLogger('entity.delete')
url = self.RESOURCE_URL_TEMPLATE.format(domain=self.conf.domain, entity=self.name, objectId=id)
request = Request('delete', url)
try:
return request.send()
except Exception as e:
logger.error(e)
raise e
@property
def schema(self):
logger = logging.getLogger('entity.schema')
try:
url = self.SCHEMA_URL_TEMPLATE.format(domain=self.conf.domain)
request = Request('get', url)
response = request.send()
schema_name = self.SCHEMA_MODEL_MAP[self.name]
return response['components']['schemas'][schema_name]
except Exception as e:
logger.error(e)
raise e