Class: HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/secured_cloud_api_client/http_client.rb

Overview

Class HttpClient performs HTTP requests to the Secured Cloud API Server. Authentication is handled via the supplied application key and shared secret.

@author

Alan Vella

Constant Summary collapse

APP_CONTENT_TYPE =
"application/vnd.securedcloud.v7.0+json"
AUTH_SCHEME =
"SC "

Class Method Summary collapse

Class Method Details

.createAuthorization(method, url) ⇒ Object

Creates authorization header for a given method and URL.



180
181
182
183
184
185
186
187
# File 'lib/secured_cloud_api_client/http_client.rb', line 180

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) ⇒ Object

Raise error if response is not successful.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/secured_cloud_api_client/http_client.rb', line 21

def self.handleResponseError(response)
  
  if ((response.code != "202") && (response.code != "200")) then
    
    errorMsg = ""
    
    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
    
    if (errorMsg == "") then
      errorMsg = "Error " + response.code.to_str() + " " + response.body()
    else
      errorMsg = "Error " + response.code.to_str() +  " " + errorMsg  
    end
    
    raise errorMsg
  end
end

.removeServletName(url) ⇒ Object

Removes the servlet name from a given external API URL.



169
170
171
172
173
174
175
176
# File 'lib/secured_cloud_api_client/http_client.rb', line 169

def self.removeServletName(url)
  urlAsArray = url.split("/");
  newURL = ""
  for i in 4..(urlAsArray.size() - 1)
    newURL += "/" + urlAsArray[i]
  end
  return newURL
end

.sendDELETERequest(authInfo, url, params = nil) ⇒ Object

Sends an HTTP DELETE request and returns response.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/secured_cloud_api_client/http_client.rb', line 96

def self.sendDELETERequest(authInfo, url, params = nil)
  
  @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)
  req = Net::HTTP::Delete.new(@url)
  
  #Populate header with required stuff.

  req['Accept'] = APP_CONTENT_TYPE
  req['Content-Type'] = APP_CONTENT_TYPE
  req['Authorization'] = AUTH_SCHEME + createAuthorization("DELETE", url)
  
  #Execute request.

  res = Net::HTTP.start(@url.host, @url.port,:use_ssl => @url.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE) {|http|http.request(req)} # Net::HTTPResponse object

  
  #Raise error if response is not successful.

  self.handleResponseError(res)

  #Return response.

  return res
  
end

.sendGETRequest(authInfo, url) ⇒ Object

Sends an HTTP GET request and returns response.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/secured_cloud_api_client/http_client.rb', line 46

def self.sendGETRequest(authInfo, url)

  @applicationKey = authInfo.getApplicationKey()
  @sharedSecret = authInfo.getSharedSecret()
  
  @url = URI.parse(url)
  req = Net::HTTP::Get.new(url)
  
  #Populate header with required stuff.

  req['Accept'] = APP_CONTENT_TYPE  
  req['Authorization'] = AUTH_SCHEME + createAuthorization("GET", url) 
  
  #Execute request.

  res = Net::HTTP.start(@url.host, @url.port,:use_ssl => @url.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE) {|http|http.request(req)} # Net::HTTPResponse object

  
  #Raise error if response is not successful.

  self.handleResponseError(res)
  
  #Return response.

  return res
end

.sendPOSTRequest(authInfo, url, body) ⇒ Object

Sends an HTTP POST request and returns response.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/secured_cloud_api_client/http_client.rb', line 70

def self.sendPOSTRequest(authInfo, url, body)
  
  @applicationKey = authInfo.getApplicationKey()
  @sharedSecret = authInfo.getSharedSecret()
  
  @url = URI.parse(url)
  req = Net::HTTP::Post.new(url)
  req.body = body
  
  #Populate header with required stuff.

  req['Accept'] = APP_CONTENT_TYPE
  req['Content-Type'] = APP_CONTENT_TYPE
  req['Authorization'] = AUTH_SCHEME + createAuthorization("POST", url)
  
  #Execute request.

  res = Net::HTTP.start(@url.host, @url.port,:use_ssl => @url.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE) {|http|http.request(req)} # Net::HTTPResponse object

  
  #Raise error if response is not successful.

  self.handleResponseError(res)

  #Return response.

  return res
end

.sendPUTRequest(authInfo, url, params = nil) ⇒ Object

Sends an HTTP PUT request and returns response.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/secured_cloud_api_client/http_client.rb', line 133

def self.sendPUTRequest(authInfo, url, params = nil)
  
  @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)
  req = Net::HTTP::Put.new(@url)
  
  #Populate header with required stuff.

  req['Accept'] = APP_CONTENT_TYPE
  req['Content-Type'] = APP_CONTENT_TYPE
  req['Authorization'] = AUTH_SCHEME + createAuthorization("PUT", url)
  
  #Execute request.

  res = Net::HTTP.start(@url.host, @url.port,:use_ssl => @url.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE) {|http|http.request(req)} # Net::HTTPResponse object

  
  #Raise error if response is not successful.

  self.handleResponseError(res)

  #Return response.

  return res
  
end