Method: Rest.rest_call
- Defined in:
- lib/rest_api.rb
.rest_call(url, method, options = {}) ⇒ Object
Makes an http method call and returns data in JSON
Attributes
-
url- the url for the request -
method- the http method [get, put, post] -
options- a hash of options+verbose+: gives verbose output (yes/no) +data+: required for put and post methods a hash of post data +username+: username for basic http authentication +password+: password for basic http authentication +suppress_errors+: continues after errors if true
Returns
-
returns a hash of the http response with these keys
-
status- success or ERROR -
message- if status is ERROR this will hold an error message -
code- the http status code 200=ok, 404=not found, 500=error, 504=not authorized -
data- the body of the http response
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 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 |
# File 'lib/rest_api.rb', line 46 def rest_call(url, method, = {}) methods = %w{get post put delete} result = rest_params = {} rest_params[:url] = URI.escape(url) unless methods.include?(method.downcase) BrpmAuto.log "No method named: #{method}" result["code"] = -1 result["status"] = "failure" result["message"] = "No method named: #{method}" return result end rest_params[:method] = method.downcase begin BrpmAuto.log("REST #{method.upcase} #{BrpmAuto.privatize(url, get_token(url))}") unless .has_key?("quiet") if .has_key?(:username) && .has_key?(:password) rest_params[:user] = [:username] rest_params[:password] = [:password] end if %{put post}.include?(method.downcase) [:data] = ["data"] if .has_key?("data") rest_params[:payload] = [:data].to_json if .has_key?(:data) if !.has_key?(:data) || rest_params[:payload].length < 4 result["code"] = -1 result["status"] = "failure" result["message"] = "No Post data" return result end BrpmAuto.log "\tPost Data: #{rest_params[:payload].inspect}" unless .has_key?("quiet") end rest_params.merge!({:headers => { :accept => :json, :content_type => :json }}) rest_params.merge!({:verify_ssl => OpenSSL::SSL::VERIFY_NONE}) rest_params.merge!({:cookies => ["cookies"] }) if .has_key?("cookies") BrpmAuto.log rest_params.inspect if .has_key?("verbose") response = RestClient::Request.new(rest_params).execute BrpmAuto.log "\tParsing response to JSON format ..." if .has_key?("verbose") begin parsed_response = JSON.parse(response) rescue parsed_response = response end BrpmAuto.log "Parsed response: #{parsed_response.inspect}" if .has_key?("verbose") if response.code < 300 BrpmAuto.log "\treturn code: #{response.code}" unless .has_key?("quiet") result["status"] = "success" result["code"] = response.code result["response"] = parsed_response result["cookies"] = response. if .has_key?("cookies") else result["status"] = "error" result["code"] = response.code result["response"] = parsed_response result["error_message"] = "REST call returned HTTP code:\n#{response.code}\n#{response}" BrpmAuto.log "\tREST call returned HTTP code #{response.code}" unless .has_key?("quiet") end rescue RestClient::Exception => e BrpmAuto.log "\tREST call generated an error: #{e.message}" unless .has_key?("quiet") if e.response.nil? result["status"] = "error" result["code"] = 1000 result["response"] = "No response received." result["error_message"] = "REST call generated a RestClient error:\n#{e.message}\n#{e.backtrace}" return end BrpmAuto.log "\tParsing response to JSON format ..." unless .has_key?("quiet") begin parsed_response = JSON.parse(e.response) rescue parsed_response = e.response end BrpmAuto.log "\tParsed response: #{parsed_response.inspect}" result["status"] = "error" result["code"] = e.response.code result["response"] = parsed_response result["error_message"] = "REST call generated a RestClient error:\n#{e.response.code}\n#{e.response}\n#{e.message}\n#{e.backtrace}" BrpmAuto.log "Back trace:\n#{e.backtrace}" unless already_exists_error(result) or not_found_error(result) end result end |