2019-06-16 23:54:10 -05:00
|
|
|
from grocy.request import Request
|
2019-06-25 00:52:17 -05:00
|
|
|
import re
|
2019-07-07 13:58:01 -05:00
|
|
|
from copy import deepcopy
|
2019-06-16 23:54:10 -05:00
|
|
|
from grocy.conf import Configuration
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
|
|
class Entity(object):
|
2019-07-07 13:58:01 -05:00
|
|
|
RESOURCE_URL_TEMPLATE = '{domain}/api/objects/{entity}/{objectId}'
|
|
|
|
COLLECTION_URL_TEMPLATE = '{domain}/api/objects/{entity}'
|
|
|
|
SCHEMA_URL_TEMPLATE = '{domain}/api/openapi/specification'
|
2019-06-25 00:52:17 -05:00
|
|
|
SCHEMA_MODEL_MAP = {
|
|
|
|
'products': 'Product',
|
2019-07-07 13:58:01 -05:00
|
|
|
'stock': 'StockEntry',
|
|
|
|
'product_groups':'ProductGroup',
|
|
|
|
'locations': 'Location',
|
2019-09-25 23:14:11 -05:00
|
|
|
'quantity_units': 'QuantityUnit',
|
|
|
|
'recipes': 'Recipe'
|
2019-06-25 00:52:17 -05:00
|
|
|
}
|
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-07-07 13:58:01 -05:00
|
|
|
|
2019-06-16 23:54:10 -05:00
|
|
|
def get(self, id=None):
|
|
|
|
logger = logging.getLogger('entity.get')
|
|
|
|
if id:
|
2019-07-07 13:58:01 -05:00
|
|
|
url = self.RESOURCE_URL_TEMPLATE.format(domain=self.conf.domain, entity=self.name, objectId=id)
|
2019-06-16 23:54:10 -05:00
|
|
|
else:
|
2019-07-07 13:58:01 -05:00
|
|
|
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
|
|
|
|
|
2019-06-25 00:52:17 -05:00
|
|
|
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:
|
2019-08-04 23:59:28 -05:00
|
|
|
return []
|
2019-06-25 00:52:17 -05:00
|
|
|
|
|
|
|
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')
|
2019-07-07 13:58:01 -05:00
|
|
|
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
|
2019-06-25 00:52:17 -05:00
|
|
|
|
|
|
|
def update(self, entity, id=None):
|
|
|
|
if id is None:
|
|
|
|
raise Exception('id property is required to update entity')
|
|
|
|
logger = logging.getLogger('entity.update')
|
2019-07-07 13:58:01 -05:00
|
|
|
url = self.RESOURCE_URL_TEMPLATE.format(domain=self.conf.domain, entity=self.name, objectId=id)
|
2019-06-25 00:52:17 -05:00
|
|
|
|
|
|
|
request = Request('put', url, resource=entity)
|
|
|
|
try:
|
|
|
|
return request.send()
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(e)
|
|
|
|
raise e
|
|
|
|
|
2019-09-25 23:14:11 -05:00
|
|
|
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
|
|
|
|
|
2019-06-25 00:52:17 -05:00
|
|
|
@property
|
|
|
|
def schema(self):
|
|
|
|
logger = logging.getLogger('entity.schema')
|
|
|
|
try:
|
2019-07-07 13:58:01 -05:00
|
|
|
url = self.SCHEMA_URL_TEMPLATE.format(domain=self.conf.domain)
|
2019-06-25 00:52:17 -05:00
|
|
|
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
|