Skip to content

Commit e4d3e80

Browse files
committed
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.
1 parent d3c7a76 commit e4d3e80

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ version = "4.5.4"
5353
[project.optional-dependencies]
5454
all = [
5555
"social-auth-core[saml]",
56-
"social-auth-core[azuread]"
56+
"social-auth-core[azuread]",
57+
"social-auth-core[shopify]"
5758
]
5859
allpy3 = [
5960
"social-auth-core[all]"
@@ -78,6 +79,9 @@ saml = [
7879
# pinned to 5.2 until a new wheel of xmlsec is released
7980
"lxml~=5.2.1"
8081
]
82+
shopify = [
83+
"ShopifyAPI"
84+
]
8185

8286
[project.urls]
8387
Homepage = "https://github.com/python-social-auth/social-core"

social_core/backends/shopify.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
https://python-social-auth.readthedocs.io/en/latest/backends/shopify.html
44
"""
55

6-
import imp # type: ignore[reportMissingImports]
6+
import shopify
77

88
from ..exceptions import AuthCanceled, AuthFailed
99
from ..utils import handle_http_errors
@@ -22,13 +22,6 @@ class ShopifyOAuth2(BaseOAuth2):
2222
def shopify_api_version(self):
2323
return self.setting("API_VERSION", "2020-10")
2424

25-
@property
26-
def shopify_api(self):
27-
if not hasattr(self, "_shopify_api"):
28-
fp, pathname, description = imp.find_module("shopify")
29-
self._shopify_api = imp.load_module("shopify", fp, pathname, description)
30-
return self._shopify_api
31-
3225
def get_user_details(self, response):
3326
"""Use the shopify store name as the username"""
3427
return {"username": str(response.get("shop", "")).replace(".myshopify.com", "")}
@@ -37,7 +30,7 @@ def extra_data(self, user, uid, response, details=None, *args, **kwargs):
3730
"""Return access_token and extra defined names to store in
3831
extra_data field"""
3932
data = super().extra_data(user, uid, response, details, *args, **kwargs)
40-
session = self.shopify_api.Session(
33+
session = shopify.Session(
4134
self.data.get("shop").strip(), version=self.shopify_api_version
4235
)
4336
# Get, and store the permanent token
@@ -47,12 +40,12 @@ def extra_data(self, user, uid, response, details=None, *args, **kwargs):
4740

4841
def auth_url(self):
4942
key, secret = self.get_key_and_secret()
50-
self.shopify_api.Session.setup(api_key=key, secret=secret)
43+
shopify.Session.setup(api_key=key, secret=secret)
5144
scope = self.get_scope()
5245
state = self.state_token()
5346
self.strategy.session_set(self.name + "_state", state)
5447
redirect_uri = self.get_redirect_uri(state)
55-
session = self.shopify_api.Session(
48+
session = shopify.Session(
5649
self.data.get("shop").strip(), version=self.shopify_api_version
5750
)
5851
return session.create_permission_url(scope=scope, redirect_uri=redirect_uri)
@@ -65,12 +58,12 @@ def auth_complete(self, *args, **kwargs):
6558
key, secret = self.get_key_and_secret()
6659
try:
6760
shop_url = self.data.get("shop")
68-
self.shopify_api.Session.setup(api_key=key, secret=secret)
69-
shopify_session = self.shopify_api.Session(
61+
shopify.Session.setup(api_key=key, secret=secret)
62+
shopify_session = shopify.Session(
7063
shop_url, version=self.shopify_api_version, token=self.data
7164
)
7265
access_token = shopify_session.token
73-
except self.shopify_api.ValidationException:
66+
except shopify.ValidationException:
7467
raise AuthCanceled(self)
7568
else:
7669
if not access_token:

0 commit comments

Comments
 (0)