diff --git a/corepos/api.py b/corepos/api.py index e67a0ff..67e7f14 100644 --- a/corepos/api.py +++ b/corepos/api.py @@ -109,6 +109,90 @@ class CoreWebAPI(object): assert set(js['result'].keys()) == set(['result']) return js['result']['result'] + def get_departments(self, **columns): + """ + Fetch some or all of Department records from CORE. + + :returns: A (potentially empty) list of department dict records. + + To fetch all departments:: + + api.get_departments() + + To fetch only departments named "Grocery":: + + api.get_departments(dept_name='Grocery') + """ + params = { + 'entity': 'Departments', + 'submethod': 'get', + 'columns': columns, + } + response = self.post(params) + result = self.parse_response(response) + return [json.loads(rec) for rec in result] + + def get_department(self, dept_no, **columns): + """ + Fetch an existing Department record from CORE. + + :returns: Either a department dict record, or ``None``. + """ + columns['dept_no'] = dept_no + params = { + 'entity': 'Departments', + 'submethod': 'get', + 'columns': columns, + } + response = self.post(params) + result = self.parse_response(response) + if result: + if len(result) > 1: + log.warning("CORE API returned %s department results", len(result)) + return json.loads(result[0]) + + def get_subdepartments(self, **columns): + """ + Fetch some or all of Subdepartment records from CORE. + + :returns: A (potentially empty) list of subdepartment dict records. + + To fetch all subdepartments:: + + api.get_subdepartments() + + To fetch only subdepartments named "Grocery":: + + api.get_subdepartments(subdept_name='Grocery') + """ + params = { + 'entity': 'SubDepts', + 'submethod': 'get', + 'columns': columns, + } + response = self.post(params) + result = self.parse_response(response) + return [json.loads(rec) for rec in result] + + def get_subdepartment(self, subdept_no, **columns): + """ + Fetch an existing Subdepartment record from CORE. + + :returns: Either a subdepartment dict record, or ``None``. + """ + columns['subdept_no'] = subdept_no + params = { + 'entity': 'SubDepts', + 'submethod': 'get', + 'columns': columns, + } + response = self.post(params) + result = self.parse_response(response) + if result: + if len(result) > 1: + log.warning("CORE API returned %s subdepartment results", len(result)) + return json.loads(result[0]) + def get_vendors(self, **columns): """ Fetch some or all of Vendor records from CORE. @@ -172,3 +256,45 @@ class CoreWebAPI(object): response = self.post(params) result = self.parse_response(response) return json.loads(result) + + def get_products(self, **columns): + """ + Fetch some or all of Product records from CORE. + + :returns: A (potentially empty) list of product dict records. + + To fetch all products:: + + api.get_products() + + To fetch only products with brand name "Braggs":: + + api.get_products(brand='Braggs') + """ + params = { + 'entity': 'Products', + 'submethod': 'get', + 'columns': columns, + } + response = self.post(params) + result = self.parse_response(response) + return [json.loads(rec) for rec in result] + + def get_product(self, upc, **columns): + """ + Fetch an existing Product record from CORE. + + :returns: Either a product dict record, or ``None``. + """ + columns['upc'] = upc + params = { + 'entity': 'Products', + 'submethod': 'get', + 'columns': columns, + } + response = self.post(params) + result = self.parse_response(response) + if result: + if len(result) > 1: + log.warning("CORE API returned %s product results", len(result)) + return json.loads(result[0])