2019-06-16 23:54:10 -05:00
|
|
|
from grocy.request import Request
|
|
|
|
from grocy.conf import Configuration
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
|
|
class Stock(object):
|
2019-07-07 13:58:01 -05:00
|
|
|
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()
|
|
|
|
|
2019-06-22 02:21:23 -05:00
|
|
|
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
|
|
|
|
|
2019-06-22 02:21:23 -05:00
|
|
|
@property
|
|
|
|
def products(self):
|
|
|
|
logger = logging.getLogger('stock.products')
|
2019-07-07 13:58:01 -05:00
|
|
|
url = self.GET_STOCK_URL_TEMPLATE.format(domain=self.conf.domain)
|
2019-06-22 02:21:23 -05:00
|
|
|
return self.__get_resources(url, logger)
|
|
|
|
|
|
|
|
def get_product(self, product_id):
|
|
|
|
logger = logging.getLogger('stock.get_product')
|
2019-07-07 13:58:01 -05:00
|
|
|
url = self.GET_STOCK_PRODUCT_DETAIL_TEMPLATE.format(domain=self.conf.domain, product_id=product_id)
|
2019-06-22 02:21:23 -05:00
|
|
|
return self.__get_resources(url, logger)
|