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_runtime_config ⇒ Hash
Fetches the runtime config from the server.
- #fetch_runtime_firewall_lists ⇒ Object
-
#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.
11 12 13 14 15 16 17 18 19 |
# File 'lib/aikido/zen/api_client.rb', line 11 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.
22 23 24 |
# File 'lib/aikido/zen/api_client.rb', line 22 def can_make_requests? @config.api_token.to_s.size > 0 end |
#fetch_runtime_config ⇒ Hash
Fetches the runtime config 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.
55 56 57 58 59 |
# File 'lib/aikido/zen/api_client.rb', line 55 def fetch_runtime_config @config.logger.debug("Fetching new runtime config") request(Net::HTTP::Get.new("/api/runtime/config", default_headers)) end |
#fetch_runtime_firewall_lists ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/aikido/zen/api_client.rb', line 61 def fetch_runtime_firewall_lists @config.logger.debug("Fetching new runtime firewall lists") headers = default_headers.merge({ "Accept-Encoding" => "gzip" }) request(Net::HTTP::Get.new("/api/runtime/firewall/lists", headers)) end |
#report(event) ⇒ void #report(settings_updating_event) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/aikido/zen/api_client.rb', line 86 def report(event) event_type = if event.respond_to?(:type) event.type else event[:type] end if @rate_limiter.throttle?(event_type) @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 = if event.respond_to?(:as_json) @config.json_encoder.call(event.as_json) else @config.json_encoder.call(event) end 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.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/aikido/zen/api_client.rb', line 33 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.realtime_endpoint ) new_updated_at = Time.at(response["configUpdatedAt"].to_i / 1000) new_updated_at > last_updated_at end |