module Eco module API class Session class Config class Apis < BaseConfig def apis self["apis"] ||= {} end def apis? apis.keys.length > 0 end def enviros apis.keys end def defined?(name) apis.key?(name) end def any_defined?(*names) [names].flatten.any? do |name| self.defined?(name) end end def add(name, key:, host:, version: :internal, mode: :local, user_key: nil, external_key: nil) apis[name] = Session::Config::Api.new( name, key: key, host: host, version: version, mode: mode, root: self, user_key: user_key, external_key: external_key ) self end def active_api self["active-api"] end def active_name=(name) raise "'#{name}' Api environment not defined" if !apis[name] self["active-name"] = name self["active-api"] = apis[name] self end def active_name self["active-name"] end # the active environment def active_root_name active_name end def service_up? @api_test ||= Session::Config::Api.api_class(active_api.version).new("foobar", host: active_api.host, logger: ::Logger.new(IO::NULL)) status = @api_test.client.get("/policy_groups").status # 401 Unauthorized "Permission denied. API key may be invalid." status == 401 end def api(logger = ::Logger.new(IO::NULL), version: nil) unless active_api raise "There's no 'active_api'. Use apis.active_name='api_enviro_name' to set the active api" end active_api.api(version: version, logger: logger) end def default_user_key=(key) self["user_key"] = key end def default_user_key self["user_key"] end end end end end end