【问题标题】:Shopify rails app retrieve session from SessionRepository with shop domainShopify rails 应用程序从带有商店域的 SessionRepository 检索会话
【发布时间】:2016-10-24 16:46:10
【问题描述】:

我正在尝试通过在我的控制器中创建一个新会话来创建一个代理应用程序。代理控制器有一个参数:商店的 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::SessionRepository 类

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

因此,retrieve 需要我的数据库中商店的 ID(如 3)而不是商店的域(如 dev-store.myshopify.com)。

我正在寻找一种在我的代理控制器上检索/创建会话的方法,通过修改 Shop 模型以便我可以使用商店域检索它,或者通过任何其他方式来创建允许我使用的会话我的代理控制器上的 API 调用。

【问题讨论】:

  • 我应该指出 ShopifyApp::SessionRepository.retrieve(1) 确实有效,并且在这种情况下 API 调用成功。这似乎与此问题重复:stackoverflow.com/questions/32147181/… 但该问题的公认解决方案不适用于应用程序代理控制器。

标签: ruby-on-rails shopify


【解决方案1】:

我知道这很旧,但我之前遇到过这个问题。有用的解决方案并不多。

这对我有用:

shop = Shop.find_by(shopify_domain: params[:shop])
shop = ShopifyApp::SessionRepository.retrieve(shop.id)
ShopifyAPI::Base.activate_session(shop)

您应该能够在数据库中按域找到您的商店。因此能够获取特定的 id。

【讨论】:

  • 这正是我当时所做的:)
猜你喜欢
  • 2018-05-06
  • 1970-01-01
  • 2017-09-15
  • 1970-01-01
  • 2020-09-26
  • 2016-11-05
  • 2020-05-15
  • 2015-11-22
  • 1970-01-01
相关资源
最近更新 更多