From 5509089741dd8cda1408285093b97f343ff57384 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sun, 15 Mar 2020 19:29:01 -0500 Subject: [PATCH] Add `get_customer()` API method --- corepos/api.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/corepos/api.py b/corepos/api.py index b324f96..9a6b5de 100644 --- a/corepos/api.py +++ b/corepos/api.py @@ -75,7 +75,9 @@ class CoreWebAPI(object): specified, ``method`` will be CORE's ``FannieEntity`` webservice. """ if not method: - method = r'\COREPOS\Fannie\API\webservices\FannieEntity' + method = 'FannieEntity' + if '\\' not in method: + method = r'\COREPOS\Fannie\API\webservices\{}'.format(method) payload = { 'jsonrpc': '2.0', @@ -91,7 +93,7 @@ class CoreWebAPI(object): response.raise_for_status() return response - def parse_response(self, response): + def parse_response(self, response, method=None): """ Generic method to "parse" a response from the API. Really this just converts the JSON to a dict (etc.), and then checks for error. If an @@ -105,10 +107,30 @@ class CoreWebAPI(object): if 'error' in js: raise CoreAPIError(js['error']) + # note, the result data format may depend on the API method involved + if method == 'FannieMember': + return js['result'] + + # assuming typical FannieEntity result here assert set(js.keys()) == set(['jsonrpc', 'id', 'result']) assert set(js['result'].keys()) == set(['result']) return js['result']['result'] + def get_customer(self, cardNo, **columns): + """ + Fetch an existing Customer record from CORE. + + :returns: Either a customer dict record, or ``None``. + """ + params = { + 'cardNo': cardNo, + 'method': 'get', + } + response = self.post(params, method='FannieMember') + result = self.parse_response(response, method='FannieMember') + if result: + return result + def get_departments(self, **columns): """ Fetch some or all of Department records from CORE.