module Eco module API class Session class Config class Apis < BaseConfig class UndefinedApi < ArgumentError; end require_relative 'apis/space_helpers' require_relative 'apis/enviro_spaces' require_relative 'apis/one_off' require_relative 'apis/service_up' include EnviroSpaces include OneOff include ServiceUp def add( name, key:, host:, space: nil, version: :internal, mode: :local, user_key: nil, external_key: nil, email: nil, pass: nil, org_id: nil ) # ensure that space_option isn't used space = to_space(space) msg = "WARNING: Overriding #{full_name(name, space: space)} API credentials" puts msg if self.defined?(name, space: space) apis(space)[name] = Session::Config::Api.new( name, space: space, key: key, host: host, version: version, mode: mode, root: self, user_key: user_key, external_key: external_key, email: email, pass: pass, org_id: org_id ) self end # the active environment def active_root_name active_name end def active_name self['active-name'] end def active_api self['active-api'] end def active_name=(name) set_active_name(name) end def set_active_name(name, space: space_option) space ||= space_option msg = missing_api_message(name, space: space) raise UndefinedApi, msg unless self.api?(name, space: space) self['active-name'] = name self['active-api'] = apis(space)[name] set_options_space!(active_space) self end def api(logger = ::Logger.new(IO::NULL), version: nil) msg = "There's no 'active_api'. " msg << "To set the target environment, please use either of:\n" msg << " * apis.set_active_name('api_enviro_name', space: 'the-space')\n" msg << " * apis.active_name='api_enviro_name'\n" raise msg unless active_api 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 def default_email=(email) self['default_email'] = email end def default_email self['default_email'] || ENV['USER_EMAIL'] end def default_pass=(pass) self['default_pass'] = pass end def default_pass self['default_pass'] || ENV['USER_PASS'] end # Method to support CLI one-off API requests def one_off return unless one_off? name = one_off_org add( name, key: one_off_key, host: "#{one_off_enviro}.ecoportal.com" ) name end end end end end end