grocy-cli/grocy/shoppinglist.py

79 lines
3.0 KiB
Python

from grocy.request import Request
from grocy.conf import Configuration
import logging
class ShoppingList(object):
ADD_PRODUCT_URL_TEMPLATE = '{domain}/api/stock/shoppinglist/add-product'
REMOVE_PRODUCT_URL_TEMPLATE = '{domain}/api/stock/shoppinglist/remove-product'
CLEAR_SHOPPING_LIST_URL_TEMPLATE = '{domain}/api/stock/shoppinglist/clear'
ADD_MISSING_PRODUCTS = '{domain}/api/stock/shoppinglist/add-missing-products'
def __init__(self, id=None):
self.conf = Configuration()
self.conf.load()
self.id = id
def add_product(self, entity):
logger = logging.getLogger('shoppinglist.add_product')
try:
if self.id is None:
raise Exception('shopping list id is required')
if entity and 'product_id' not in entity:
raise Exception('Must provide a product id')
if entity and 'product_amount' not in entity:
raise Exception('Must provide a product_amount id')
entity['list_id'] = self.id
url = self.ADD_PRODUCT_URL_TEMPLATE.format(domain=self.conf.domain)
request = Request('post', url, resource=entity)
return request.send()
except Exception as e:
logger.error(e)
raise e
def remove_product(self, entity):
logger = logging.getLogger('shoppinglist.remove_product')
try:
if self.id is None:
raise Exception('shopping list id is required')
if entity and 'product_id' not in entity:
raise Exception('Must provide a product id')
if entity and 'product_amount' not in entity:
raise Exception('Must provide a product_amount id')
entity['list_id'] = self.id
url = self.REMOVE_PRODUCT_URL_TEMPLATE.format(domain=self.conf.domain)
request = Request('post', url, resource=entity)
return request.send()
except Exception as e:
logger.error(e)
raise e
def clear(self):
logger = logging.getLogger('shoppinglist.clear')
try:
if self.id is None:
raise Exception('shopping list id is required')
entity = {'list_id': self.id}
url = self.CLEAR_SHOPPING_LIST_URL_TEMPLATE.format(domain=self.conf.domain)
request = Request('post', url, resource=entity)
return request.send()
except Exception as e:
logger.eror(e)
raise e
def add_missing_products(self):
logger = logging.getLogger('shoppinglist.add_min_stock')
try:
if self.id is None:
raise Exception('shopping list id is required')
entity = {'list_id': self.id}
url = self.ADD_MISSING_PRODUCTS.format(domain=self.conf.domain)
request = Request('post', url, resource=entity)
return request.send()
except Exception as e:
logger.error(e)
raise e