grocy-cli/grocy/stock.py

32 lines
1005 B
Python
Raw Normal View History

2019-06-16 23:54:10 -05:00
from grocy.request import Request
from grocy.conf import Configuration
import logging
class Stock(object):
GET_STOCK_URL_TEMPLATE = '{domain}/api/stock'
GET_STOCK_PRODUCT_DETAIL_TEMPLATE = '{domain}/api/stock/products/{product_id}'
2019-06-16 23:54:10 -05:00
def __init__(self):
self.conf = Configuration()
self.conf.load()
def __get_resources(self, url, logger):
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
@property
def products(self):
logger = logging.getLogger('stock.products')
url = self.GET_STOCK_URL_TEMPLATE.format(domain=self.conf.domain)
return self.__get_resources(url, logger)
def get_product(self, product_id):
logger = logging.getLogger('stock.get_product')
url = self.GET_STOCK_PRODUCT_DETAIL_TEMPLATE.format(domain=self.conf.domain, product_id=product_id)
return self.__get_resources(url, logger)