ruby RestClient 每个请求设置
ruby RestClient per request settings
我非常喜欢 RestClient API 但在我看来我无法为每个请求配置代理、超时、请求挂钩、ssl 等。因此,例如,如果我想在不同线程中同时执行一些请求,它们可能会相互干扰,因为我更改了一些配置。
我错过了什么吗?有没有解决方法,因为在我看来这是一个严重的限制。
更新:
其实看Request class我觉得只有代理配置和before_execution_procs
是全局配置。 before_execution_procs
很可能会有解决方法。如果您知道如何根据请求设置这些,我将不胜感激。
示例:
(1..10).each {
Thread.new {
RestClient.get(..., proxy: "some proxy", before_execution_hooks: [some, array, of, hooks])
}
}
如果我这样做:
(1..10).each {
Thread.new {
RestClient.proxy = "per request proxy"
RestClient.add_before_execution_proc {...}
RestClient.get(...)
}
}
然后我将以每个请求以及多个过程的未知代理结束。
您现在可以根据请求选项设置代理
With RestClient::Request or with RestClient::Resource 具有覆盖全局设置的 :proxy 和 :before_execution_proc 选项:
所以下面的代码应该是线程安全的:
(1..10).each do
Thread.new do
RestClient::Resource.new(url, proxy: 'resquest specific', before_execution_proc: ...).get
# or
RestClient::Request.execute(method: :get, url: 'http://...', proxy: ...)
end
end
我非常喜欢 RestClient API 但在我看来我无法为每个请求配置代理、超时、请求挂钩、ssl 等。因此,例如,如果我想在不同线程中同时执行一些请求,它们可能会相互干扰,因为我更改了一些配置。
我错过了什么吗?有没有解决方法,因为在我看来这是一个严重的限制。
更新:
其实看Request class我觉得只有代理配置和before_execution_procs
是全局配置。 before_execution_procs
很可能会有解决方法。如果您知道如何根据请求设置这些,我将不胜感激。
示例:
(1..10).each {
Thread.new {
RestClient.get(..., proxy: "some proxy", before_execution_hooks: [some, array, of, hooks])
}
}
如果我这样做:
(1..10).each {
Thread.new {
RestClient.proxy = "per request proxy"
RestClient.add_before_execution_proc {...}
RestClient.get(...)
}
}
然后我将以每个请求以及多个过程的未知代理结束。
您现在可以根据请求选项设置代理
With RestClient::Request or with RestClient::Resource 具有覆盖全局设置的 :proxy 和 :before_execution_proc 选项:
所以下面的代码应该是线程安全的:
(1..10).each do
Thread.new do
RestClient::Resource.new(url, proxy: 'resquest specific', before_execution_proc: ...).get
# or
RestClient::Request.execute(method: :get, url: 'http://...', proxy: ...)
end
end