Skip to content
This repository
Browse code

allow username/password params to automagically lookup api_key

closes #30
  • Loading branch information...
commit 5097e263984176b2f8d842cc5159f6a613ad6cc4 1 parent 464a1cc
Wesley Beary authored October 30, 2012
42  lib/heroku/api.rb
@@ -36,24 +36,36 @@
36 36
 module Heroku
37 37
   class API
38 38
 
  39
+    HEADERS = {
  40
+      'Accept'                => 'application/json',
  41
+      'Accept-Encoding'       => 'gzip',
  42
+      #'Accept-Language'       => 'en-US, en;q=0.8',
  43
+      'User-Agent'            => "heroku-rb/#{Heroku::API::VERSION}",
  44
+      'X-Ruby-Version'        => RUBY_VERSION,
  45
+      'X-Ruby-Platform'       => RUBY_PLATFORM
  46
+    }
  47
+
  48
+    OPTIONS = {
  49
+      :headers  => {},
  50
+      :host     => 'api.heroku.com',
  51
+      :nonblock => false,
  52
+      :scheme   => 'https'
  53
+    }
  54
+
39 55
     def initialize(options={})
  56
+      options = OPTIONS.merge(options)
  57
+
40 58
       @api_key = options.delete(:api_key) || ENV['HEROKU_API_KEY']
  59
+      if !@api_key && options.has_key?(:username) && options.has_key?(:password)
  60
+        @connection = Excon.new("#{options[:scheme]}://#{options[:host]}", options.merge(:headers => HEADERS))
  61
+        @api_key = self.post_login(options[:username], options[:password]).body["api_key"]
  62
+      end
  63
+
41 64
       user_pass = ":#{@api_key}"
42  
-      options = {
43  
-        :headers  => {},
44  
-        :host     => 'api.heroku.com',
45  
-        :nonblock => false,
46  
-        :scheme   => 'https'
47  
-      }.merge(options)
48  
-      options[:headers] = {
49  
-        'Accept'                => 'application/json',
50  
-        'Accept-Encoding'       => 'gzip',
51  
-        #'Accept-Language'       => 'en-US, en;q=0.8',
52  
-        'Authorization'         => "Basic #{Base64.encode64(user_pass).gsub("\n", '')}",
53  
-        'User-Agent'            => "heroku-rb/#{Heroku::API::VERSION}",
54  
-        'X-Ruby-Version'        => RUBY_VERSION,
55  
-        'X-Ruby-Platform'       => RUBY_PLATFORM
56  
-      }.merge(options[:headers])
  65
+      options[:headers] = HEADERS.merge({
  66
+        'Authorization' => "Basic #{Base64.encode64(user_pass).gsub("\n", '')}",
  67
+      }).merge(options[:headers])
  68
+
57 69
       @connection = Excon.new("#{options[:scheme]}://#{options[:host]}", options)
58 70
     end
59 71
 
17  lib/heroku/api/mock.rb
@@ -137,15 +137,20 @@ def self.get_mock_processes(mock_data, app)
137 137
       end
138 138
 
139 139
       def self.parse_stub_params(params)
140  
-        api_key = Base64.decode64(params[:headers]['Authorization']).split(':').last
  140
+        mock_data = nil
141 141
 
142  
-        parsed = params.dup
143  
-        begin # try to JSON decode
144  
-          parsed[:body] &&= Heroku::API::OkJson.decode(parsed[:body])
145  
-        rescue # else leave as is
  142
+        if params[:headers].has_key?('Authorization')
  143
+          api_key = Base64.decode64(params[:headers]['Authorization']).split(':').last
  144
+
  145
+          parsed = params.dup
  146
+          begin # try to JSON decode
  147
+            parsed[:body] &&= Heroku::API::OkJson.decode(parsed[:body])
  148
+          rescue # else leave as is
  149
+          end
  150
+          mock_data = @mock_data[api_key]
146 151
         end
147 152
 
148  
-        [parsed, @mock_data[api_key]]
  153
+        [parsed, mock_data]
149 154
       end
150 155
 
151 156
       def self.remove_mock_app_addon(mock_data, app, addon)
8  test/test_login.rb
@@ -9,4 +9,12 @@ def test_post_login
9 9
     assert_equal(200, response.status)
10 10
   end
11 11
 
  12
+  def test_post_login_implied
  13
+    # FIXME: user/pass will only work in mock for now, maybe use ENV
  14
+    _heroku_ = Heroku::API.new(:mock => true, :username => 'email@example.com', :password => 'fake_password')
  15
+    response = _heroku_.get_apps
  16
+
  17
+    assert_equal(200, response.status)
  18
+  end
  19
+
12 20
 end

0 notes on commit 5097e26

Please sign in to comment.
Something went wrong with that request. Please try again.