class HttpClient
Class HttpClient
performs HTTP requests to the Secured Cloud API Server. Authentication is handled via the supplied application key and shared secret.
This class expects the pem file “/cert/sc.pem” to be found in the working directory of the application using this library. This pem file is used for SSL validation.
- @author
-
Alan Vella
Constants
- APP_CONTENT_TYPE
- AUTH_SCHEME
- PEM_FILE_PATH
Public Class Methods
createAuthorization(method, url)
click to toggle source
Creates authorization header for a given method and URL.
# File lib/secured_cloud_api_client/http_client.rb, line 282 def self.createAuthorization(method, url) stringToSign = method + " " + removeServletName(url) + " " + @applicationKey hash = OpenSSL::HMAC.digest('sha256', @sharedSecret, stringToSign) requestSignature = Base64.encode64(hash).gsub(/\s+/, ' ').strip credentials = @applicationKey + ":" + requestSignature; encodedCredentials = Base64.encode64(credentials).gsub(/\s+/, ' ').strip return encodedCredentials end
handleResponseError(response)
click to toggle source
Raise error if response is not successful.
# File lib/secured_cloud_api_client/http_client.rb, line 35 def self.handleResponseError(response) if ((response.code != "202") && (response.code != "200")) then errorMsg = "" httpStatusDesc = "" #Add description of HTTP error code if (response.code == "400") then httpStatusDesc = "Bad Request" elsif (response.code == "403") then httpStatusDesc = "Forbidden" elsif (response.code == "404") then httpStatusDesc = "Resource Not Found" elsif (response.code == "405") then httpStatusDesc = "Method Not Allowed" elsif (response.code == "405") then httpStatusDesc = "Too Many Requests" elsif (response.code == "500") then httpStatusDesc = "Internal Error" end #Add error message from SecuredCloud API if there is any if ((response['X-Application-Error-Reference'] != nil) && (response['X-Application-Error-Reference'] != "")) then errorMsg = response['X-Application-Error-Reference'] end if ((response['X-Application-Error-Description'] != nil) && (response['X-Application-Error-Description'] != "")) then errorMsg = response['X-Application-Error-Description'] end #Build final error message to be sent to user. if (errorMsg == "") then errorMsg = "Error " + response.code.to_str() + " (" + httpStatusDesc + ") " + response.body() else errorMsg = "Error " + response.code.to_str() + " (" + httpStatusDesc + ") " + errorMsg end raise errorMsg end end
removeServletName(url)
click to toggle source
Removes the servlet name from a given external API URL.
# File lib/secured_cloud_api_client/http_client.rb, line 271 def self.removeServletName(url) urlAsArray = url.split("/"); newURL = "" for i in 4..(urlAsArray.size() - 1) newURL += "/" + urlAsArray[i] end return newURL end
runChecks()
click to toggle source
Run checks before issuing request.
# File lib/secured_cloud_api_client/http_client.rb, line 26 def self.runChecks if !(File.exists?(PEM_FILE_PATH)) then raise "A valid pem file named sc.pem is required for SSL validation. This file must be placed in the cert directory of your vagrant workspace." end end
sendDELETERequest(authInfo, url, params = nil)
click to toggle source
Sends an HTTP DELETE request and returns response.
# File lib/secured_cloud_api_client/http_client.rb, line 167 def self.sendDELETERequest(authInfo, url, params = nil) self.runChecks begin @applicationKey = authInfo.getApplicationKey() @sharedSecret = authInfo.getSharedSecret() #Add parameters if present if (params != nil) then url += "?" params.each do |param| url += param[0] + "=" + param[1] + "&" end #Remove last ampersand url = url[0...-1] end @url = URI.parse(url) #Setup SSL validation. http = Net::HTTP.new(@url.host, @url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER http.cert_store = OpenSSL::X509::Store.new http.cert_store.set_default_paths http.cert_store.add_file(PEM_FILE_PATH) #Create and execute request request = Net::HTTP::Delete.new(@url.request_uri) request['Accept'] = APP_CONTENT_TYPE request['Content-Type'] = APP_CONTENT_TYPE request['Authorization'] = AUTH_SCHEME + createAuthorization("DELETE", url) response = http.request(request) rescue Exception => e raise "Error creating API request: " + e.message end #Raise error if response is not successful. self.handleResponseError(response) #Return response. return response end
sendGETRequest(authInfo, url)
click to toggle source
Sends an HTTP GET request and returns response.
# File lib/secured_cloud_api_client/http_client.rb, line 85 def self.sendGETRequest(authInfo, url) self.runChecks begin @applicationKey = authInfo.getApplicationKey() @sharedSecret = authInfo.getSharedSecret() @url = URI.parse(url) #Setup SSL validation. http = Net::HTTP.new(@url.host, @url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER http.cert_store = OpenSSL::X509::Store.new http.cert_store.set_default_paths http.cert_store.add_file(PEM_FILE_PATH) #Create and execute request request = Net::HTTP::Get.new(@url.request_uri) request['Accept'] = APP_CONTENT_TYPE request['Authorization'] = AUTH_SCHEME + createAuthorization("GET", url) response = http.request(request) rescue Exception => e raise "Error creating API request: " + e.message end #Raise error if response is not successful. self.handleResponseError(response) #Return response. return response end
sendPOSTRequest(authInfo, url, body)
click to toggle source
Sends an HTTP POST request and returns response.
# File lib/secured_cloud_api_client/http_client.rb, line 126 def self.sendPOSTRequest(authInfo, url, body) self.runChecks begin @applicationKey = authInfo.getApplicationKey() @sharedSecret = authInfo.getSharedSecret() @url = URI.parse(url) #Setup SSL validation. http = Net::HTTP.new(@url.host, @url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER http.cert_store = OpenSSL::X509::Store.new http.cert_store.set_default_paths http.cert_store.add_file(PEM_FILE_PATH) #Create and execute request request = Net::HTTP::Post.new(@url.request_uri) request['Accept'] = APP_CONTENT_TYPE request['Content-Type'] = APP_CONTENT_TYPE request['Authorization'] = AUTH_SCHEME + createAuthorization("POST", url) request.body = body response = http.request(request) rescue Exception => e raise "Error creating API request: " + e.message end #Raise error if response is not successful. self.handleResponseError(response) #Return response. return response end
sendPUTRequest(authInfo, url, params = nil)
click to toggle source
Sends an HTTP PUT request and returns response.
# File lib/secured_cloud_api_client/http_client.rb, line 219 def self.sendPUTRequest(authInfo, url, params = nil) self.runChecks begin @applicationKey = authInfo.getApplicationKey() @sharedSecret = authInfo.getSharedSecret() #Add parameters if present if (params != nil) then url += "?" params.each do |param| url += param[0] + "=" + param[1] + "&" end #Remove last ampersand url = url[0...-1] end @url = URI.parse(url) #Setup SSL validation. http = Net::HTTP.new(@url.host, @url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER http.cert_store = OpenSSL::X509::Store.new http.cert_store.set_default_paths http.cert_store.add_file(PEM_FILE_PATH) #Create and execute request request = Net::HTTP::Put.new(@url.request_uri) request['Accept'] = APP_CONTENT_TYPE request['Content-Type'] = APP_CONTENT_TYPE request['Authorization'] = AUTH_SCHEME + createAuthorization("PUT", url) response = http.request(request) rescue Exception => e raise "Error creating API request: " + e.message end #Raise error if response is not successful. self.handleResponseError(response) #Return response. return response end