Permalink
Browse files

allow username/password params to automagically lookup api_key

closes #30
  • Loading branch information...
geemus committed Oct 30, 2012
1 parent 464a1cc commit 5097e263984176b2f8d842cc5159f6a613ad6cc4
Showing with 46 additions and 21 deletions.
  1. +27 −15 lib/heroku/api.rb
  2. +11 −6 lib/heroku/api/mock.rb
  3. +8 −0 test/test_login.rb
View
@@ -36,24 +36,36 @@
module Heroku
class API
+ HEADERS = {
+ 'Accept' => 'application/json',
+ 'Accept-Encoding' => 'gzip',
+ #'Accept-Language' => 'en-US, en;q=0.8',
+ 'User-Agent' => "heroku-rb/#{Heroku::API::VERSION}",
+ 'X-Ruby-Version' => RUBY_VERSION,
+ 'X-Ruby-Platform' => RUBY_PLATFORM
+ }
+
+ OPTIONS = {
+ :headers => {},
+ :host => 'api.heroku.com',
+ :nonblock => false,
+ :scheme => 'https'
+ }
+
def initialize(options={})
+ options = OPTIONS.merge(options)
+
@api_key = options.delete(:api_key) || ENV['HEROKU_API_KEY']
+ if !@api_key && options.has_key?(:username) && options.has_key?(:password)
+ @connection = Excon.new("#{options[:scheme]}://#{options[:host]}", options.merge(:headers => HEADERS))
+ @api_key = self.post_login(options[:username], options[:password]).body["api_key"]
+ end
+
user_pass = ":#{@api_key}"
- options = {
- :headers => {},
- :host => 'api.heroku.com',
- :nonblock => false,
- :scheme => 'https'
- }.merge(options)
- options[:headers] = {
- 'Accept' => 'application/json',
- 'Accept-Encoding' => 'gzip',
- #'Accept-Language' => 'en-US, en;q=0.8',
- 'Authorization' => "Basic #{Base64.encode64(user_pass).gsub("\n", '')}",
- 'User-Agent' => "heroku-rb/#{Heroku::API::VERSION}",
- 'X-Ruby-Version' => RUBY_VERSION,
- 'X-Ruby-Platform' => RUBY_PLATFORM
- }.merge(options[:headers])
+ options[:headers] = HEADERS.merge({
+ 'Authorization' => "Basic #{Base64.encode64(user_pass).gsub("\n", '')}",
+ }).merge(options[:headers])
+
@connection = Excon.new("#{options[:scheme]}://#{options[:host]}", options)
end
View
@@ -137,15 +137,20 @@ def self.get_mock_processes(mock_data, app)
end
def self.parse_stub_params(params)
- api_key = Base64.decode64(params[:headers]['Authorization']).split(':').last
+ mock_data = nil
- parsed = params.dup
- begin # try to JSON decode
- parsed[:body] &&= Heroku::API::OkJson.decode(parsed[:body])
- rescue # else leave as is
+ if params[:headers].has_key?('Authorization')
+ api_key = Base64.decode64(params[:headers]['Authorization']).split(':').last
+
+ parsed = params.dup
+ begin # try to JSON decode
+ parsed[:body] &&= Heroku::API::OkJson.decode(parsed[:body])
+ rescue # else leave as is
+ end
+ mock_data = @mock_data[api_key]
end
- [parsed, @mock_data[api_key]]
+ [parsed, mock_data]
end
def self.remove_mock_app_addon(mock_data, app, addon)
View
@@ -9,4 +9,12 @@ def test_post_login
assert_equal(200, response.status)
end
+ def test_post_login_implied
+ # FIXME: user/pass will only work in mock for now, maybe use ENV
+ _heroku_ = Heroku::API.new(:mock => true, :username => 'email@example.com', :password => 'fake_password')
+ response = _heroku_.get_apps
+
+ assert_equal(200, response.status)
+ end
+
end

0 comments on commit 5097e26

Please sign in to comment.