From 7dc09d0a2ee12cbbe985f175d9301318d46c2f2f Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sun, 17 Sep 2023 13:47:06 -0500 Subject: [PATCH] Let config override the CORE API client factory --- rattail_corepos/app.py | 20 ++++++++++++++++++++ rattail_corepos/corepos/api.py | 22 +++++++--------------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/rattail_corepos/app.py b/rattail_corepos/app.py index 9f09683..75df474 100644 --- a/rattail_corepos/app.py +++ b/rattail_corepos/app.py @@ -104,3 +104,23 @@ class CoreHandler(GenericHandler): office_url = self.get_office_url(require=require) if office_url: return f'{office_url}/item/vendors/VendorIndexPage.php?vid={id}' + + def make_webapi(self): + """ + Make and return a new CORE-POS API client object. + """ + + spec = self.config.get('corepos.api', 'factory', + default='corepos.api:CoreWebAPI') + factory = self.app.load_object(spec) + + url = self.config.require('corepos.api', 'url') + + kwargs = {} + username = self.config.get('corepos.api', 'htdigest.username') + password = self.config.get('corepos.api', 'htdigest.password') + if username and password: + kwargs['htdigest_username'] = username + kwargs['htdigest_password'] = password + + return factory(url, **kwargs) diff --git a/rattail_corepos/corepos/api.py b/rattail_corepos/corepos/api.py index f13af2c..22acb0d 100644 --- a/rattail_corepos/corepos/api.py +++ b/rattail_corepos/corepos/api.py @@ -24,22 +24,14 @@ CORE-POS API """ -from requests.auth import HTTPDigestAuth - -from corepos.api import CoreWebAPI +import warnings def make_corepos_api(config): - """ - Make and return a new CORE-POS API client object. - """ - url = config.require('corepos.api', 'url') + """ DEPRECATED """ + warnings.warn("make_corepos_api() function is deprecated; please use " + "app.get_corepos_handler().make_webapi() instead", + DeprecationWarning, stacklevel=2) - kwargs = {} - username = config.get('corepos.api', 'htdigest.username') - password = config.get('corepos.api', 'htdigest.password') - if username and password: - kwargs['htdigest_username'] = username - kwargs['htdigest_password'] = password - - return CoreWebAPI(url, **kwargs) + app = config.get_app() + return app.get_corepos_handler().make_webapi()