Class: Receiver::RestApiConnector
- Defined in:
- lib/receiver/rest_api_connector.rb
Overview
Allow to connects to an API and execute Rest command (GET/POST/PUT/DELETE)
Direct Known Subclasses
Instance Attribute Summary collapse
-
#auth_type ⇒ Object
readonly
The type of authentification.
-
#dest_ip ⇒ Object
readonly
The destination IP.
-
#dest_port ⇒ Object
readonly
The destination port.
-
#timeout ⇒ Object
readonly
The timeout.
Instance Method Summary collapse
-
#get_message_html(html) ⇒ Object
private
Get message for HTML error content.
-
#initialize(dest_ip, dest_port, auth_type, user, password, timeout = 10000, logger = nil) ⇒ RestApiConnector
constructor
Default constructor.
-
#launch_request(request_type, address, body = '') ⇒ Object
Launch an http request to the rest api.
-
#launch_request_mock(request_type, address, body = nil) ⇒ Object
Launch an http request to the rest api.
Constructor Details
#initialize(dest_ip, dest_port, auth_type, user, password, timeout = 10000, logger = nil) ⇒ RestApiConnector
Default constructor.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/receiver/rest_api_connector.rb', line 45 def initialize(dest_ip, dest_port, auth_type, user, password, timeout = 10000, logger = nil) super(logger) @logger.info("Receiver::RestApiConnector initialize the parameters...") if dest_ip == nil || dest_port == nil || user == nil || password == nil raise ArgumentError.new("Arguments can\'t be nil!") elsif auth_type != "BASIC" && auth_type != "DIGEST" raise ArgumentError.new("Authentication type invalid: " + auth_type) end @dest_ip = dest_ip @dest_port = dest_port @auth_type = auth_type @user = user @password = password @timeout = timeout @header = { "Accept" => "application/json", "Content-Type" => "application/json" } @body = '' @http = Net::HTTP.new(@dest_ip, @dest_port) @logger.info("Receiver::RestApiConnector initialize the parameters...") end |
Instance Attribute Details
#auth_type ⇒ Object (readonly)
The type of authentification
24 25 26 |
# File 'lib/receiver/rest_api_connector.rb', line 24 def auth_type @auth_type end |
#dest_ip ⇒ Object (readonly)
The destination IP.
18 19 20 |
# File 'lib/receiver/rest_api_connector.rb', line 18 def dest_ip @dest_ip end |
#dest_port ⇒ Object (readonly)
The destination port.
20 21 22 |
# File 'lib/receiver/rest_api_connector.rb', line 20 def dest_port @dest_port end |
#timeout ⇒ Object (readonly)
The timeout.
22 23 24 |
# File 'lib/receiver/rest_api_connector.rb', line 22 def timeout @timeout end |
Instance Method Details
#get_message_html(html) ⇒ Object (private)
Get message for HTML error content.
262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/receiver/rest_api_connector.rb', line 262 def (html) @logger.info("Receiver::RestApiConnector get the message html '#{html}' of the request...") begin parsed_html = XmlSimple.xml_in(html) #$LOG.debug(parsedHTML) return parsed_html['body'][0]['p'][0]['content'] rescue return html end end |
#launch_request(request_type, address, body = '') ⇒ Object
Launch an http request to the rest api.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 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 130 131 132 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/receiver/rest_api_connector.rb', line 78 def launch_request(request_type, address, body = '') @logger.info("Receiver::RestApiConnector launch a #{request_type} request on the '#{address}' with body '#{body}'...") uri = URI.parse("http://#{@dest_ip}:#{@dest_port}#{address.strip}") uri.user=@user.strip uri.password=@password.strip response=nil error = false if request_type == nil || address == nil || body == nil raise ArgumentError.new("Arguments can\'t be nil!") end if @auth_type == "BASIC" case request_type when "GET" request = Net::HTTP::Get.new(address) when "POST" request = Net::HTTP::Post.new(address) when "PUT" request = Net::HTTP::Put.new(address) when "DELETE" request = Net::HTTP::Delete.new(address) else raise ArgumentError.new("Invalid type: " + request_type) end request.basic_auth(@user, @password) request.body = body response = @http.request(request) error = false case request_type when "GET" || "DELETE" if response.code != '200' error = true end when "POST" if response.code != '201' error = true end when "PUT" if response.code != '200' && response.code != '202' error = true end end elsif @auth_type == "DIGEST" Net::HTTP.start(uri.host, uri.port) {|http| http.read_timeout = @timeout case request_type when "GET" request = Net::HTTP::Get.new(uri.request_uri,@header) when "POST" request = Net::HTTP::Post.new(uri.request_uri) when "PUT" request = Net::HTTP::Put.new(uri.request_uri,@header) when "DELETE" request = Net::HTTP::Delete.new(uri.request_uri,@header) else raise ArgumentError.new("Invalid type: " + request_type) end request.body = body response = http.request request if response['www-authenticate'] begin auth = Net::HTTP::DigestAuth.new.auth_header(uri, response['www-authenticate'], request_type) request.add_field 'Authorization', auth response = http.request request rescue => e = (response.body) end raise Common::HTTPError.getConsistentError(, response.code, response.body) end error = false case request_type when "GET" || "DELETE" if response.code != '200' error = true end when "POST" if response.code != '302' error = true end when "PUT" if response.code != '200' error = true end else raise ArgumentError.new("Invalid type: " + request_type) end #if error && response.body != "" # begin # m = JSON.parse(response.body) # message = m['message'] # rescue # # @todo raise exception # message = get_message_html(response.body) # end # raise Common::HTTPError.getConsistentError(message, response.code, response.body) #end #return response } end if error begin m = JSON.parse(response.body) = m['message'] rescue = (response.body) end raise Common::HTTPError.getConsistentError(, response.code, response.body) end return response end |
#launch_request_mock(request_type, address, body = nil) ⇒ Object
Launch an http request to the rest api.
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/receiver/rest_api_connector.rb', line 208 def launch_request_mock(request_type, address, body = nil) uri = URI.parse("http://#{@dest_ip}:#{@dest_port}#{address.strip}") uri.user=@user uri.password=@password response=nil if request_type == nil || address == nil || body == nil raise ArgumentError.new("Arguments can\'t be nil!") end if @auth_type == "DIGEST" uri = URI.parse("http://#{@dest_ip}:#{@dest_port}#{address.strip}") uri.user=@user.strip uri.password=@password.strip response=nil Net::HTTP.start(uri.host, uri.port) {|http| http.read_timeout = @timeout case request_type when "GET" request = Net::HTTP::Get.new(uri.request_uri,@header) when "POST" request = Net::HTTP::Post.new(uri.request_uri,@header) when "PUT" request = Net::HTTP::Put.new(uri.request_uri,@header) when "DELETE" request = Net::HTTP::Delete.new(uri.request_uri,@header) else raise ArgumentError.new("Invalid type: " + request_type) end request.body = body response = http.request request if response['www-authenticate'] begin auth = Net::HTTP::DigestAuth.new.auth_header(uri, response['www-authenticate'], request_type) request.add_field 'Authorization', auth response = http.request request return response rescue => e = (response.body) end raise Common::HTTPError.getConsistentError(, response.code, response.body) end } end return response end |