module EY
  class CLI
    class API < EY::API

      def initialize(token = nil)
        @token = token
        @token ||= self.class.read_token
        @token ||= self.class.fetch_token
        raise EY::Error, "Sorry, we couldn't get your API token." unless @token
      end

      def request(*)
        begin
          super
        rescue EY::API::InvalidCredentials
          EY.ui.warn "Credentials rejected, please authenticate again"
          refresh
          retry
        end
      end

      def refresh
        @token = self.class.fetch_token
      end

      def self.fetch_token
        EY.ui.info("We need to fetch your API token, please login")
        begin
          email    = EY.ui.ask("Email: ")
          password = EY.ui.ask("Password: ", true)
          super(email, password)
        rescue EY::API::InvalidCredentials
          EY.ui.warn "Invalid username or password, please try again"
          retry
        end
      end

    end
  end
end
module EY
  class CLI
    class API < EY::API

      def initialize(token = nil)
        @token = token
        @token ||= self.class.read_token
        @token ||= self.class.fetch_token
        raise EY::Error, "Sorry, we couldn't get your API token." unless @token
      end

      def request(*)
        begin
          super
        rescue EY::API::InvalidCredentials
          EY.ui.warn "Credentials rejected, please authenticate again"
          refresh
          retry
        end
      end

      def refresh
        @token = self.class.fetch_token
      end

      def self.fetch_token
        EY.ui.warn("The engineyard gem is prerelease software. Please do not use")
        EY.ui.warn("this tool to deploy to mission-critical environments, yet.")
        EY.ui.info("We need to fetch your API token, please login")
        begin
          email    = EY.ui.ask("Email: ")
          password = EY.ui.ask("Password: ", true)
          super(email, password)
        rescue EY::API::InvalidCredentials
          EY.ui.warn "Invalid username or password, please try again"
          retry
        end
      end

    end
  end
end
Add prerelease warning while fetching API token
Add prerelease warning while fetching API token

Change-Id: I611ad411eaa5eaed90745a497a836b6a0531f5b4
Reviewed-on: http://review.engineyard.com/190
Reviewed-by: Brian Donovan <5c0e7f6634184a28b1cddd81dceb95d9113bf00f@engineyard.com>
Tested-by: Brian Donovan <5c0e7f6634184a28b1cddd81dceb95d9113bf00f@engineyard.com>
