我正在使用helpscout
ruby gem并尝试检查请求限制何时低于某个数字(即剩余2个请求),并在剩余时间间隔内休眠循环以重置速率限制。
发出请求时,是否可以通过API访问响应 header ?
https://developer.helpscout.com/help-desk-api/#basic-rate-limiting
X-RateLimit-Interval-* Length of the rate limiting interval in seconds
X-RateLimit-Limit-* Maximum number of requests per interval
X-RateLimit-Remaining-* Number of requests remaining in the current rate limit interval
对讲(https://developers.intercom.com/reference#rate-limiting)允许您检查rate_limit_details并返回 header ,但我找不到帮助侦察员的任何内容或了解如何访问它们。
intercom.rate_limit_details
#=> {:limit=>180, :remaining=>179, :reset_at=>2014-10-07 14:58:00 +0100}
最佳答案
问题是helpscout
gem无法捕获该信息。如果您看一下源代码
https://github.com/hramos/helpscout/blob/db8da936853c8df694186ab11100d4482f74d302/lib/helpscout/models.rb#L44
# Error Envelope
class ErrorEnvelope
attr_reader :status, :message
# Creates a new ErrorEnvelope object from a Hash of attributes
def initialize(object)
@status = object["status"]
@message = object["message"]
end
end
发生错误时,它们仅捕获
status
和message
。如果要捕获其他 header 值,则可以增强下面的类 # Error Envelope
class ErrorEnvelope
attr_reader :status, :message, :limit
# Creates a new ErrorEnvelope object from a Hash of attributes
def initialize(object)
@status = object["status"]
@message = object["message"]
@limit = object["header"]["X-RateLimit-...."]
end
end
但是,这只会在出现错误时告诉您限制。您可以进一步增强库,以捕获每个调用的这些限制。您将需要修改
client.rb
https://github.com/hramos/helpscout/blob/2449bc2604667edfca5ed934c8e61cd129b17af5/lib/helpscout/client.rb
module HelpScout
class Client
include HTTParty
@@last_headers
def self.get(*more)
response = HTTParty.get(*more)
@@last_headers = response.headers
return response
end
def self.last_headers
@@last_headers
end
....
....
end
因此,执行
HelpScout.last_headers
将为您提供上次响应的 header ,然后您可以从同一响应中捕获所需的任何字段关于Ruby-用于速率限制的访问响应 header (帮助侦察器),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48737300/