From 11c69186469dfa110f4994ef8a9069e34028f047 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Fri, 31 Jan 2025 06:13:33 +0100 Subject: [PATCH] fix: import shopify API directly There is no need to delay importing and make it crash at usage time. Also add it to optional deps. --- pyproject.toml | 4 ++++ social_core/backends/shopify.py | 27 ++++++++++----------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 76f27092..7abe6071 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -54,6 +54,7 @@ version = "4.5.4" all = [ "social-auth-core[saml]", "social-auth-core[azuread]" + "social-auth-core[shopify]" ] allpy3 = [ "social-auth-core[all]" @@ -78,6 +79,9 @@ saml = [ # pinned to 5.2 until a new wheel of xmlsec is released "lxml~=5.2.1" ] +shopify = [ + "ShopifyAPI", +] [project.urls] Homepage = "https://github.com/python-social-auth/social-core" diff --git a/social_core/backends/shopify.py b/social_core/backends/shopify.py index abef6d8f..0294b67c 100644 --- a/social_core/backends/shopify.py +++ b/social_core/backends/shopify.py @@ -3,7 +3,7 @@ https://python-social-auth.readthedocs.io/en/latest/backends/shopify.html """ -import imp # type: ignore[reportMissingImports] +import shopify from ..exceptions import AuthCanceled, AuthFailed from ..utils import handle_http_errors @@ -22,13 +22,6 @@ class ShopifyOAuth2(BaseOAuth2): def shopify_api_version(self): return self.setting("API_VERSION", "2020-10") - @property - def shopify_api(self): - if not hasattr(self, "_shopify_api"): - fp, pathname, description = imp.find_module("shopify") - self._shopify_api = imp.load_module("shopify", fp, pathname, description) - return self._shopify_api - def get_user_details(self, response): """Use the shopify store name as the username""" return {"username": str(response.get("shop", "")).replace(".myshopify.com", "")} @@ -37,8 +30,8 @@ def extra_data(self, user, uid, response, details=None, *args, **kwargs): """Return access_token and extra defined names to store in extra_data field""" data = super().extra_data(user, uid, response, details, *args, **kwargs) - session = self.shopify_api.Session( - self.data.get("shop").strip(), version=self.shopify_api_version + session = shopify.Session( + self.data.get("shop").strip(), version=shopify_version ) # Get, and store the permanent token token = session.request_token(data["access_token"]) @@ -47,13 +40,13 @@ def extra_data(self, user, uid, response, details=None, *args, **kwargs): def auth_url(self): key, secret = self.get_key_and_secret() - self.shopify_api.Session.setup(api_key=key, secret=secret) + shopify.Session.setup(api_key=key, secret=secret) scope = self.get_scope() state = self.state_token() self.strategy.session_set(self.name + "_state", state) redirect_uri = self.get_redirect_uri(state) - session = self.shopify_api.Session( - self.data.get("shop").strip(), version=self.shopify_api_version + session = shopify.Session( + self.data.get("shop").strip(), version=shopify_version ) return session.create_permission_url(scope=scope, redirect_uri=redirect_uri) @@ -65,12 +58,12 @@ def auth_complete(self, *args, **kwargs): key, secret = self.get_key_and_secret() try: shop_url = self.data.get("shop") - self.shopify_api.Session.setup(api_key=key, secret=secret) - shopify_session = self.shopify_api.Session( - shop_url, version=self.shopify_api_version, token=self.data + shopify.Session.setup(api_key=key, secret=secret) + shopify_session = shopify.Session( + shop_url, version=shopify_version, token=self.data ) access_token = shopify_session.token - except self.shopify_api.ValidationException: + except shopify.ValidationException: raise AuthCanceled(self) else: if not access_token: