Shopify rails 应用从具有商店域的 SessionRepository 检索会话
Shopify rails app retrieve session from SessionRepository with shop domain
我正在尝试通过在我的控制器中创建一个新会话来创建一个代理应用程序。代理控制器有一个参数:商店的 myshopify.com 域。使用它,我想从我的会话存储库中检索该商店的会话并实例化该会话。
这就是我的代码现在的样子
class ProxyController < ActionController::Base
def index
shop_domain = params[:shop]
#puts ShopifyApp::SessionRepository.methods.sort
shop = ShopifyApp::SessionRepository.retrieve(shop_domain)
ShopifyAPI::Base.activate_session(shop)
这是ShopifyApp::SessionRepositoryclass
module ShopifyApp
class SessionRepository
class ConfigurationError < StandardError; end
class << self
def storage=(storage)
@storage = storage
unless storage.nil? || self.storage.respond_to?(:store) && self.storage.respond_to?(:retrieve)
raise ArgumentError, "storage must respond to :store and :retrieve"
end
end
def retrieve(id)
storage.retrieve(id)
end
def store(session)
storage.store(session)
end
def storage
load_storage || raise(ConfigurationError.new("ShopifySessionRepository.storage is not configured!"))
end
private
def load_storage
return unless @storage
@storage.respond_to?(:safe_constantize) ? @storage.safe_constantize : @storage
end
end
end
end
这是存储模块。
module ShopifyApp
module SessionStorage
extend ActiveSupport::Concern
class_methods do
def store(session)
shop = self.find_or_initialize_by(shopify_domain: session.url)
shop.shopify_token = session.token
shop.save!
shop.id
end
def retrieve(id)
return unless id
if shop = self.find_by(id: id)
ShopifyAPI::Session.new(shop.shopify_domain, shop.shopify_token)
end
end
end
end
end
所以检索需要我数据库中商店的 ID(如 3)而不是商店的域名(如 dev-store。myshopify.com)。
我正在寻找一种在我的代理控制器上 retrieve/create 会话的方法,通过修改商店模型以便我可以使用商店域检索它,或者通过任何其他方式创建会话允许我在我的代理控制器上使用 API 调用。
我知道这很老,但我之前遇到过这个问题。没有太多有用的解决方案。
这对我有用:
shop = Shop.find_by(shopify_domain: params[:shop])
shop = ShopifyApp::SessionRepository.retrieve(shop.id)
ShopifyAPI::Base.activate_session(shop)
您应该可以在数据库中按域找到您的商店。因此能够获取特定的 id。
我正在尝试通过在我的控制器中创建一个新会话来创建一个代理应用程序。代理控制器有一个参数:商店的 myshopify.com 域。使用它,我想从我的会话存储库中检索该商店的会话并实例化该会话。
这就是我的代码现在的样子
class ProxyController < ActionController::Base
def index
shop_domain = params[:shop]
#puts ShopifyApp::SessionRepository.methods.sort
shop = ShopifyApp::SessionRepository.retrieve(shop_domain)
ShopifyAPI::Base.activate_session(shop)
这是ShopifyApp::SessionRepositoryclass
module ShopifyApp
class SessionRepository
class ConfigurationError < StandardError; end
class << self
def storage=(storage)
@storage = storage
unless storage.nil? || self.storage.respond_to?(:store) && self.storage.respond_to?(:retrieve)
raise ArgumentError, "storage must respond to :store and :retrieve"
end
end
def retrieve(id)
storage.retrieve(id)
end
def store(session)
storage.store(session)
end
def storage
load_storage || raise(ConfigurationError.new("ShopifySessionRepository.storage is not configured!"))
end
private
def load_storage
return unless @storage
@storage.respond_to?(:safe_constantize) ? @storage.safe_constantize : @storage
end
end
end
end
这是存储模块。
module ShopifyApp
module SessionStorage
extend ActiveSupport::Concern
class_methods do
def store(session)
shop = self.find_or_initialize_by(shopify_domain: session.url)
shop.shopify_token = session.token
shop.save!
shop.id
end
def retrieve(id)
return unless id
if shop = self.find_by(id: id)
ShopifyAPI::Session.new(shop.shopify_domain, shop.shopify_token)
end
end
end
end
end
所以检索需要我数据库中商店的 ID(如 3)而不是商店的域名(如 dev-store。myshopify.com)。
我正在寻找一种在我的代理控制器上 retrieve/create 会话的方法,通过修改商店模型以便我可以使用商店域检索它,或者通过任何其他方式创建会话允许我在我的代理控制器上使用 API 调用。
我知道这很老,但我之前遇到过这个问题。没有太多有用的解决方案。
这对我有用:
shop = Shop.find_by(shopify_domain: params[:shop])
shop = ShopifyApp::SessionRepository.retrieve(shop.id)
ShopifyAPI::Base.activate_session(shop)
您应该可以在数据库中按域找到您的商店。因此能够获取特定的 id。