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) apis[name] = Session::Config::Api.new( key: key, host: host, version: version, mode: mode, root: self ) self 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 # you can create multiple apis under same root_name (same org) def active_root_name active_name.split("-").first end def active_api self["active-api"] end def service_up? api_klass = (active_api.version == :external)? Ecoportal::API::External : Ecoportal::API::Internal @api_test ||= api_klass.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), force_new: false) key = active_api.key host = active_api.host mode = active_api.mode version = active_api.version return @api if (!force_new) && @api && key == @key && host == @host && @mode == mode && version == @version api_klass = (version == :external)? Ecoportal::API::External : Ecoportal::API::Internal @api = api_klass.new(key, host: host, logger: logger) @key = key; @host = host; @mode = mode; @version = version @api end def new_api(logger = ::Logger.new(IO::NULL)) api(logger, force_new: true) end end end end end end