lib/communication.rb in mxhero-api-1.2.3 vs lib/communication.rb in mxhero-api-1.2.4
- old
+ new
@@ -1,8 +1,9 @@
require 'httpclient'
require 'json'
require_relative 'response'
+require 'base64'
module MxHero
module API
module Communication
@@ -13,11 +14,11 @@
# @param [Hash] more_options
# @option more_options [Boolean] :throw_exception (default: true) throw exception if the response status are between 500 and 600
# @return [HTTP::Message](http://www.rubydoc.info/gems/httpclient/HTTP/Message)
def call(method, url, body = nil, more_options = {})
unless @client
- @client ||= HTTPClient.new
+ @client ||= HTTPClient.new(:force_basic_auth => true)
end
@client.set_auth(url, @username, @password)
response = @client.request(method, url, nil, body, headers)
raise "Unauthorized" if response.status == 401
unless more_options[:throw_exception] == false
@@ -26,10 +27,14 @@
response
end
# Default headers
def headers
- @headers ||= { 'Accept' => 'application/json', 'Content-Type' => 'application/json' }
+ {
+ 'Accept' => 'application/json',
+ 'Content-Type' => 'application/json',
+ "Authorization" => "Basic " + ::Base64.encode64(@username + ':' + @password).gsub("\n",''),
+ }.merge(@as_user ? { "X-MxHero-As-User" => @as_user } : {})
end
# @return [Hash]
def json_parse(json)
JSON.parse(json, symbolize_names: true)