grocy-cli/grocy/shoppinglist.py

31 lines
1.0 KiB
Python
Raw Normal View History

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'
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