我开发了一个应用程序,允许我们的客户创建自己的会员保护网站.然后,我的应用程序连接到外部API服务(客户特定的api_key/api_url),以同步/更新/添加数据到此其他服务.好吧,我已经为这个其他服务编写了一个API包装器.但是,我现在看到连接为零的随机丢弃.以下是我目前使用连接的方式:
我有一个xml/rpc连接类
class ApiConnection
attr_accessor :api_url, :api_key, :retry_count
def initialize(url, key)
@api_url = url
@api_key = key
@retry_count = 1
end
def api_perform(class_type, method, *args)
server = XMLRPC::Client.new3({'host' => @api_url, 'path' => "/api/xmlrpc", 'port' => 443, 'use_ssl' => true})
result = server.call("#{class_type}.#{method}", @api_key, *args)
return result
end
end
Run Code Online (Sandbox Code Playgroud)
我还有一个模块可以包含在我的模型中以访问和调用api方法
module ApiService
# Set account specific ApiConnection obj
def self.set_account_api_conn(url, key)
if ac = Thread.current[:api_conn]
ac.api_url, ac.api_key = url, key
else
Thread.current[:api_conn] = ApiConnection.new(url, key)
end
end
########################
### Email Service …Run Code Online (Sandbox Code Playgroud)