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.
- #should_fetch_settings_endpoint=(uri) ⇒ Object
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 20 21 |
# 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 @should_fetch_settings_endpoint = @config.realtime_endpoint end |
Instance Method Details
#can_make_requests? ⇒ Boolean
Returns whether we have a configured token.
28 29 30 |
# File 'lib/aikido/zen/api_client.rb', line 28 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.
61 62 63 64 65 |
# File 'lib/aikido/zen/api_client.rb', line 61 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
67 68 69 70 71 72 73 74 75 |
# File 'lib/aikido/zen/api_client.rb', line 67 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
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/aikido/zen/api_client.rb', line 92 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.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/aikido/zen/api_client.rb', line 39 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: @should_fetch_settings_endpoint ) new_updated_at = Time.at(response["configUpdatedAt"].to_i) new_updated_at > last_updated_at end |
#should_fetch_settings_endpoint=(uri) ⇒ Object
23 24 25 |
# File 'lib/aikido/zen/api_client.rb', line 23 def should_fetch_settings_endpoint=(uri) @should_fetch_settings_endpoint = URI(uri) end |