lib/fastly/api_client.rb in fastly-4.0.0.alpha4 vs lib/fastly/api_client.rb in fastly-4.0.0
- old
+ new
@@ -13,29 +13,40 @@
require 'logger'
require 'tempfile'
require 'time'
require 'typhoeus'
+# https://developer.fastly.com/reference/api/#rate-limiting
+DEFAULT_RATELIMIT = 1000
+
module Fastly
class ApiClient
# The Configuration object holding settings to be used in the API client.
attr_accessor :config
# Defines the headers to be used in HTTP requests of all API calls by default.
#
# @return [Hash]
attr_accessor :default_headers
+ # The last observed value of http header Fastly-RateLimit-Remaining
+ attr_reader :rate_limit_remaining
+
+ # The last observed value of http header Fastly-RateLimit-Reset
+ attr_reader :rate_limit_reset
+
# Initializes the ApiClient
# @option config [Configuration] Configuration for initializing the object, default to Configuration.default
def initialize(config = Configuration.default)
@config = config
@user_agent = "fastly-ruby/#{VERSION}"
@default_headers = {
'Content-Type' => 'application/json',
'User-Agent' => @user_agent
}
+ @rate_limit_remaining = DEFAULT_RATELIMIT
+ @rate_limit_reset = 0
end
def self.default
@@default ||= ApiClient.new
end
@@ -48,9 +59,14 @@
request = build_request(http_method, path, opts)
response = request.run
if @config.debugging
@config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
+ end
+
+ if http_method != "GET" && http_method != "HEAD"
+ @rate_limit_remaining = response.headers.fetch("fastly-ratelimit-remaining", DEFAULT_RATELIMIT).to_i
+ @rate_limit_reset = response.headers.fetch("fastly-ratelimit-reset", 0).to_i
end
unless response.success?
if response.timed_out?
fail ApiError.new('Connection timed out')