Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions lib/adp/api_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def get_access_token
end

# @return [Object]
def get_adp_data(product_url)
def get_adp_data(product_url, headers = {}, body_only = true)

raise ConnectionException, "Connection error: can't get data, not connected" if (self.access_token.nil? || !is_connected_indicator?)

Expand All @@ -106,17 +106,21 @@ def get_adp_data(product_url)
"redirect_uri" => self.connection_configuration.redirectURL
};

data = send_web_request(product_url, data, authorization, 'application/json', 'GET')
headers = headers.merge({"Authorization" => authorization})

response = send_web_request(product_url, data, headers, 'GET')

# Get parsed body and rescue in case "response.body" is not JSON parsable
body = JSON.parse(response.body) rescue {}
raise ConnectionException, "Connection error: #{data['error']}, #{data['error_description']}" unless data["error"].nil?
Comment on lines +114 to 115

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@heliocola Honestly I'm a little bit bummed that I couldn't come up with something smarter here. This is just weird and has "but what if" written all over it. I'll be thinking about it, I'm sure I can come up with a better error handling (well, I just wanted to re-use what ADP gem had already)


return data
body_only ? return body : return response
end

def send_web_request(url, data={}, authorization=nil, content_type=nil, method=nil)
def send_web_request(url, data={}, headers = {}, method=nil)

data ||= {}
content_type ||= "application/x-www-form-urlencoded"
headers["Content-Type"] ||= "application/x-www-form-urlencoded"
method ||= 'POST'

log = Logger.new(STDOUT)
Expand All @@ -139,25 +143,23 @@ def send_web_request(url, data={}, authorization=nil, content_type=nil, method=n
http.cert = OpenSSL::X509::Certificate.new( pem );
http.key = OpenSSL::PKey::RSA.new(key, self.connection_configuration.sslKeyPass);
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.cert_store = OpenSSL::X509::Store.new
http.cert_store.add_file(self.connection_configuration.sslCaPath)
end

if method.eql?('POST')
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data( data );
headers = headers.merge({"User-Agent" => useragent })
else
request = Net::HTTP::Get.new(uri.request_uri)
end

request.initialize_http_header({"User-Agent" => useragent })

request["Content-Type"] = content_type

# add credentials if available
request["Authorization"] = authorization unless authorization.nil?
request.initialize_http_header(headers)

response = JSON.parse(http.request(request).body)
if request.method.eql?('POST')
return JSON.parse(http.request(request).body)
else
return http.request(request)
end
end
end
end
Expand Down