如何处理 Shopify API 与 Shopify gem 的连接? [英] How to handle Shopify API connection with Shopify gem?
问题描述
您好,我正在 Shopify 应用中使用 Shopify gem,我正在寻找有关如何处理与 Shopify 的 API 连接的建议.
Hi I'm using the Shopify gem in my Shopify app and I'm looking for suggestions on how to handle the API connection to Shopify.
我正在使用 webhooks 和 delay_jobs,所以我需要一种方法来打开控制器外部的连接.
I'm using webhooks and delayed_jobs so I need a way to open the connection outside of the controller.
目前我将此方法添加到我的 Shop 模型中:
At the moment I added this method to my Shop model:
def connect_to_store
session = ShopifyAPI::Session.new(self.url, self.access_token)
session.valid?
ShopifyAPI::Base.activate_session(session)
end
所以我可以很容易的打开连接,例如:
So I can open the connection very easily, for example:
Shop.find(1).connect_to_store
ShopifyAPI::Shop.current.name
问题是,在我的 Product 模块中,我需要在多个方法中打开连接,但我最终多次调用 connect_to_store 方法,我担心在没有真正需要的情况下打开到同一个商店的多个连接.
The problem is that, inside my Product module, I need the connection open inside several methods but I end up calling the connect_to_store method several times and I'm worried about opening several connections to the same store, without a real need.
有没有办法检查一个连接是否已经打开,只有在没有找到另一个连接时才打开一个新连接?
Is there a way to check if a connection is already opened and open a new one only if another one is not found?
谢谢,奥古斯托
------------------- 更新-------------------
我更好地解释了我的问题.
I explain better my issue.
假设在我的产品模型中,我想查看给定产品的 compare_at_price 是否大于其价格,在这种情况下,我想向 Shopify 产品添加促销"标签.
Let's say that in my Product model I want to see if a given product has a compare_at_price greater than its price and, in this case, I want to add a "sale" tag to the Shopify product.
在我的产品模型中,我有:
In my Product model I have:
class Product < ActiveRecord::Base
belongs_to :shop
def get_from_shopify
self.shop.connect_to_store
@shopify_p = ShopifyAPI::Product.find(self.shopify_id)
end
def add_tag(tag)
@shopify_p = self.get_from_shopify
shopify_p_tags = shopify_p.tags.split(",")
shopify_p_tags.collect{|x| x.strip!}
unless shopify_p_tags.include?(tag)
shopify_p_tags << tag
shopify_p_tags.join(",")
shopify_p.tags = shopify_p_tags
shopify_p.save
end
end
def on_sale?
@shopify_p = self.get_from_shopify
sale = false
shopify_p.variants.each do |v|
unless v.compare_at_price.nil?
if v.compare_at_price > v.price
sale = true
end
end
end
return sale
end
def update_sale_tag
if self.on_sale?
self.add_tag("sale")
end
end
end
我的问题是,如果我打电话:
My problem is that if I call:
p.update_sale_tag
Shop.connect_to_store 被多次调用,我已经验证了几次,但我已经通过了验证.
the Shop.connect_to_store is called several times and I authenticate several times while I'm already authenticated.
你会如何重构这段代码?
How would you refactor this code?
推荐答案
我通过将 Shopify 返回的 OAuth 令牌存储在商店中来解决此问题(无论如何您都应该这样做).访问 API 所需要的只是令牌,因此在您的商店模型中,您将拥有如下方法:
I approach this by storing the OAuth token that is returned by Shopify with the store (you should be doing this anyway). All you need to access the API is the token, so in your shop model you would have a method like:
def shopify_api_path
"https://#{Rails.configuration.shopify_api_key}:#{self.shopify_token}@#{self.shopify_domain}/admin"
end
然后,如果您想访问延迟作业工作者中特定商店的 API,您只需:
Then if you want to access the API for a particular store in a Delayed Job worker, you would simply:
begin
ShopifyAPI::Base.site = shop.shopify_api_path
# Make whatever calls to the API that you want here.
products = ShopifyAPI::Product.all
ensure
ShopifyAPI::Base.site = nil
end
希望能有所帮助.我发现在控制器之外使用 Sessions 有点麻烦,特别是因为这很好而且很容易.
Hopefully that helps a little. I find working with Sessions outside of controllers to be a bit messy, particularly since this is nice and easy.
这篇关于如何处理 Shopify API 与 Shopify gem 的连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!