Class: OpenTox::RestClientWrapper
Overview
Adjustments to the rest-client gem for OpenTox
Constant Summary collapse
- @@subjectid =
nil
Instance Attribute Summary collapse
-
#request ⇒ Object
Returns the value of attribute request.
-
#response ⇒ Object
Returns the value of attribute response.
Class Method Summary collapse
Instance Method Summary collapse
-
#method ⇒ RestClient::Response
REST methods Raises OpenTox::Error if call fails (rescued in overwrite.rb -> halt 502) Does not wait for task to finish and returns task uri.
Instance Attribute Details
#request ⇒ Object
Returns the value of attribute request.
6 7 8 |
# File 'lib/rest-client-wrapper.rb', line 6 def request @request end |
#response ⇒ Object
Returns the value of attribute response.
6 7 8 |
# File 'lib/rest-client-wrapper.rb', line 6 def response @response end |
Class Method Details
.subjectid ⇒ Object
14 15 16 |
# File 'lib/rest-client-wrapper.rb', line 14 def self.subjectid @@subjectid end |
.subjectid=(subjectid) ⇒ Object
10 11 12 |
# File 'lib/rest-client-wrapper.rb', line 10 def self.subjectid=(subjectid) @@subjectid = subjectid end |
Instance Method Details
#method ⇒ RestClient::Response
REST methods Raises OpenTox::Error if call fails (rescued in overwrite.rb -> halt 502) Does not wait for task to finish and returns task uri
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rest-client-wrapper.rb', line 25 [:head,:get,:post,:put,:delete].each do |method| define_singleton_method method do |uri,payload={},headers={},waiting_task=nil| uri = Addressable::URI.encode(uri) # check input raise ArgumentError, "Headers are not a hash: #{headers.inspect} for #{uri}." unless headers==nil or headers.is_a?(Hash) headers[:subjectid] ||= @@subjectid raise ArgumentError, "Invalid URI: '#{uri}'" unless URI.valid? uri # make sure that no header parameters are set in the payload [:accept,:content_type,:subjectid].each do |header| if defined? $aa || URI(uri).host == URI($aa[:uri]).host else raise ArgumentError, "#{header} should be submitted in the headers of URI: #{uri}" if payload and payload.is_a?(Hash) and payload[header] end end # create request args={} args[:method] = method args[:url] = uri args[:verify_ssl] = 0 if headers[:verify_ssl].nil? || headers[:verify_ssl].empty? args[:timeout] = 1800 args[:payload] = payload headers.each{ |k,v| headers.delete(k) if v==nil } if headers #remove keys with empty values, as this can cause problems args[:headers] = headers $logger.debug "post to #{uri} with params #{payload.inspect.to_s[0..1000]}" if method.to_s=="post" @request = RestClient::Request.new(args) # ignore error codes from Task services (may return error codes >= 400 according to API, which causes exceptions in RestClient and RDF::Reader) @response = @request.execute do |response, request, result| if [301, 302, 307].include? response.code and request.method == :get response.follow_redirection(request, result) =begin elsif response.code >= 400 and !URI.task?(uri) error = known_errors.collect{|e| e if e[:code] == response.code}.compact.first begin # errors are returned as error reports in json, try to parse content = JSON.parse(response) msg = content["message"].to_s cause = content["errorCause"].to_s raise if msg.size==0 && cause.size==0 # parsing failed rescue # parsing error failed, use complete content as message msg = "Could not parse error response from rest call '#{method}' to '#{uri}':\n#{response}" cause = nil end Object.method(error[:method]).call "#{msg}, #{uri}, #{cause}" # call error method =end else response end end end end |