from grocy import RestService from tabulate import tabulate from os import path class Recipe(object): GET_RECIPES = '/get-objects/recipes' GET_PRODUCT_BY_ID = '/get-object/products/{0}' def __init__(self, **entries): self.__dict__.update(entries) self._init_rest_service() #self._set_default_table_formats() #if not hasattr('tablefmt', self): # self.tablefmt = None #if not hasattr('colalign', self): # self.colalign = None def _set_default_table_formats(self): if not hasattr('formats', self): self.tablefmt = None self.colalign = None elif not hasattr('table', self.formats): self.tableformat = None elif not hasattr('col', self.formats): self.colalign = None def _init_rest_service(self): if self.api.startswith == '/': self.api = self.api[1:] if self.api.endswith == '/': self.api = self.api[1:-1] self.rest_service = RestService(self.api, json=True) self.rest_service.addHeader('Content-Type', 'application/json') self.rest_service.addToken(self.token) def get_list(self): try: recipes = self.rest_service.get(Recipe.GET_RECIPES) table_headers = ['#', 'Name'] table_entries = [] for recipe in recipes: table_entry = [recipe.get('id'), recipe.get('name')] table_entries.append(table_entry) except Exception as e: raise e # Generate stock overview table return tabulate(table_entries, headers=table_headers)