Class: CommonRestUtils
Instance Method Summary collapse
- #callMethod(url_suffix, http_method, body = nil) ⇒ Object
-
#initialize(uri_prefix) ⇒ CommonRestUtils
constructor
A new instance of CommonRestUtils.
Methods included from ConfigReader
#config_dir, #config_file, #imkdir, #mk_config_dir, #mk_user_dir, #param, #user_file
Constructor Details
#initialize(uri_prefix) ⇒ CommonRestUtils
Returns a new instance of CommonRestUtils.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/common.rb', line 63 def initialize(uri_prefix) @uri_prefix = uri_prefix proxy = param("proxy") if proxy != "" RestClient.proxy = proxy end @headers = {:Content_type => "Application/json", :Authorization => "TOK:" + param("token"), :Accept => "Application/json"} end |
Instance Method Details
#callMethod(url_suffix, http_method, body = nil) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/common.rb', line 8 def callMethod(url_suffix, http_method, body=nil) url = @uri_prefix + url_suffix if (body) json_body = JSON.pretty_generate body end begin response = case http_method when :put RestClient.put(url, json_body, @headers) when :get RestClient.get(url, @headers) when :post RestClient.post(url, json_body, @headers) when :delete RestClient.delete(url, @headers) else "unsupported http method:" + http_method end rescue Exception => e puts "Problem executing REST command to server " + param("rest_base_url") puts e. puts "#{e.response}" proxy_auth_req = (e. =~ /\A407 /) if e. =~ /\A407 / ProxyCredentials.proxy_credentials param("proxy") if @proxy_retries && @proxy_retries > 3 raise e. + " too many retries" else @proxy_retries = @proxy_retries ? @proxy_retries + 1 : 1 return callMethod(url_suffix, http_method, body) end end exit if $debug puts "full URL is:" + url if (body) puts "body is: json_body" end end raise e. + " (" + e.class.to_s + ")" end if ( ! response.nil? && response.respond_to?(:code) && response.code != 200) raise "Bad response code:" + response.code.to_s end if (response.length > 0) if $debug File.open("raw.json", "w").write(response) end JSON.parse response else nil end end |