Permalink
Browse files

Add oauth connection to API

  • Loading branch information...
xdmnl committed Aug 31, 2012
1 parent 34028d8 commit a72e6c8d4a25e95d386b14f0bf5129e67daa73ef
Showing with 32 additions and 9 deletions.
  1. +6 −0 lib/basecamp/active_resource.rb
  2. +24 −7 lib/basecamp/base.rb
  3. +2 −2 lib/basecamp/connection.rb
@@ -6,6 +6,8 @@
# => "200 OK"
class ActiveResource::Connection
alias_method :original_handle_response, :handle_response
+ alias :static_default_header :default_header
+
def handle_response(response)
Thread.current[:active_resource_connection_headers] = response
original_handle_response(response)
@@ -14,4 +16,8 @@ def handle_response(response)
def response
Thread.current[:active_resource_connection_headers]
end
+
+ def set_header(key, value)
+ default_header.update(key => value)
+ end
end
View
@@ -1,14 +1,15 @@
module Basecamp
class << self
attr_accessor :use_xml
- attr_reader :site, :user, :password, :use_ssl
+ attr_reader :site, :user, :password, :use_ssl, :use_oauth, :access_token
def establish_connection!(site, user, password, use_ssl = false, use_xml = true)
- @site = site
- @user = user
- @password = password
- @use_ssl = use_ssl
- @use_xml = use_xml
+ @site = site
+ @user = user
+ @password = password
+ @use_ssl = use_ssl
+ @use_xml = use_xml
+ @use_oauth = false
Resource.user = user
Resource.password = password
@@ -18,14 +19,30 @@ def establish_connection!(site, user, password, use_ssl = false, use_xml = true)
@connection = Connection.new(self)
end
+ def establish_oauth_connection!(site, access_token, use_ssl = false, use_xml = true)
+ @site = site
+ @use_ssl = use_ssl
+ @use_xml = use_xml
+ @use_oauth = true
+ @access_token = access_token
+
+ Resource.site = (use_ssl ? "https" : "http") + "://" + site
+ Resource.format = (use_xml ? :xml : :json)
+ Resource.connection.set_header('Authorization', "Bearer #{access_token}")
+
+ @connection = Connection.new(self)
+ end
+
def connection
@connection || raise('No connection established')
end
# Make a raw web-service request to Basecamp. This will return a Hash of
# Arrays of the response, and may seem a little odd to the uninitiated.
def request(path, parameters = {})
- response = Basecamp.connection.post(path, StringIO.new(convert_body(parameters)), "Content-Type" => content_type)
+ headers = {'Content-Type' => content_type}
+ headers.merge!('Authorization' => "Bearer #{@access_token}") if @use_oauth
+ response = Basecamp.connection.post(path, StringIO.new(convert_body(parameters)), headers)
if response.code.to_i / 100 == 2
return {} if response.body.blank?
@@ -8,15 +8,15 @@ def initialize(master)
def post(path, iostream, headers = {})
request = Net::HTTP::Post.new(path, headers.merge('Accept' => 'application/xml'))
- request.basic_auth(@master.user, @master.password)
+ request.basic_auth(@master.user, @master.password) unless @master.use_oauth
request.body_stream = iostream
request.content_length = iostream.size
@connection.request(request)
end
def get(path, headers = {})
request = Net::HTTP::Get.new(path, headers.merge('Accept' => 'application/xml'))
- request.basic_auth(@master.user, @master.password)
+ request.basic_auth(@master.user, @master.password) unless @master.use_oauth
@connection.request(request)
end
end; end

0 comments on commit a72e6c8

Please sign in to comment.