Skip to content

Commit

Permalink
fix: import shopify API directly
Browse files Browse the repository at this point in the history
There is no need to delay importing and make it crash at usage time.
Also add it to optional deps.
  • Loading branch information
nijel committed Jan 31, 2025
1 parent d3c7a76 commit 1f70240
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ version = "4.5.4"
[project.optional-dependencies]
all = [
"social-auth-core[saml]",
"social-auth-core[azuread]"
"social-auth-core[azuread]",
"social-auth-core[shopify]"
]
allpy3 = [
"social-auth-core[all]"
Expand 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"
Expand Down
21 changes: 7 additions & 14 deletions social_core/backends/shopify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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", "")}
Expand All @@ -37,7 +30,7 @@ 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(
session = shopify.Session(
self.data.get("shop").strip(), version=self.shopify_api_version
)
# Get, and store the permanent token
Expand All @@ -47,12 +40,12 @@ 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(
session = shopify.Session(
self.data.get("shop").strip(), version=self.shopify_api_version
)
return session.create_permission_url(scope=scope, redirect_uri=redirect_uri)
Expand All @@ -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(
shopify.Session.setup(api_key=key, secret=secret)
shopify_session = shopify.Session(
shop_url, version=self.shopify_api_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:
Expand Down

0 comments on commit 1f70240

Please sign in to comment.