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, options = {})
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 options.has_key?("quiet")
if options.has_key?(:username) && options.has_key?(:password)
rest_params[:user] = options[:username]
rest_params[:password] = options[:password]
end
if %{put post}.include?(method.downcase)
options[:data] = options["data"] if options.has_key?("data")
rest_params[:payload] = options[:data].to_json if options.has_key?(:data)
if !options.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 options.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 => options["cookies"] }) if options.has_key?("cookies")
BrpmAuto.log rest_params.inspect if options.has_key?("verbose")
response = RestClient::Request.new(rest_params).execute
BrpmAuto.log "\tParsing response to JSON format ..." if options.has_key?("verbose")
begin
parsed_response = JSON.parse(response)
rescue
parsed_response = response
end
BrpmAuto.log "Parsed response: #{parsed_response.inspect}" if options.has_key?("verbose")
if response.code < 300
BrpmAuto.log "\treturn code: #{response.code}" unless options.has_key?("quiet")
result["status"] = "success"
result["code"] = response.code
result["response"] = parsed_response
result["cookies"] = response.cookies if options.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 options.has_key?("quiet")
end
rescue RestClient::Exception => e
BrpmAuto.log "\tREST call generated an error: #{e.message}" unless options.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 options.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
|