Class: Aikido::Zen::APIClient
- Inherits:
-
Object
- Object
- Aikido::Zen::APIClient
- Defined in:
- lib/aikido/zen/api_client.rb
Overview
Implements all communication with the Aikido servers.
Instance Method Summary collapse
-
#can_make_requests? ⇒ Boolean
Whether we have a configured token.
-
#fetch_settings ⇒ Hash
Fetches the runtime settings from the server.
-
#initialize(config: Aikido::Zen.config, rate_limiter: Aikido::Zen::RateLimiter::Breaker.new, system_info: Aikido::Zen.system_info) ⇒ APIClient
constructor
A new instance of APIClient.
- #report(event) ⇒ Object
-
#should_fetch_settings?(last_updated_at = Aikido::Zen.runtime_settings.updated_at) ⇒ Boolean
Checks with the Aikido Runtime API the timestamp of the last settings update, and compares against the given value.
Constructor Details
#initialize(config: Aikido::Zen.config, rate_limiter: Aikido::Zen::RateLimiter::Breaker.new, system_info: Aikido::Zen.system_info) ⇒ APIClient
Returns a new instance of APIClient.
9 10 11 12 13 14 15 16 17 |
# File 'lib/aikido/zen/api_client.rb', line 9 def initialize( config: Aikido::Zen.config, rate_limiter: Aikido::Zen::RateLimiter::Breaker.new, system_info: Aikido::Zen.system_info ) @config = config @system_info = system_info @rate_limiter = rate_limiter end |
Instance Method Details
#can_make_requests? ⇒ Boolean
Returns whether we have a configured token.
20 21 22 |
# File 'lib/aikido/zen/api_client.rb', line 20 def can_make_requests? @config.api_token.to_s.size > 0 end |
#fetch_settings ⇒ Hash
Fetches the runtime settings from the server. In case of a timeout or other low-lever error, the request will be automatically retried up to two times, after which it will raise an error.
53 54 55 56 57 |
# File 'lib/aikido/zen/api_client.rb', line 53 def fetch_settings @config.logger.debug("Fetching new runtime settings") request(Net::HTTP::Get.new("/api/runtime/config", default_headers)) end |
#report(event) ⇒ void #report(settings_updating_event) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/aikido/zen/api_client.rb', line 74 def report(event) if @rate_limiter.throttle?(event) @config.logger.error("Not reporting #{event.type.upcase} event due to rate limiting") return end @config.logger.debug("Reporting #{event.type.upcase} event") req = Net::HTTP::Post.new("/api/runtime/events", default_headers) req.content_type = "application/json" req.body = @config.json_encoder.call(event.as_json) request(req) rescue Aikido::Zen::RateLimitedError @rate_limiter.open! raise end |
#should_fetch_settings?(last_updated_at = Aikido::Zen.runtime_settings.updated_at) ⇒ Boolean
Checks with the Aikido Runtime API the timestamp of the last settings update, and compares against the given value.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/aikido/zen/api_client.rb', line 31 def should_fetch_settings?(last_updated_at = Aikido::Zen.runtime_settings.updated_at) @config.logger.debug("Polling for new runtime settings to fetch") return false unless can_make_requests? return true if last_updated_at.nil? response = request( Net::HTTP::Get.new("/config", default_headers), base_url: @config.runtime_api_base_url ) new_updated_at = Time.at(response["configUpdatedAt"].to_i / 1000) new_updated_at > last_updated_at end |