Skip to content

Commit

Permalink
Added option to configure header globally
Browse files Browse the repository at this point in the history
  • Loading branch information
plribeiro3000 committed Jun 13, 2013
1 parent cf43116 commit ea50f5b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 13 deletions.
15 changes: 15 additions & 0 deletions lib/api-client/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,20 @@ def path=(path)
path = "#{path}/" unless path[path.size - 1, 1] == '/'
@path = path
end

# Return the default header for requisitions.
#
# @return [Hash] the default header.
def header
return { 'Content-Type' => 'application/json' } unless @header
@header
end

# Set the default header for requisitions.
#
# @param [Hash] header the default header for requitions.
def header=(header = {})
@header = { 'Content-Type' => 'application/json' }.merge(header)
end
end
end
8 changes: 4 additions & 4 deletions lib/api-client/dispatcher/net-http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module ApiClient::Dispatcher::NetHttp
# @return [HTTP] the response object.
def self.get(url, header = {})
initialize_connection(url)
call { @http.get(@uri.request_uri, { 'Content-Type' => 'application/json' }.merge(header)) }
call { @http.get(@uri.request_uri, ApiClient.config.header.merge(header)) }
end

# Make a post request and returns it.
Expand All @@ -21,7 +21,7 @@ def self.get(url, header = {})
# @return [HTTP] the response object.
def self.post(url, args, header = {})
initialize_connection(url)
call { @http.post(@uri.request_uri, args.to_json, { 'Content-Type' => 'application/json' }.merge(header)) }
call { @http.post(@uri.request_uri, args.to_json, ApiClient.config.header.merge(header)) }
end

# Make a put request and returns it.
Expand All @@ -32,7 +32,7 @@ def self.post(url, args, header = {})
# @return [HTTP] the response object.
def self.put(url, args, header = {})
initialize_connection(url)
call { @http.put(@uri.request_uri, args.to_json, { 'Content-Type' => 'application/json' }.merge(header)) }
call { @http.put(@uri.request_uri, args.to_json, ApiClient.config.header.merge(header)) }
end

# Make a patch request and returns it.
Expand All @@ -43,7 +43,7 @@ def self.put(url, args, header = {})
# @return [HTTP] the response object.
def self.patch(url, args, header = {})
initialize_connection(url)
call { @http.patch(@uri.request_uri, args.to_json, { 'Content-Type' => 'application/json' }.merge(header)) }
call { @http.patch(@uri.request_uri, args.to_json, ApiClient.config.header.merge(header)) }
end

# Make a delete request and returns it.
Expand Down
10 changes: 5 additions & 5 deletions lib/api-client/dispatcher/typhoeus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module ApiClient::Dispatcher::Typhoeus
# @param [Hash] header attributes of the request.
# @return [Typhoeus::Request] the response object.
def self.get(url, header = {})
::Typhoeus::Request.get(url, :headers => header)
::Typhoeus::Request.get(url, :headers => ApiClient.config.header.merge(header))
end

# Make a post request and returns it.
Expand All @@ -18,7 +18,7 @@ def self.get(url, header = {})
# @param [Hash] header attributes of the request.
# @return [Typhoeus::Request] the response object.
def self.post(url, args, header = {})
::Typhoeus::Request.post(url, :params => args, :headers => header)
::Typhoeus::Request.post(url, :params => args, :headers => ApiClient.config.header.merge(header))
end

# Make a put request and returns it.
Expand All @@ -28,7 +28,7 @@ def self.post(url, args, header = {})
# @param [Hash] header attributes of the request.
# @return [Typhoeus::Request] the response object.
def self.put(url, args, header = {})
::Typhoeus::Request.put(url, :params => args, :headers => header)
::Typhoeus::Request.put(url, :params => args, :headers => ApiClient.config.header.merge(header))
end

# Make a patch request and returns it.
Expand All @@ -38,7 +38,7 @@ def self.put(url, args, header = {})
# @param [Hash] header attributes of the request.
# @return [Typhoeus::Request] the response object.
def self.patch(url, args, header = {})
::Typhoeus::Request.patch(url, :params => args, :headers => header)
::Typhoeus::Request.patch(url, :params => args, :headers => ApiClient.config.header.merge(header))
end

# Make a delete request and returns it.
Expand All @@ -47,6 +47,6 @@ def self.patch(url, args, header = {})
# @param [Hash] header attributes of the request.
# @return [Typhoeus::Request] the response object.
def self.delete(url, header = {})
::Typhoeus::Request.delete(url, :headers => header)
::Typhoeus::Request.delete(url, :headers => ApiClient.config.header.merge(header))
end
end
38 changes: 34 additions & 4 deletions spec/api-client/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe ApiClient::Configuration do
describe '#path' do
describe 'when not configured' do
context 'when not configured' do
before :each do
ApiClient.configure do |config|
config.path = ''
Expand All @@ -14,7 +14,7 @@
end
end

describe 'when properly configured' do
context 'when properly configured' do
before :each do
ApiClient.configure do |config|
config.path = 'http://api.example.com'
Expand All @@ -28,7 +28,7 @@
end

describe '#path=' do
describe "with a string without '/'" do
context "with a string without '/'" do
before :each do
ApiClient.config.path = 'http://api.example.com'
end
Expand All @@ -38,7 +38,7 @@
end
end

describe "with a string with '/'" do
context "with a string with '/'" do
before :each do
ApiClient.config.path = 'http://api.example.com/'
end
Expand All @@ -48,4 +48,34 @@
end
end
end

describe '#header' do
context 'when not configured' do
it 'should return a hash with configs for content_type only' do
ApiClient.config.header.should == { 'Content-Type' => 'application/json' }
end
end

context 'when configured' do
before :each do
ApiClient.config.instance_variable_set('@header', { 'key' => 'value' })
end

it 'should return a hash with the configured header' do
ApiClient.config.header.should == { 'key' => 'value' }
end
end
end

describe '#header=' do
before :each do
ApiClient.configure do |config|
config.header = { 'Content-Type' => 'application/xml' }
end
end

it 'should merge content_type json with the given hash' do
ApiClient.config.header.should == { 'Content-Type' => 'application/xml' }
end
end
end

0 comments on commit ea50f5b

Please sign in to comment.