From f40871b9b60ae706c0668c9ac4cfbfff866ce5dc Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Tue, 28 Feb 2012 17:13:20 +0000 Subject: generic rest-client calls ignoring http errors from task services --- lib/rest-client-wrapper.rb | 280 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 280 insertions(+) create mode 100644 lib/rest-client-wrapper.rb (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb new file mode 100644 index 0000000..95aee8e --- /dev/null +++ b/lib/rest-client-wrapper.rb @@ -0,0 +1,280 @@ +module OpenTox + + class WrapperResult < String + attr_accessor :content_type, :code + end + + class RestClientWrapper + + # create REST class methods + [:head,:get,:post,:put,:dealete].each do |method| + + #define_singleton_method method do |uri,args={},headers={},waiting_task=nil, wait=true| + define_singleton_method method do |uri,payload={},headers={},waiting_task=nil, wait=true| + + args={} + args[:method] = method + args[:url] = uri + args[:timeout] = 600 + args[:payload] = payload + args[:headers] = headers + #raise OpenTox::BadRequestError.new "Empty URI." unless uri # error raised at method call + raise OpenTox::BadRequestError.new "Invalid URI: '#{uri}'" unless URI.valid? uri + raise OpenTox::BadRequestError.new "Unreachable URI: '#{uri}'" unless URI.accessible? uri +=begin + raise "headers are no hash: "+headers.inspect unless headers==nil or headers.is_a?(Hash) + # TODO: loop over accept, contant_type, subjectid + raise OpenTox::BadRequestError.new "accept should go into the headers" if payload and payload.is_a?(Hash) and payload[:accept] + raise OpenTox::BadRequestError.new "content_type should go into the headers" if payload and payload.is_a?(Hash) and payload[:content_type] + raise OpenTox::BadRequestError.new "subjectid should go into the headers" if payload and payload.is_a?(Hash) and payload[:subjectid] + raise "__waiting_task__ must be 'nil' or '(sub)task', is "+waiting_task.class.to_s if + waiting_task!=nil and !(waiting_task.is_a?(Task) || waiting_task.is_a?(SubTask)) + headers.each{ |k,v| headers.delete(k) if v==nil } if headers #remove keys with empty values, as this can cause problems + ## PENDING partner services accept subjectid only in header + headers = {} unless headers + #headers[:subjectid] = payload.delete(:subjectid) if payload and payload.is_a?(Hash) and payload.has_key?(:subjectid) + + # PENDING needed for NUTA, until we finally agree on how to send subjectid + #headers[:subjectid] = payload.delete(:subjectid) if uri=~/ntua/ and payload and payload.is_a?(Hash) and payload.has_key?(:subjectid) +=end + + begin + #$logger.debug "RestCall: "+method.to_s+" "+uri.to_s+" "+headers.inspect+" "+args.inspect + request = RestClient::Request.new(args) + result = request.execute do |response, request, result| + unless response.code < 400 or URI.task? uri + $logger.error "#{uri} returned #{result.inspect}" + raise OpenTox::RestCallError result.inspect + end + return response + end + # ignore error codes from Task services (may contain eg 500 which causes exceptions in RestClient and RDF::Reader + #LOGGER.debug "result body size: #{result.body.size}" + + # PENDING NTUA does return errors with 200 + raise RestClient::ExceptionWithResponse.new(result) if uri=~/ntua/ and result.body =~ /about.*http:\/\/anonymous.org\/error/ + + # result is a string, with the additional fields content_type and code + res = WrapperResult.new(result.body) + res.content_type = result.headers[:content_type] + raise "content-type not set" unless res.content_type + res.code = result.code + + # TODO: Ambit returns task representation with 200 instead of result URI + return res if res.code==200 || !wait + + while (res.code==201 || res.code==202) + res = wait_for_task(res, uri, waiting_task) + end + raise "illegal status code: '"+res.code.to_s+"'" unless res.code==200 + return res + + rescue RestClient::RequestTimeout => ex + received_error ex.message, 408, nil, {:rest_uri => uri, :headers => headers, :payload => payload} + rescue Errno::ETIMEDOUT => ex + received_error ex.message, 408, nil, {:rest_uri => uri, :headers => headers, :payload => payload} + rescue Errno::ECONNREFUSED => ex + received_error ex.message, 500, nil, {:rest_uri => uri, :headers => headers, :payload => payload} + rescue RestClient::ExceptionWithResponse => ex + # error comming from a different webservice, + received_error ex.http_body, ex.http_code, ex.response.net_http_res.content_type, {:rest_uri => uri, :headers => headers, :payload => payload} + rescue OpenTox::RestCallError => ex + # already a rest-error, probably comes from wait_for_task, just pass through + raise ex + rescue => ex + # some internal error occuring in rest-client-wrapper, just pass through + raise ex + end + end + end + +=begin + # performs a GET REST call + # raises OpenTox::Error if call fails (rescued in overwrite.rb -> halt 502) + # per default: waits for Task to finish and returns result URI of Task + # @param [String] uri destination URI + # @param [optional,Hash] headers contains params like accept-header + # @param [optional,OpenTox::Task] waiting_task (can be a OpenTox::Subtask as well), progress is updated accordingly + # @param [wait,Boolean] wait set to false to NOT wait for task if result is task + # @return [OpenTox::WrapperResult] a String containing the result-body of the REST call + def self.get(uri, headers={}, waiting_task=nil, wait=true ) + execute( "get", uri, nil, headers, waiting_task, wait) + end + + # performs a POST REST call + # raises OpenTox::Error if call fails (rescued in overwrite.rb -> halt 502) + # per default: waits for Task to finish and returns result URI of Task + # @param [String] uri destination URI + # @param [optional,String] payload data posted to the service + # @param [optional,Hash] headers contains params like accept-header + # @param [optional,OpenTox::Task] waiting_task (can be a OpenTox::Subtask as well), progress is updated accordingly + # @param [wait,Boolean] wait set to false to NOT wait for task if result is task + # @return [OpenTox::WrapperResult] a String containing the result-body of the REST call + def self.post(uri, payload=nil, headers={}, waiting_task=nil, wait=true ) + execute( "post", uri, payload, headers, waiting_task, wait ) + end + + # performs a PUT REST call + # raises OpenTox::Error if call fails (rescued in overwrite.rb -> halt 502) + # @param [String] uri destination URI + # @param [optional,Hash] headers contains params like accept-header + # @param [optional,String] payload data put to the service + # @return [OpenTox::WrapperResult] a String containing the result-body of the REST call + def self.put(uri, payload=nil, headers={} ) + execute( "put", uri, payload, headers ) + end + + # performs a DELETE REST call + # raises OpenTox::Error if call fails (rescued in overwrite.rb -> halt 502) + # @param [String] uri destination URI + # @param [optional,Hash] headers contains params like accept-header + # @return [OpenTox::WrapperResult] a String containing the result-body of the REST call + def self.delete(uri, headers=nil ) + execute( "delete", uri, nil, headers) + end + + def self.head(uri) + execute("head",uri) + end + + private + def self.execute( rest_call, uri, payload=nil, headers={}, waiting_task=nil, wait=true ) + + raise OpenTox::BadRequestError.new "Empty URI." unless uri + raise OpenTox::BadRequestError.new "Invalid URI: '#{uri}'" unless URI.valid? uri + raise OpenTox::BadRequestError.new "Unreachable URI: '#{uri}'" unless URI.accessible? uri + raise "headers are no hash: "+headers.inspect unless headers==nil or headers.is_a?(Hash) + # TODO: loop over accept, contant_type, subjectid + raise OpenTox::BadRequestError.new "accept should go into the headers" if payload and payload.is_a?(Hash) and payload[:accept] + raise OpenTox::BadRequestError.new "content_type should go into the headers" if payload and payload.is_a?(Hash) and payload[:content_type] + raise OpenTox::BadRequestError.new "subjectid should go into the headers" if payload and payload.is_a?(Hash) and payload[:subjectid] + raise "__waiting_task__ must be 'nil' or '(sub)task', is "+waiting_task.class.to_s if + waiting_task!=nil and !(waiting_task.is_a?(Task) || waiting_task.is_a?(SubTask)) + headers.each{ |k,v| headers.delete(k) if v==nil } if headers #remove keys with empty values, as this can cause problems + ## PENDING partner services accept subjectid only in header + headers = {} unless headers + #headers[:subjectid] = payload.delete(:subjectid) if payload and payload.is_a?(Hash) and payload.has_key?(:subjectid) + + # PENDING needed for NUTA, until we finally agree on how to send subjectid + #headers[:subjectid] = payload.delete(:subjectid) if uri=~/ntua/ and payload and payload.is_a?(Hash) and payload.has_key?(:subjectid) + + begin + #LOGGER.debug "RestCall: "+rest_call.to_s+" "+uri.to_s+" "+headers.inspect+" "+payload.inspect + resource = RestClient::Resource.new(uri,{:timeout => 600}) + if rest_call=="post" || rest_call=="put" + result = resource.send(rest_call, payload, headers){|response, request, result| return response } + elsif rest_call == "head" + result = resource.send(rest_call){ |response, request, result| return response } + else + result = resource.send(rest_call, headers){|response, request, result| return response } + end + # ignore error codes from Task services (may contain eg 500 which causes exceptions in RestClient and RDF::Reader + unless result.code < 400 or URI.task? @uri + $logger.error "#{@uri} returned #{result}" + raise OpenTox::RestCallError result + end + #LOGGER.debug "result body size: #{result.body.size}" + + # PENDING NTUA does return errors with 200 + raise RestClient::ExceptionWithResponse.new(result) if uri=~/ntua/ and result.body =~ /about.*http:\/\/anonymous.org\/error/ + + # result is a string, with the additional fields content_type and code + res = WrapperResult.new(result.body) + res.content_type = result.headers[:content_type] + raise "content-type not set" unless res.content_type + res.code = result.code + + # TODO: Ambit returns task representation with 200 instead of result URI + return res if res.code==200 || !wait + + while (res.code==201 || res.code==202) + res = wait_for_task(res, uri, waiting_task) + end + raise "illegal status code: '"+res.code.to_s+"'" unless res.code==200 + return res + + rescue RestClient::RequestTimeout => ex + received_error ex.message, 408, nil, {:rest_uri => uri, :headers => headers, :payload => payload} + rescue Errno::ETIMEDOUT => ex + received_error ex.message, 408, nil, {:rest_uri => uri, :headers => headers, :payload => payload} + rescue Errno::ECONNREFUSED => ex + received_error ex.message, 500, nil, {:rest_uri => uri, :headers => headers, :payload => payload} + rescue RestClient::ExceptionWithResponse => ex + # error comming from a different webservice, + received_error ex.http_body, ex.http_code, ex.response.net_http_res.content_type, {:rest_uri => uri, :headers => headers, :payload => payload} + rescue OpenTox::RestCallError => ex + # already a rest-error, probably comes from wait_for_task, just pass through + raise ex + rescue => ex + # some internal error occuring in rest-client-wrapper, just pass through + raise ex + end + end +=end + + def self.wait_for_task( res, base_uri, waiting_task=nil ) + #TODO remove TUM hack + res.content_type = "text/uri-list" if base_uri =~/tu-muenchen/ and res.content_type == "application/x-www-form-urlencoded;charset=UTF-8" + + task = nil + case res.content_type + when /application\/rdf\+xml/ + task = OpenTox::Task.from_rdfxml(res) + when /yaml/ + task = OpenTox::Task.from_yaml(res) + when /text\// + raise "uri list has more than one entry, should be a task" if res.content_type=~/text\/uri-list/ and res.split("\n").size > 1 #if uri list contains more then one uri, its not a task + task = OpenTox::Task.find(res.to_s.chomp) if res.to_s.uri? + else + raise "unknown content-type for task : '"+res.content_type.to_s+"'"+" base-uri: "+base_uri.to_s+" content: "+res[0..200].to_s + end + + #LOGGER.debug "result is a task '"+task.uri.to_s+"', wait for completion" + task.wait waiting_task + unless task.completed? # maybe task was cancelled / error + if task.errorReport + received_error task.errorReport, task.http_code, nil, {:rest_uri => task.uri, :rest_code => task.http_code} + else + raise "status of task '"+task.uri.to_s+"' is no longer running (hasStatus is '"+task.status+ + "'), but it is neither completed nor has an errorReport" + end + end + + res = WrapperResult.new task.result_uri + res.code = task.http_code + res.content_type ="text/uri-list" + return res + end + + def self.received_error( body, code, content_type=nil, params=nil ) + + # try to parse body + report = nil + if body.is_a?(OpenTox::ErrorReport) + report = body + else + case content_type + when /yaml/ + report = YAML.load(body) + when /rdf/ + report = OpenTox::ErrorReport.from_rdf(body) + end + end + + unless report + # parsing was not successfull + # raise 'plain' RestCallError + err = OpenTox::RestCallError.new("REST call returned error: '"+body.to_s+"'") + err.rest_params = params + raise err + else + # parsing sucessfull + # raise RestCallError with parsed report as error cause + err = OpenTox::RestCallError.new("REST call subsequent error") + err.errorCause = report + err.rest_params = params + raise err + end + end + end +end -- cgit v1.2.3 From f0d38b06cbdcfd59494c220e6f4685f2e5aa38fd Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Wed, 29 Feb 2012 15:06:27 +0000 Subject: logging in error.rb, dynamic class methods in rest-client-wrapper.rb --- lib/rest-client-wrapper.rb | 232 +++++++++------------------------------------ 1 file changed, 45 insertions(+), 187 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 95aee8e..27538e4 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -1,73 +1,57 @@ module OpenTox - class WrapperResult < String - attr_accessor :content_type, :code - end - class RestClientWrapper - # create REST class methods + # REST methods + # Raises OpenTox::Error if call fails (rescued in overwrite.rb -> halt 502) + # Waits for Task to finish and returns result URI of Task per default + # @param [String] destination URI + # @param [optional,Hash|String] Payload data posted to the service + # @param [optional,Hash] Headers with params like :accept, :content_type, :subjectid + # @param [optional,OpenTox::Task] waiting_task (can be a OpenTox::Subtask as well), progress is updated accordingly + # @param [wait,Boolean] Set to false to NOT wait for task if result is a task + # @return [RestClient::Response] REST call response [:head,:get,:post,:put,:dealete].each do |method| - #define_singleton_method method do |uri,args={},headers={},waiting_task=nil, wait=true| define_singleton_method method do |uri,payload={},headers={},waiting_task=nil, wait=true| + # catch input errors + raise OpenTox::BadRequestError.new "Invalid URI: '#{uri}'" unless URI.valid? uri + raise OpenTox::BadRequestError.new "Unreachable URI: '#{uri}'" unless URI.accessible? uri + raise OpenTox::BadRequestError.new "Headers are not a hash: #{headers.inspect}" unless headers==nil or headers.is_a?(Hash) + [:accept,:content_type,:subjectid].each do |header| + raise OpenTox::BadRequestError.new "#{header} should be submitted in the headers" if payload and payload.is_a?(Hash) and payload[header] + end + raise OpenTox::BadRequestError "waiting_task is not 'nil', OpenTox::SubTask or OpenTox::Task: #{waiting_task.class}" unless waiting_task.nil? or waiting_task.is_a?(OpenTox::Task) or waiting_task.is_a?(OpenTox::SubTask) + args={} args[:method] = method args[:url] = uri args[:timeout] = 600 args[:payload] = payload - args[:headers] = headers - #raise OpenTox::BadRequestError.new "Empty URI." unless uri # error raised at method call - raise OpenTox::BadRequestError.new "Invalid URI: '#{uri}'" unless URI.valid? uri - raise OpenTox::BadRequestError.new "Unreachable URI: '#{uri}'" unless URI.accessible? uri -=begin - raise "headers are no hash: "+headers.inspect unless headers==nil or headers.is_a?(Hash) - # TODO: loop over accept, contant_type, subjectid - raise OpenTox::BadRequestError.new "accept should go into the headers" if payload and payload.is_a?(Hash) and payload[:accept] - raise OpenTox::BadRequestError.new "content_type should go into the headers" if payload and payload.is_a?(Hash) and payload[:content_type] - raise OpenTox::BadRequestError.new "subjectid should go into the headers" if payload and payload.is_a?(Hash) and payload[:subjectid] - raise "__waiting_task__ must be 'nil' or '(sub)task', is "+waiting_task.class.to_s if - waiting_task!=nil and !(waiting_task.is_a?(Task) || waiting_task.is_a?(SubTask)) headers.each{ |k,v| headers.delete(k) if v==nil } if headers #remove keys with empty values, as this can cause problems - ## PENDING partner services accept subjectid only in header - headers = {} unless headers - #headers[:subjectid] = payload.delete(:subjectid) if payload and payload.is_a?(Hash) and payload.has_key?(:subjectid) - - # PENDING needed for NUTA, until we finally agree on how to send subjectid - #headers[:subjectid] = payload.delete(:subjectid) if uri=~/ntua/ and payload and payload.is_a?(Hash) and payload.has_key?(:subjectid) -=end + args[:headers] = headers + begin - #$logger.debug "RestCall: "+method.to_s+" "+uri.to_s+" "+headers.inspect+" "+args.inspect request = RestClient::Request.new(args) - result = request.execute do |response, request, result| - unless response.code < 400 or URI.task? uri - $logger.error "#{uri} returned #{result.inspect}" - raise OpenTox::RestCallError result.inspect - end + response = request.execute do |response, request, result| + # ignore error codes from Task services (may contain eg 500 which causes exceptions in RestClient and RDF::Reader + raise OpenTox::RestCallError request, response unless response.code < 400 or URI.task? uri return response end - # ignore error codes from Task services (may contain eg 500 which causes exceptions in RestClient and RDF::Reader - #LOGGER.debug "result body size: #{result.body.size}" + # TODO: tests for workarounds # PENDING NTUA does return errors with 200 - raise RestClient::ExceptionWithResponse.new(result) if uri=~/ntua/ and result.body =~ /about.*http:\/\/anonymous.org\/error/ - - # result is a string, with the additional fields content_type and code - res = WrapperResult.new(result.body) - res.content_type = result.headers[:content_type] - raise "content-type not set" unless res.content_type - res.code = result.code + #raise RestClient::ExceptionWithResponse.new(response) if uri=~/ntua/ and response.body =~ /about.*http:\/\/anonymous.org\/error/ - # TODO: Ambit returns task representation with 200 instead of result URI - return res if res.code==200 || !wait - - while (res.code==201 || res.code==202) - res = wait_for_task(res, uri, waiting_task) + return response if response.code==200 or wait.false? + + # wait for task + while response.code==201 or response.code==202 + response = wait_for_task(response, uri, waiting_task) end - raise "illegal status code: '"+res.code.to_s+"'" unless res.code==200 - return res + return response rescue RestClient::RequestTimeout => ex received_error ex.message, 408, nil, {:rest_uri => uri, :headers => headers, :payload => payload} @@ -87,146 +71,24 @@ module OpenTox end end end - -=begin - # performs a GET REST call - # raises OpenTox::Error if call fails (rescued in overwrite.rb -> halt 502) - # per default: waits for Task to finish and returns result URI of Task - # @param [String] uri destination URI - # @param [optional,Hash] headers contains params like accept-header - # @param [optional,OpenTox::Task] waiting_task (can be a OpenTox::Subtask as well), progress is updated accordingly - # @param [wait,Boolean] wait set to false to NOT wait for task if result is task - # @return [OpenTox::WrapperResult] a String containing the result-body of the REST call - def self.get(uri, headers={}, waiting_task=nil, wait=true ) - execute( "get", uri, nil, headers, waiting_task, wait) - end - - # performs a POST REST call - # raises OpenTox::Error if call fails (rescued in overwrite.rb -> halt 502) - # per default: waits for Task to finish and returns result URI of Task - # @param [String] uri destination URI - # @param [optional,String] payload data posted to the service - # @param [optional,Hash] headers contains params like accept-header - # @param [optional,OpenTox::Task] waiting_task (can be a OpenTox::Subtask as well), progress is updated accordingly - # @param [wait,Boolean] wait set to false to NOT wait for task if result is task - # @return [OpenTox::WrapperResult] a String containing the result-body of the REST call - def self.post(uri, payload=nil, headers={}, waiting_task=nil, wait=true ) - execute( "post", uri, payload, headers, waiting_task, wait ) - end - - # performs a PUT REST call - # raises OpenTox::Error if call fails (rescued in overwrite.rb -> halt 502) - # @param [String] uri destination URI - # @param [optional,Hash] headers contains params like accept-header - # @param [optional,String] payload data put to the service - # @return [OpenTox::WrapperResult] a String containing the result-body of the REST call - def self.put(uri, payload=nil, headers={} ) - execute( "put", uri, payload, headers ) - end - - # performs a DELETE REST call - # raises OpenTox::Error if call fails (rescued in overwrite.rb -> halt 502) - # @param [String] uri destination URI - # @param [optional,Hash] headers contains params like accept-header - # @return [OpenTox::WrapperResult] a String containing the result-body of the REST call - def self.delete(uri, headers=nil ) - execute( "delete", uri, nil, headers) - end - - def self.head(uri) - execute("head",uri) - end - - private - def self.execute( rest_call, uri, payload=nil, headers={}, waiting_task=nil, wait=true ) - - raise OpenTox::BadRequestError.new "Empty URI." unless uri - raise OpenTox::BadRequestError.new "Invalid URI: '#{uri}'" unless URI.valid? uri - raise OpenTox::BadRequestError.new "Unreachable URI: '#{uri}'" unless URI.accessible? uri - raise "headers are no hash: "+headers.inspect unless headers==nil or headers.is_a?(Hash) - # TODO: loop over accept, contant_type, subjectid - raise OpenTox::BadRequestError.new "accept should go into the headers" if payload and payload.is_a?(Hash) and payload[:accept] - raise OpenTox::BadRequestError.new "content_type should go into the headers" if payload and payload.is_a?(Hash) and payload[:content_type] - raise OpenTox::BadRequestError.new "subjectid should go into the headers" if payload and payload.is_a?(Hash) and payload[:subjectid] - raise "__waiting_task__ must be 'nil' or '(sub)task', is "+waiting_task.class.to_s if - waiting_task!=nil and !(waiting_task.is_a?(Task) || waiting_task.is_a?(SubTask)) - headers.each{ |k,v| headers.delete(k) if v==nil } if headers #remove keys with empty values, as this can cause problems - ## PENDING partner services accept subjectid only in header - headers = {} unless headers - #headers[:subjectid] = payload.delete(:subjectid) if payload and payload.is_a?(Hash) and payload.has_key?(:subjectid) - - # PENDING needed for NUTA, until we finally agree on how to send subjectid - #headers[:subjectid] = payload.delete(:subjectid) if uri=~/ntua/ and payload and payload.is_a?(Hash) and payload.has_key?(:subjectid) - - begin - #LOGGER.debug "RestCall: "+rest_call.to_s+" "+uri.to_s+" "+headers.inspect+" "+payload.inspect - resource = RestClient::Resource.new(uri,{:timeout => 600}) - if rest_call=="post" || rest_call=="put" - result = resource.send(rest_call, payload, headers){|response, request, result| return response } - elsif rest_call == "head" - result = resource.send(rest_call){ |response, request, result| return response } - else - result = resource.send(rest_call, headers){|response, request, result| return response } - end - # ignore error codes from Task services (may contain eg 500 which causes exceptions in RestClient and RDF::Reader - unless result.code < 400 or URI.task? @uri - $logger.error "#{@uri} returned #{result}" - raise OpenTox::RestCallError result - end - #LOGGER.debug "result body size: #{result.body.size}" - - # PENDING NTUA does return errors with 200 - raise RestClient::ExceptionWithResponse.new(result) if uri=~/ntua/ and result.body =~ /about.*http:\/\/anonymous.org\/error/ - - # result is a string, with the additional fields content_type and code - res = WrapperResult.new(result.body) - res.content_type = result.headers[:content_type] - raise "content-type not set" unless res.content_type - res.code = result.code - - # TODO: Ambit returns task representation with 200 instead of result URI - return res if res.code==200 || !wait - - while (res.code==201 || res.code==202) - res = wait_for_task(res, uri, waiting_task) - end - raise "illegal status code: '"+res.code.to_s+"'" unless res.code==200 - return res - - rescue RestClient::RequestTimeout => ex - received_error ex.message, 408, nil, {:rest_uri => uri, :headers => headers, :payload => payload} - rescue Errno::ETIMEDOUT => ex - received_error ex.message, 408, nil, {:rest_uri => uri, :headers => headers, :payload => payload} - rescue Errno::ECONNREFUSED => ex - received_error ex.message, 500, nil, {:rest_uri => uri, :headers => headers, :payload => payload} - rescue RestClient::ExceptionWithResponse => ex - # error comming from a different webservice, - received_error ex.http_body, ex.http_code, ex.response.net_http_res.content_type, {:rest_uri => uri, :headers => headers, :payload => payload} - rescue OpenTox::RestCallError => ex - # already a rest-error, probably comes from wait_for_task, just pass through - raise ex - rescue => ex - # some internal error occuring in rest-client-wrapper, just pass through - raise ex - end - end -=end - def self.wait_for_task( res, base_uri, waiting_task=nil ) + def self.wait_for_task( response, base_uri, waiting_task=nil ) #TODO remove TUM hack - res.content_type = "text/uri-list" if base_uri =~/tu-muenchen/ and res.content_type == "application/x-www-form-urlencoded;charset=UTF-8" + # response.headers[:content_type] = "text/uri-list" if base_uri =~/tu-muenchen/ and response.headers[:content_type] == "application/x-www-form-urlencoded;charset=UTF-8" + puts "TASK" + puts response.inspect task = nil - case res.content_type + case response.headers[:content_type] when /application\/rdf\+xml/ - task = OpenTox::Task.from_rdfxml(res) - when /yaml/ - task = OpenTox::Task.from_yaml(res) - when /text\// - raise "uri list has more than one entry, should be a task" if res.content_type=~/text\/uri-list/ and res.split("\n").size > 1 #if uri list contains more then one uri, its not a task - task = OpenTox::Task.find(res.to_s.chomp) if res.to_s.uri? + # TODO: task uri from rdf + #task = OpenTox::Task.from_rdfxml(response) + #task = OpenTox::Task.from_rdfxml(response) + when /text\/uri-list/ + raise OpenTox::RestCallError nil, response, "Uri list has more than one entry, should be a single task" if response.split("\n").size > 1 #if uri list contains more then one uri, its not a task + task = OpenTox::Task.new(response.to_s.chomp) if URI.available? response.to_s else - raise "unknown content-type for task : '"+res.content_type.to_s+"'"+" base-uri: "+base_uri.to_s+" content: "+res[0..200].to_s + raise OpenTox::RestCallError nil, response, "Unknown content-type for task : '"+response.headers[:content_type].to_s+"'"+" base-uri: "+base_uri.to_s+" content: "+response[0..200].to_s end #LOGGER.debug "result is a task '"+task.uri.to_s+"', wait for completion" @@ -235,15 +97,11 @@ module OpenTox if task.errorReport received_error task.errorReport, task.http_code, nil, {:rest_uri => task.uri, :rest_code => task.http_code} else - raise "status of task '"+task.uri.to_s+"' is no longer running (hasStatus is '"+task.status+ + raise OpenTox::RestCallError nil, response, "Status of task '"+task.uri.to_s+"' is no longer running (hasStatus is '"+task.status+ "'), but it is neither completed nor has an errorReport" end end - - res = WrapperResult.new task.result_uri - res.code = task.http_code - res.content_type ="text/uri-list" - return res + response end def self.received_error( body, code, content_type=nil, params=nil ) -- cgit v1.2.3 From 77a5576782b85f2e3fdaf1b65eda2543eedb6b12 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Wed, 29 Feb 2012 15:17:27 +0000 Subject: request and response as RestClientWrapper instance variables --- lib/rest-client-wrapper.rb | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 27538e4..67114fb 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -2,6 +2,8 @@ module OpenTox class RestClientWrapper + attr_accessor :request, :response + # REST methods # Raises OpenTox::Error if call fails (rescued in overwrite.rb -> halt 502) # Waits for Task to finish and returns result URI of Task per default @@ -34,24 +36,24 @@ module OpenTox begin - request = RestClient::Request.new(args) - response = request.execute do |response, request, result| + @request = RestClient::Request.new(args) + @response = @request.execute do |response, request, result| # ignore error codes from Task services (may contain eg 500 which causes exceptions in RestClient and RDF::Reader - raise OpenTox::RestCallError request, response unless response.code < 400 or URI.task? uri + rest_call_error unless response.code < 400 or URI.task? uri return response end # TODO: tests for workarounds # PENDING NTUA does return errors with 200 - #raise RestClient::ExceptionWithResponse.new(response) if uri=~/ntua/ and response.body =~ /about.*http:\/\/anonymous.org\/error/ + #raise RestClient::ExceptionWithResponse.new(@response) if uri=~/ntua/ and @response.body =~ /about.*http:\/\/anonymous.org\/error/ - return response if response.code==200 or wait.false? + return @response if @response.code==200 or wait.false? # wait for task - while response.code==201 or response.code==202 - response = wait_for_task(response, uri, waiting_task) + while @response.code==201 or @response.code==202 + @response = wait_for_task(@response, uri, waiting_task) end - return response + return @response rescue RestClient::RequestTimeout => ex received_error ex.message, 408, nil, {:rest_uri => uri, :headers => headers, :payload => payload} @@ -74,21 +76,19 @@ module OpenTox def self.wait_for_task( response, base_uri, waiting_task=nil ) #TODO remove TUM hack - # response.headers[:content_type] = "text/uri-list" if base_uri =~/tu-muenchen/ and response.headers[:content_type] == "application/x-www-form-urlencoded;charset=UTF-8" + # @response.headers[:content_type] = "text/uri-list" if base_uri =~/tu-muenchen/ and @response.headers[:content_type] == "application/x-www-form-urlencoded;charset=UTF-8" - puts "TASK" - puts response.inspect task = nil - case response.headers[:content_type] + case @response.headers[:content_type] when /application\/rdf\+xml/ # TODO: task uri from rdf - #task = OpenTox::Task.from_rdfxml(response) - #task = OpenTox::Task.from_rdfxml(response) + #task = OpenTox::Task.from_rdfxml(@response) + #task = OpenTox::Task.from_rdfxml(@response) when /text\/uri-list/ - raise OpenTox::RestCallError nil, response, "Uri list has more than one entry, should be a single task" if response.split("\n").size > 1 #if uri list contains more then one uri, its not a task - task = OpenTox::Task.new(response.to_s.chomp) if URI.available? response.to_s + rest_call_error "Uri list has more than one entry, should be a single task" if @response.split("\n").size > 1 #if uri list contains more then one uri, its not a task + task = OpenTox::Task.new(@response.to_s.chomp) if URI.available? @response.to_s else - raise OpenTox::RestCallError nil, response, "Unknown content-type for task : '"+response.headers[:content_type].to_s+"'"+" base-uri: "+base_uri.to_s+" content: "+response[0..200].to_s + rest_call_error @response, "Unknown content-type for task : '"+@response.headers[:content_type].to_s+"'"+" base-uri: "+base_uri.to_s+" content: "+@response[0..200].to_s end #LOGGER.debug "result is a task '"+task.uri.to_s+"', wait for completion" @@ -97,11 +97,15 @@ module OpenTox if task.errorReport received_error task.errorReport, task.http_code, nil, {:rest_uri => task.uri, :rest_code => task.http_code} else - raise OpenTox::RestCallError nil, response, "Status of task '"+task.uri.to_s+"' is no longer running (hasStatus is '"+task.status+ + rest_call_error "Status of task '"+task.uri.to_s+"' is no longer running (hasStatus is '"+task.status+ "'), but it is neither completed nor has an errorReport" end end - response + @response + end + + def rest_call_error message + raise OpenTox::RestCallError @request, @response, message end def self.received_error( body, code, content_type=nil, params=nil ) -- cgit v1.2.3 From c2986f418ede0ea443df0a1f7690c433b637dc57 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Wed, 29 Feb 2012 18:50:41 +0000 Subject: TaskError implemented, logging still partially redundant --- lib/rest-client-wrapper.rb | 88 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 61 insertions(+), 27 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 67114fb..1e871b0 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -17,15 +17,7 @@ module OpenTox define_singleton_method method do |uri,payload={},headers={},waiting_task=nil, wait=true| - # catch input errors - raise OpenTox::BadRequestError.new "Invalid URI: '#{uri}'" unless URI.valid? uri - raise OpenTox::BadRequestError.new "Unreachable URI: '#{uri}'" unless URI.accessible? uri - raise OpenTox::BadRequestError.new "Headers are not a hash: #{headers.inspect}" unless headers==nil or headers.is_a?(Hash) - [:accept,:content_type,:subjectid].each do |header| - raise OpenTox::BadRequestError.new "#{header} should be submitted in the headers" if payload and payload.is_a?(Hash) and payload[header] - end - raise OpenTox::BadRequestError "waiting_task is not 'nil', OpenTox::SubTask or OpenTox::Task: #{waiting_task.class}" unless waiting_task.nil? or waiting_task.is_a?(OpenTox::Task) or waiting_task.is_a?(OpenTox::SubTask) - + # create request args={} args[:method] = method args[:url] = uri @@ -33,13 +25,23 @@ module OpenTox 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 + @request = RestClient::Request.new(args) + + # catch input errors + rest_error "Invalid URI: '#{uri}'" unless URI.valid? uri + rest_error "Unreachable URI: '#{uri}'" unless URI.accessible? uri + rest_error "Headers are not a hash: #{headers.inspect}" unless headers==nil or headers.is_a?(Hash) + # make sure that no header parameters are set in payload + [:accept,:content_type,:subjectid].each do |header| + rest_error "#{header} should be submitted in the headers" if payload and payload.is_a?(Hash) and payload[header] + end + rest_error "waiting_task is not 'nil', OpenTox::SubTask or OpenTox::Task: #{waiting_task.class}" unless waiting_task.nil? or waiting_task.is_a?(OpenTox::Task) or waiting_task.is_a?(OpenTox::SubTask) begin - @request = RestClient::Request.new(args) @response = @request.execute do |response, request, result| # ignore error codes from Task services (may contain eg 500 which causes exceptions in RestClient and RDF::Reader - rest_call_error unless response.code < 400 or URI.task? uri + rest_error unless response.code < 400 or URI.task? uri return response end @@ -55,26 +57,34 @@ module OpenTox end return @response - rescue RestClient::RequestTimeout => ex - received_error ex.message, 408, nil, {:rest_uri => uri, :headers => headers, :payload => payload} - rescue Errno::ETIMEDOUT => ex - received_error ex.message, 408, nil, {:rest_uri => uri, :headers => headers, :payload => payload} - rescue Errno::ECONNREFUSED => ex - received_error ex.message, 500, nil, {:rest_uri => uri, :headers => headers, :payload => payload} - rescue RestClient::ExceptionWithResponse => ex + rescue + rest_error $!.message + end +=begin + rescue RestClient::RequestTimeout + raise OpenTox::Error @request, @response, $!.message + #received_error ex.message, 408, nil, {:rest_uri => uri, :headers => headers, :payload => payload} + rescue Errno::ETIMEDOUT + raise OpenTox::Error @request, @response, $!.message + #received_error ex.message, 408, nil, {:rest_uri => uri, :headers => headers, :payload => payload} + rescue Errno::ECONNREFUSED + raise OpenTox::Error $!.message + #received_error ex.message, 500, nil, {:rest_uri => uri, :headers => headers, :payload => payload} + rescue RestClient::ExceptionWithResponse # error comming from a different webservice, received_error ex.http_body, ex.http_code, ex.response.net_http_res.content_type, {:rest_uri => uri, :headers => headers, :payload => payload} - rescue OpenTox::RestCallError => ex + #rescue OpenTox::RestCallError => ex # already a rest-error, probably comes from wait_for_task, just pass through - raise ex - rescue => ex + #raise ex + #rescue => ex # some internal error occuring in rest-client-wrapper, just pass through - raise ex + #raise ex end +=end end end - def self.wait_for_task( response, base_uri, waiting_task=nil ) + def wait_for_task( response, base_uri, waiting_task=nil ) #TODO remove TUM hack # @response.headers[:content_type] = "text/uri-list" if base_uri =~/tu-muenchen/ and @response.headers[:content_type] == "application/x-www-form-urlencoded;charset=UTF-8" @@ -85,10 +95,10 @@ module OpenTox #task = OpenTox::Task.from_rdfxml(@response) #task = OpenTox::Task.from_rdfxml(@response) when /text\/uri-list/ - rest_call_error "Uri list has more than one entry, should be a single task" if @response.split("\n").size > 1 #if uri list contains more then one uri, its not a task + rest_error "Uri list has more than one entry, should be a single task" if @response.split("\n").size > 1 #if uri list contains more then one uri, its not a task task = OpenTox::Task.new(@response.to_s.chomp) if URI.available? @response.to_s else - rest_call_error @response, "Unknown content-type for task : '"+@response.headers[:content_type].to_s+"'"+" base-uri: "+base_uri.to_s+" content: "+@response[0..200].to_s + rest_error @response, "Unknown content-type for task : '"+@response.headers[:content_type].to_s+"'"+" base-uri: "+base_uri.to_s+" content: "+@response[0..200].to_s end #LOGGER.debug "result is a task '"+task.uri.to_s+"', wait for completion" @@ -104,14 +114,37 @@ module OpenTox @response end - def rest_call_error message - raise OpenTox::RestCallError @request, @response, message + def self.rest_error message + raise OpenTox::RestError.new :request => @request, :response => @response, :cause => message + end + +=begin + def self.bad_request_error message + raise OpenTox::Error.new message + end + + def self.not_found_error message + raise OpenTox::NotFoundError.new message end + def self.received_error( body, code, content_type=nil, params=nil ) + + # try to parse body TODO + body.is_a?(OpenTox::ErrorReport) ? report = body : report = OpenTox::ErrorReport.from_rdf(body) + rest_call_error "REST call returned error: '"+body.to_s+"'" unless report + # parsing sucessfull + # raise RestCallError with parsed report as error cause + err = OpenTox::RestCallError.new(@request, @response, "REST call subsequent error") + err.errorCause = report + raise err + end +=end +=begin def self.received_error( body, code, content_type=nil, params=nil ) # try to parse body report = nil + #report = OpenTox::ErrorReport.from_rdf(body) if body.is_a?(OpenTox::ErrorReport) report = body else @@ -138,5 +171,6 @@ module OpenTox raise err end end +=end end end -- cgit v1.2.3 From cbc5f08e92c92601009f0c11c8ec67ede2894858 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Fri, 2 Mar 2012 09:33:44 +0000 Subject: error report fixed for old task services --- lib/rest-client-wrapper.rb | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 1e871b0..0780dd5 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -27,29 +27,25 @@ module OpenTox args[:headers] = headers @request = RestClient::Request.new(args) - # catch input errors - rest_error "Invalid URI: '#{uri}'" unless URI.valid? uri - rest_error "Unreachable URI: '#{uri}'" unless URI.accessible? uri - rest_error "Headers are not a hash: #{headers.inspect}" unless headers==nil or headers.is_a?(Hash) - # make sure that no header parameters are set in payload + # check input + raise OpenTox::Error.new 400, "Invalid URI: '#{uri}'" unless URI.valid? uri + raise OpenTox::Error.new 400, "Unreachable URI: '#{uri}'" unless URI.accessible? uri + raise OpenTox::Error.new 400, "Headers are not a hash: #{headers.inspect}" unless headers==nil or headers.is_a?(Hash) + # make sure that no header parameters are set in the payload [:accept,:content_type,:subjectid].each do |header| - rest_error "#{header} should be submitted in the headers" if payload and payload.is_a?(Hash) and payload[header] + raise OpenTox::Error.new 400, "#{header} should be submitted in the headers" if payload and payload.is_a?(Hash) and payload[header] end - rest_error "waiting_task is not 'nil', OpenTox::SubTask or OpenTox::Task: #{waiting_task.class}" unless waiting_task.nil? or waiting_task.is_a?(OpenTox::Task) or waiting_task.is_a?(OpenTox::SubTask) + raise OpenTox::Error.new 400, "waiting_task is not 'nil', OpenTox::SubTask or OpenTox::Task: #{waiting_task.class}" unless waiting_task.nil? or waiting_task.is_a?(OpenTox::Task) or waiting_task.is_a?(OpenTox::SubTask) begin @response = @request.execute do |response, request, result| # ignore error codes from Task services (may contain eg 500 which causes exceptions in RestClient and RDF::Reader - rest_error unless response.code < 400 or URI.task? uri + rest_error "Response code is #{response.code}" unless response.code < 400 or URI.task? uri return response end - - # TODO: tests for workarounds - # PENDING NTUA does return errors with 200 - #raise RestClient::ExceptionWithResponse.new(@response) if uri=~/ntua/ and @response.body =~ /about.*http:\/\/anonymous.org\/error/ - return @response if @response.code==200 or wait.false? + return @response if @response.code==200 or !wait # wait for task while @response.code==201 or @response.code==202 @@ -98,7 +94,7 @@ module OpenTox rest_error "Uri list has more than one entry, should be a single task" if @response.split("\n").size > 1 #if uri list contains more then one uri, its not a task task = OpenTox::Task.new(@response.to_s.chomp) if URI.available? @response.to_s else - rest_error @response, "Unknown content-type for task : '"+@response.headers[:content_type].to_s+"'"+" base-uri: "+base_uri.to_s+" content: "+@response[0..200].to_s + rest_error "Unknown content-type for task : '"+@response.headers[:content_type].to_s+"'"+" base-uri: "+base_uri.to_s+" content: "+@response[0..200].to_s end #LOGGER.debug "result is a task '"+task.uri.to_s+"', wait for completion" @@ -107,7 +103,7 @@ module OpenTox if task.errorReport received_error task.errorReport, task.http_code, nil, {:rest_uri => task.uri, :rest_code => task.http_code} else - rest_call_error "Status of task '"+task.uri.to_s+"' is no longer running (hasStatus is '"+task.status+ + rest_error "Status of task '"+task.uri.to_s+"' is no longer running (hasStatus is '"+task.status+ "'), but it is neither completed nor has an errorReport" end end @@ -115,7 +111,7 @@ module OpenTox end def self.rest_error message - raise OpenTox::RestError.new :request => @request, :response => @response, :cause => message + raise OpenTox::RestCallError.new @request, @response, message end =begin -- cgit v1.2.3 From 53fe462b9c310bc84df50d058500772b7f3cbc3c Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Fri, 2 Mar 2012 09:40:08 +0000 Subject: error test fixed --- lib/rest-client-wrapper.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 0780dd5..17e1cd0 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -28,14 +28,14 @@ module OpenTox @request = RestClient::Request.new(args) # check input - raise OpenTox::Error.new 400, "Invalid URI: '#{uri}'" unless URI.valid? uri - raise OpenTox::Error.new 400, "Unreachable URI: '#{uri}'" unless URI.accessible? uri - raise OpenTox::Error.new 400, "Headers are not a hash: #{headers.inspect}" unless headers==nil or headers.is_a?(Hash) + raise OpenTox::BadRequestError.new "Invalid URI: '#{uri}'" unless URI.valid? uri + raise OpenTox::BadRequestError.new "Unreachable URI: '#{uri}'" unless URI.accessible? uri + raise OpenTox::BadRequestError.new "Headers are not a hash: #{headers.inspect}" unless headers==nil or headers.is_a?(Hash) # make sure that no header parameters are set in the payload [:accept,:content_type,:subjectid].each do |header| - raise OpenTox::Error.new 400, "#{header} should be submitted in the headers" if payload and payload.is_a?(Hash) and payload[header] + raise OpenTox::BadRequestError.new "#{header} should be submitted in the headers" if payload and payload.is_a?(Hash) and payload[header] end - raise OpenTox::Error.new 400, "waiting_task is not 'nil', OpenTox::SubTask or OpenTox::Task: #{waiting_task.class}" unless waiting_task.nil? or waiting_task.is_a?(OpenTox::Task) or waiting_task.is_a?(OpenTox::SubTask) + raise OpenTox::BadRequestError.new "waiting_task is not 'nil', OpenTox::SubTask or OpenTox::Task: #{waiting_task.class}" unless waiting_task.nil? or waiting_task.is_a?(OpenTox::Task) or waiting_task.is_a?(OpenTox::SubTask) begin -- cgit v1.2.3 From a5f8c658ba87a00950766182966b65c65d5e2b66 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Fri, 2 Mar 2012 19:04:02 +0000 Subject: additional OpenTox errors, *_error methods in rest-client-wrapper --- lib/rest-client-wrapper.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 17e1cd0..64c7d7e 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -28,14 +28,14 @@ module OpenTox @request = RestClient::Request.new(args) # check input - raise OpenTox::BadRequestError.new "Invalid URI: '#{uri}'" unless URI.valid? uri - raise OpenTox::BadRequestError.new "Unreachable URI: '#{uri}'" unless URI.accessible? uri - raise OpenTox::BadRequestError.new "Headers are not a hash: #{headers.inspect}" unless headers==nil or headers.is_a?(Hash) + bad_request_error "Invalid URI: '#{uri}'" unless URI.valid? uri + bad_request_error "Unreachable URI: '#{uri}'" unless URI.accessible? uri + bad_request_error "Headers are not a hash: #{headers.inspect}" unless headers==nil or headers.is_a?(Hash) # make sure that no header parameters are set in the payload [:accept,:content_type,:subjectid].each do |header| - raise OpenTox::BadRequestError.new "#{header} should be submitted in the headers" if payload and payload.is_a?(Hash) and payload[header] + bad_request_error "#{header} should be submitted in the headers" if payload and payload.is_a?(Hash) and payload[header] end - raise OpenTox::BadRequestError.new "waiting_task is not 'nil', OpenTox::SubTask or OpenTox::Task: #{waiting_task.class}" unless waiting_task.nil? or waiting_task.is_a?(OpenTox::Task) or waiting_task.is_a?(OpenTox::SubTask) + bad_request_error "waiting_task is not 'nil', OpenTox::SubTask or OpenTox::Task: #{waiting_task.class}" unless waiting_task.nil? or waiting_task.is_a?(OpenTox::Task) or waiting_task.is_a?(OpenTox::SubTask) begin -- cgit v1.2.3 From 2f6d5c75fc1fece5fc10cc7c45ad59cf6b820d64 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Wed, 7 Mar 2012 17:13:48 +0000 Subject: error and dataset tests added, wait_for_task moved to URI.to_object --- lib/rest-client-wrapper.rb | 73 +++++++++++++--------------------------------- 1 file changed, 20 insertions(+), 53 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 64c7d7e..e594729 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -13,7 +13,7 @@ module OpenTox # @param [optional,OpenTox::Task] waiting_task (can be a OpenTox::Subtask as well), progress is updated accordingly # @param [wait,Boolean] Set to false to NOT wait for task if result is a task # @return [RestClient::Response] REST call response - [:head,:get,:post,:put,:dealete].each do |method| + [:head,:get,:post,:put,:delete].each do |method| define_singleton_method method do |uri,payload={},headers={},waiting_task=nil, wait=true| @@ -25,64 +25,40 @@ module OpenTox 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 - @request = RestClient::Request.new(args) # check input bad_request_error "Invalid URI: '#{uri}'" unless URI.valid? uri - bad_request_error "Unreachable URI: '#{uri}'" unless URI.accessible? uri + not_found_error "URI '#{uri}' not found." unless URI.accessible? uri bad_request_error "Headers are not a hash: #{headers.inspect}" unless headers==nil or headers.is_a?(Hash) # make sure that no header parameters are set in the payload [:accept,:content_type,:subjectid].each do |header| bad_request_error "#{header} should be submitted in the headers" if payload and payload.is_a?(Hash) and payload[header] end - bad_request_error "waiting_task is not 'nil', OpenTox::SubTask or OpenTox::Task: #{waiting_task.class}" unless waiting_task.nil? or waiting_task.is_a?(OpenTox::Task) or waiting_task.is_a?(OpenTox::SubTask) + #bad_request_error "waiting_task is not 'nil', OpenTox::SubTask or OpenTox::Task: #{waiting_task.class}" unless waiting_task.nil? or waiting_task.is_a?(OpenTox::Task) or waiting_task.is_a?(OpenTox::SubTask) - - begin - @response = @request.execute do |response, request, result| - # ignore error codes from Task services (may contain eg 500 which causes exceptions in RestClient and RDF::Reader - rest_error "Response code is #{response.code}" unless response.code < 400 or URI.task? uri - return response - end - - return @response if @response.code==200 or !wait + # perform request + @request = RestClient::Request.new(args) + #begin + # do not throw RestClient exceptions in order to create a @response object (needed for error reports) in every case + @response = @request.execute { |response, request, result| return response } + # ignore error codes from Task services (may return error codes >= 400 according to API, which causes exceptions in RestClient and RDF::Reader) + raise OpenTox::RestCallError.new @request, @response, "Response code is #{@response.code}." unless @response.code < 400 or URI.task? uri + #return @response if @response.code==200 or !wait # wait for task - while @response.code==201 or @response.code==202 - @response = wait_for_task(@response, uri, waiting_task) - end - return @response + #while @response.code==201 or @response.code==202 + #@response = wait_for_task(@response, uri, waiting_task) + #end + @response - rescue - rest_error $!.message - end -=begin - rescue RestClient::RequestTimeout - raise OpenTox::Error @request, @response, $!.message - #received_error ex.message, 408, nil, {:rest_uri => uri, :headers => headers, :payload => payload} - rescue Errno::ETIMEDOUT - raise OpenTox::Error @request, @response, $!.message - #received_error ex.message, 408, nil, {:rest_uri => uri, :headers => headers, :payload => payload} - rescue Errno::ECONNREFUSED - raise OpenTox::Error $!.message - #received_error ex.message, 500, nil, {:rest_uri => uri, :headers => headers, :payload => payload} - rescue RestClient::ExceptionWithResponse - # error comming from a different webservice, - received_error ex.http_body, ex.http_code, ex.response.net_http_res.content_type, {:rest_uri => uri, :headers => headers, :payload => payload} - #rescue OpenTox::RestCallError => ex - # already a rest-error, probably comes from wait_for_task, just pass through - #raise ex - #rescue => ex - # some internal error occuring in rest-client-wrapper, just pass through - #raise ex - end -=end + #rescue + #rest_error $!.message + #end end end +=begin def wait_for_task( response, base_uri, waiting_task=nil ) - #TODO remove TUM hack - # @response.headers[:content_type] = "text/uri-list" if base_uri =~/tu-muenchen/ and @response.headers[:content_type] == "application/x-www-form-urlencoded;charset=UTF-8" task = nil case @response.headers[:content_type] @@ -97,7 +73,6 @@ module OpenTox rest_error "Unknown content-type for task : '"+@response.headers[:content_type].to_s+"'"+" base-uri: "+base_uri.to_s+" content: "+@response[0..200].to_s end - #LOGGER.debug "result is a task '"+task.uri.to_s+"', wait for completion" task.wait waiting_task unless task.completed? # maybe task was cancelled / error if task.errorReport @@ -111,18 +86,10 @@ module OpenTox end def self.rest_error message + puts message raise OpenTox::RestCallError.new @request, @response, message end -=begin - def self.bad_request_error message - raise OpenTox::Error.new message - end - - def self.not_found_error message - raise OpenTox::NotFoundError.new message - end - def self.received_error( body, code, content_type=nil, params=nil ) # try to parse body TODO -- cgit v1.2.3 From 63fcd8f8feed58af4b1e1ff0e5fdaa09791c9596 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Thu, 8 Mar 2012 15:23:43 +0000 Subject: improved integration of error reports, call stack added as errorDetails --- lib/rest-client-wrapper.rb | 122 ++++++--------------------------------------- 1 file changed, 15 insertions(+), 107 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index e594729..c9e6bbb 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -10,21 +10,10 @@ module OpenTox # @param [String] destination URI # @param [optional,Hash|String] Payload data posted to the service # @param [optional,Hash] Headers with params like :accept, :content_type, :subjectid - # @param [optional,OpenTox::Task] waiting_task (can be a OpenTox::Subtask as well), progress is updated accordingly - # @param [wait,Boolean] Set to false to NOT wait for task if result is a task # @return [RestClient::Response] REST call response [:head,:get,:post,:put,:delete].each do |method| - define_singleton_method method do |uri,payload={},headers={},waiting_task=nil, wait=true| - - # create request - args={} - args[:method] = method - args[:url] = uri - args[:timeout] = 600 - 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 + define_singleton_method method do |uri,payload={},headers={}| # check input bad_request_error "Invalid URI: '#{uri}'" unless URI.valid? uri @@ -34,106 +23,25 @@ module OpenTox [:accept,:content_type,:subjectid].each do |header| bad_request_error "#{header} should be submitted in the headers" if payload and payload.is_a?(Hash) and payload[header] end - #bad_request_error "waiting_task is not 'nil', OpenTox::SubTask or OpenTox::Task: #{waiting_task.class}" unless waiting_task.nil? or waiting_task.is_a?(OpenTox::Task) or waiting_task.is_a?(OpenTox::SubTask) + + # create request + args={} + args[:method] = method + args[:url] = uri + args[:timeout] = 600 + 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 # perform request @request = RestClient::Request.new(args) - #begin - # do not throw RestClient exceptions in order to create a @response object (needed for error reports) in every case - @response = @request.execute { |response, request, result| return response } - # ignore error codes from Task services (may return error codes >= 400 according to API, which causes exceptions in RestClient and RDF::Reader) - raise OpenTox::RestCallError.new @request, @response, "Response code is #{@response.code}." unless @response.code < 400 or URI.task? uri - #return @response if @response.code==200 or !wait - - # wait for task - #while @response.code==201 or @response.code==202 - #@response = wait_for_task(@response, uri, waiting_task) - #end - @response - - #rescue - #rest_error $!.message - #end - end - end - -=begin - def wait_for_task( response, base_uri, waiting_task=nil ) - - task = nil - case @response.headers[:content_type] - when /application\/rdf\+xml/ - # TODO: task uri from rdf - #task = OpenTox::Task.from_rdfxml(@response) - #task = OpenTox::Task.from_rdfxml(@response) - when /text\/uri-list/ - rest_error "Uri list has more than one entry, should be a single task" if @response.split("\n").size > 1 #if uri list contains more then one uri, its not a task - task = OpenTox::Task.new(@response.to_s.chomp) if URI.available? @response.to_s - else - rest_error "Unknown content-type for task : '"+@response.headers[:content_type].to_s+"'"+" base-uri: "+base_uri.to_s+" content: "+@response[0..200].to_s + # do not throw RestClient exceptions in order to create a @response object (needed for error reports) in every case + @response = @request.execute { |response, request, result| return response } + # ignore error codes from Task services (may return error codes >= 400 according to API, which causes exceptions in RestClient and RDF::Reader) + raise OpenTox::RestCallError.new @request, @response, "Response code is #{@response.code}." unless @response.code < 400 or URI.task? uri + @response end - - task.wait waiting_task - unless task.completed? # maybe task was cancelled / error - if task.errorReport - received_error task.errorReport, task.http_code, nil, {:rest_uri => task.uri, :rest_code => task.http_code} - else - rest_error "Status of task '"+task.uri.to_s+"' is no longer running (hasStatus is '"+task.status+ - "'), but it is neither completed nor has an errorReport" - end - end - @response - end - - def self.rest_error message - puts message - raise OpenTox::RestCallError.new @request, @response, message end - def self.received_error( body, code, content_type=nil, params=nil ) - - # try to parse body TODO - body.is_a?(OpenTox::ErrorReport) ? report = body : report = OpenTox::ErrorReport.from_rdf(body) - rest_call_error "REST call returned error: '"+body.to_s+"'" unless report - # parsing sucessfull - # raise RestCallError with parsed report as error cause - err = OpenTox::RestCallError.new(@request, @response, "REST call subsequent error") - err.errorCause = report - raise err - end -=end -=begin - def self.received_error( body, code, content_type=nil, params=nil ) - - # try to parse body - report = nil - #report = OpenTox::ErrorReport.from_rdf(body) - if body.is_a?(OpenTox::ErrorReport) - report = body - else - case content_type - when /yaml/ - report = YAML.load(body) - when /rdf/ - report = OpenTox::ErrorReport.from_rdf(body) - end - end - - unless report - # parsing was not successfull - # raise 'plain' RestCallError - err = OpenTox::RestCallError.new("REST call returned error: '"+body.to_s+"'") - err.rest_params = params - raise err - else - # parsing sucessfull - # raise RestCallError with parsed report as error cause - err = OpenTox::RestCallError.new("REST call subsequent error") - err.errorCause = report - err.rest_params = params - raise err - end - end -=end end end -- cgit v1.2.3 From 9b51fd3f058d96263e32665b3dd83e38ab30563d Mon Sep 17 00:00:00 2001 From: rautenberg Date: Wed, 21 Mar 2012 16:26:32 +0100 Subject: prevent ssl uris from URI.accessible? check --- lib/rest-client-wrapper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index c9e6bbb..13ccf09 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -17,11 +17,11 @@ module OpenTox # check input bad_request_error "Invalid URI: '#{uri}'" unless URI.valid? uri - not_found_error "URI '#{uri}' not found." unless URI.accessible? uri + not_found_error "URI '#{uri}' not found." unless URI.accessible? uri unless URI.ssl?(uri) bad_request_error "Headers are not a hash: #{headers.inspect}" unless headers==nil or headers.is_a?(Hash) # make sure that no header parameters are set in the payload [:accept,:content_type,:subjectid].each do |header| - bad_request_error "#{header} should be submitted in the headers" if payload and payload.is_a?(Hash) and payload[header] + bad_request_error "#{header} should be submitted in the headers" if payload and payload.is_a?(Hash) and payload[header] unless URI(uri).host == URI(AA).host end # create request -- cgit v1.2.3 From 0d457e40c005ba4209ec7e3093aa06d73d65a442 Mon Sep 17 00:00:00 2001 From: rautenberg Date: Wed, 28 Mar 2012 18:26:19 +0200 Subject: change for new aa variables --- lib/rest-client-wrapper.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 13ccf09..30292de 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -16,12 +16,13 @@ module OpenTox define_singleton_method method do |uri,payload={},headers={}| # check input + @subjectid = headers[:subjectid] ? headers[:subjectid] : nil bad_request_error "Invalid URI: '#{uri}'" unless URI.valid? uri - not_found_error "URI '#{uri}' not found." unless URI.accessible? uri unless URI.ssl?(uri) + not_found_error "URI '#{uri}' not found." unless URI.accessible?(uri, @subjectid) unless URI.ssl?(uri) bad_request_error "Headers are not a hash: #{headers.inspect}" unless headers==nil or headers.is_a?(Hash) # make sure that no header parameters are set in the payload [:accept,:content_type,:subjectid].each do |header| - bad_request_error "#{header} should be submitted in the headers" if payload and payload.is_a?(Hash) and payload[header] unless URI(uri).host == URI(AA).host + bad_request_error "#{header} should be submitted in the headers" if payload and payload.is_a?(Hash) and payload[header] unless URI(uri).host == URI($aa[:uri]).host end # create request -- cgit v1.2.3 From ef6b74aa8167c1cd2685e8c7ab97f6905d5a03b9 Mon Sep 17 00:00:00 2001 From: rautenberg Date: Thu, 29 Mar 2012 13:24:27 +0200 Subject: let subjectid in header if aa is defined and request go to opensso --- lib/rest-client-wrapper.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 30292de..a5adc80 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -16,13 +16,16 @@ module OpenTox define_singleton_method method do |uri,payload={},headers={}| # check input - @subjectid = headers[:subjectid] ? headers[:subjectid] : nil + @subjectid = headers[:subjectid] ? headers[:subjectid] : nil bad_request_error "Invalid URI: '#{uri}'" unless URI.valid? uri not_found_error "URI '#{uri}' not found." unless URI.accessible?(uri, @subjectid) unless URI.ssl?(uri) bad_request_error "Headers are not a hash: #{headers.inspect}" unless headers==nil or headers.is_a?(Hash) # make sure that no header parameters are set in the payload [:accept,:content_type,:subjectid].each do |header| - bad_request_error "#{header} should be submitted in the headers" if payload and payload.is_a?(Hash) and payload[header] unless URI(uri).host == URI($aa[:uri]).host + if defined? $aa || URI(uri).host == URI($aa[:uri]).host + else + bad_request_error "#{header} should be submitted in the headers" if payload and payload.is_a?(Hash) and payload[header] + end end # create request -- cgit v1.2.3 From 97f9367b05a9a665022adc5c3f0a988acb1c4fa3 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Thu, 29 Mar 2012 14:50:03 +0200 Subject: RestClientWrapper follows redirects --- lib/rest-client-wrapper.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 30292de..aa5d9c4 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -34,13 +34,16 @@ module OpenTox 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 - # perform request @request = RestClient::Request.new(args) - # do not throw RestClient exceptions in order to create a @response object (needed for error reports) in every case - @response = @request.execute { |response, request, result| return response } # ignore error codes from Task services (may return error codes >= 400 according to API, which causes exceptions in RestClient and RDF::Reader) - raise OpenTox::RestCallError.new @request, @response, "Response code is #{@response.code}." unless @response.code < 400 or URI.task? uri - @response + @response = @request.execute do |response, request, result| + if [301, 302, 307].include? response.code and request.method == :get + response.follow_redirection(request, result) + else + raise OpenTox::RestCallError.new response.to_s, request, uri unless response.code < 400 or URI.task? uri + response + end + end end end -- cgit v1.2.3 From f8b6f8d19566d372e47edba7968ce66ff09052c9 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Thu, 26 Apr 2012 16:04:05 +0200 Subject: tests removed, URI.accessible? check temporarily removed --- lib/rest-client-wrapper.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 479d5a5..3071432 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -18,7 +18,8 @@ module OpenTox # check input @subjectid = headers[:subjectid] ? headers[:subjectid] : nil bad_request_error "Invalid URI: '#{uri}'" unless URI.valid? uri - not_found_error "URI '#{uri}' not found." unless URI.accessible?(uri, @subjectid) unless URI.ssl?(uri) + #TODO fix for internal installations + #not_found_error "URI '#{uri}' not found." unless URI.accessible?(uri, @subjectid) unless URI.ssl?(uri) bad_request_error "Headers are not a hash: #{headers.inspect}" unless headers==nil or headers.is_a?(Hash) # make sure that no header parameters are set in the payload [:accept,:content_type,:subjectid].each do |header| -- cgit v1.2.3 From 1d49fe0da8e7d4dfc57fdbfffdc4e32db2ef0647 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Wed, 2 May 2012 20:06:16 +0000 Subject: initial task service --- lib/rest-client-wrapper.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 3071432..af1ba42 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -19,7 +19,7 @@ module OpenTox @subjectid = headers[:subjectid] ? headers[:subjectid] : nil bad_request_error "Invalid URI: '#{uri}'" unless URI.valid? uri #TODO fix for internal installations - #not_found_error "URI '#{uri}' not found." unless URI.accessible?(uri, @subjectid) unless URI.ssl?(uri) + not_found_error "URI '#{uri}' not found." unless URI.accessible?(uri, @subjectid) unless URI.ssl?(uri) bad_request_error "Headers are not a hash: #{headers.inspect}" unless headers==nil or headers.is_a?(Hash) # make sure that no header parameters are set in the payload [:accept,:content_type,:subjectid].each do |header| @@ -44,7 +44,9 @@ module OpenTox if [301, 302, 307].include? response.code and request.method == :get response.follow_redirection(request, result) else - raise OpenTox::RestCallError.new response.to_s, request, uri unless response.code < 400 or URI.task? uri + #TODO Reactivate for external services + #raise OpenTox::RestCallError.new response.to_s, request, uri unless response.code < 400 or URI.task? uri + rest_call_error response.to_s, request, uri unless response.code < 400 or URI.task? uri response end end -- cgit v1.2.3 From 8b430fdfb285cbbb24116909c7fe69a424b522d2 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Thu, 3 May 2012 12:48:06 +0200 Subject: remote task tests working --- lib/rest-client-wrapper.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index af1ba42..d5d9c82 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -44,9 +44,7 @@ module OpenTox if [301, 302, 307].include? response.code and request.method == :get response.follow_redirection(request, result) else - #TODO Reactivate for external services - #raise OpenTox::RestCallError.new response.to_s, request, uri unless response.code < 400 or URI.task? uri - rest_call_error response.to_s, request, uri unless response.code < 400 or URI.task? uri + raise OpenTox::RestCallError.new response.to_s, request, uri unless response.code < 400 or URI.task? uri response end end -- cgit v1.2.3 From 8a5e3d69a16fc0c7d551e000270fe243ed121c85 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Fri, 4 May 2012 10:29:55 +0000 Subject: ntriples as default format, rdfxml as fallback --- lib/rest-client-wrapper.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index d5d9c82..479d5a5 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -18,7 +18,6 @@ module OpenTox # check input @subjectid = headers[:subjectid] ? headers[:subjectid] : nil bad_request_error "Invalid URI: '#{uri}'" unless URI.valid? uri - #TODO fix for internal installations not_found_error "URI '#{uri}' not found." unless URI.accessible?(uri, @subjectid) unless URI.ssl?(uri) bad_request_error "Headers are not a hash: #{headers.inspect}" unless headers==nil or headers.is_a?(Hash) # make sure that no header parameters are set in the payload -- cgit v1.2.3 From 5f61cefdefc51a728147478820ffe04c5ec127d5 Mon Sep 17 00:00:00 2001 From: rautenberg Date: Mon, 4 Jun 2012 16:14:07 +0200 Subject: remove URI.ssl and URI.accessable test --- lib/rest-client-wrapper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 479d5a5..5707831 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -18,7 +18,7 @@ module OpenTox # check input @subjectid = headers[:subjectid] ? headers[:subjectid] : nil bad_request_error "Invalid URI: '#{uri}'" unless URI.valid? uri - not_found_error "URI '#{uri}' not found." unless URI.accessible?(uri, @subjectid) unless URI.ssl?(uri) + #not_found_error "URI '#{uri}' not found." unless URI.accessible?(uri, @subjectid) unless URI.ssl?(uri) bad_request_error "Headers are not a hash: #{headers.inspect}" unless headers==nil or headers.is_a?(Hash) # make sure that no header parameters are set in the payload [:accept,:content_type,:subjectid].each do |header| -- cgit v1.2.3 From 81f0924c654875a738b646ad951a28f906e0a97c Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Wed, 18 Jul 2012 09:59:21 +0200 Subject: rest call error handling fixed --- lib/rest-client-wrapper.rb | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 5707831..e89c90d 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -42,8 +42,30 @@ module OpenTox @response = @request.execute do |response, request, result| if [301, 302, 307].include? response.code and request.method == :get response.follow_redirection(request, result) + elsif response.code >= 400 and !URI.task?(uri) + message = response.to_s + message += "\nREST paramenters:\n#{request.args.inspect}" + case response.code + when 400 + bad_request_error message, uri + when 401 + not_authorized_error message, uri + when 404 + not_found_error message, uri + when 433 + locked_error message, uri + when 500 + internal_server_error message, uri + when 501 + not_implemented_error message, uri + when 503 + service_unavailable_error message, uri + when 504 + time_out_error message, uri + else + rest_call_error message, uri + end else - raise OpenTox::RestCallError.new response.to_s, request, uri unless response.code < 400 or URI.task? uri response end end -- cgit v1.2.3 From 32ad3c8f6e1e16cfe9fd59a47df6b560ffb13ddd Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Wed, 1 Aug 2012 14:56:08 +0200 Subject: task error handling improved --- lib/rest-client-wrapper.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index e89c90d..387a01b 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -17,9 +17,9 @@ module OpenTox # check input @subjectid = headers[:subjectid] ? headers[:subjectid] : nil - bad_request_error "Invalid URI: '#{uri}'" unless URI.valid? uri - #not_found_error "URI '#{uri}' not found." unless URI.accessible?(uri, @subjectid) unless URI.ssl?(uri) - bad_request_error "Headers are not a hash: #{headers.inspect}" unless headers==nil or headers.is_a?(Hash) + bad_request_error "Invalid URI: '#{uri}'", uri unless URI.valid? uri + #not_found_error "URI '#{uri}' not found.", uri unless URI.accessible?(uri, @subjectid) unless URI.ssl?(uri) + bad_request_error "Headers are not a hash: #{headers.inspect}", uri unless headers==nil or headers.is_a?(Hash) # 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 -- cgit v1.2.3 From 75b1d2a98c17d8ef86c3a7a974e1be5444c9fb20 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Thu, 2 Aug 2012 23:33:11 +0200 Subject: error handling improved --- lib/rest-client-wrapper.rb | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 387a01b..67a6264 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -18,13 +18,13 @@ module OpenTox # check input @subjectid = headers[:subjectid] ? headers[:subjectid] : nil bad_request_error "Invalid URI: '#{uri}'", uri unless URI.valid? uri - #not_found_error "URI '#{uri}' not found.", uri unless URI.accessible?(uri, @subjectid) unless URI.ssl?(uri) + #resource_not_found_error "URI '#{uri}' not found.", uri unless URI.accessible?(uri, @subjectid) unless URI.ssl?(uri) bad_request_error "Headers are not a hash: #{headers.inspect}", uri unless headers==nil or headers.is_a?(Hash) # 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 - bad_request_error "#{header} should be submitted in the headers" if payload and payload.is_a?(Hash) and payload[header] + bad_request_error "#{header} should be submitted in the headers", uri if payload and payload.is_a?(Hash) and payload[header] end end @@ -44,27 +44,11 @@ module OpenTox response.follow_redirection(request, result) elsif response.code >= 400 and !URI.task?(uri) message = response.to_s - message += "\nREST paramenters:\n#{request.args.inspect}" - case response.code - when 400 - bad_request_error message, uri - when 401 - not_authorized_error message, uri - when 404 - not_found_error message, uri - when 433 - locked_error message, uri - when 500 - internal_server_error message, uri - when 501 - not_implemented_error message, uri - when 503 - service_unavailable_error message, uri - when 504 - time_out_error message, uri - else - rest_call_error message, uri - end + parameters = request.args + parameters[:headers][:subjectid] = "REMOVED" if parameters[:headers] and parameters[:headers][:subjectid] + message += "\nREST paramenters:\n#{parameters.inspect}" + error = known_errors.collect{|e| e if e[:code] == response.code}.compact.first + Object.method(error[:method]).call message, uri # call error method else response end @@ -72,5 +56,18 @@ module OpenTox end end + def self.known_errors + errors = [] + RestClient::STATUSES.each do |code,k| + if code >= 400 + method = k.underscore.gsub(/ |'/,'_') + method += "_error" unless method.match(/_error$/) + klass = method.split("_").collect{|s| s.capitalize}.join("") + errors << {:code => code, :method => method.to_sym, :class => klass} + end + end + errors + end + end end -- cgit v1.2.3 From f1028bbed9f5b00391c66bf843eb95a39253ccf4 Mon Sep 17 00:00:00 2001 From: Andreas Maunz Date: Thu, 11 Oct 2012 15:16:44 +0200 Subject: Fixed RestClientWrapper description --- lib/rest-client-wrapper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 67a6264..1258339 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -6,7 +6,7 @@ module OpenTox # REST methods # Raises OpenTox::Error if call fails (rescued in overwrite.rb -> halt 502) - # Waits for Task to finish and returns result URI of Task per default + # Does not wait for task to finish and returns task uri # @param [String] destination URI # @param [optional,Hash|String] Payload data posted to the service # @param [optional,Hash] Headers with params like :accept, :content_type, :subjectid @@ -46,7 +46,7 @@ module OpenTox message = response.to_s parameters = request.args parameters[:headers][:subjectid] = "REMOVED" if parameters[:headers] and parameters[:headers][:subjectid] - message += "\nREST paramenters:\n#{parameters.inspect}" + message += "\nREST parameters:\n#{parameters.inspect}" error = known_errors.collect{|e| e if e[:code] == response.code}.compact.first Object.method(error[:method]).call message, uri # call error method else -- cgit v1.2.3 From 54b833546cea44e03eca3183ad4a37b9f30651a1 Mon Sep 17 00:00:00 2001 From: mguetlein Date: Mon, 4 Feb 2013 14:07:28 +0100 Subject: add validation, some debug messages, small adjustments --- lib/rest-client-wrapper.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 1258339..0d1c56b 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -13,13 +13,13 @@ module OpenTox # @return [RestClient::Response] REST call response [:head,:get,:post,:put,:delete].each do |method| - define_singleton_method method do |uri,payload={},headers={}| + define_singleton_method method do |uri,payload={},headers={},waiting_task=nil| - # check input + # check input + bad_request_error "Headers are not a hash: #{headers.inspect}", uri unless headers==nil or headers.is_a?(Hash) @subjectid = headers[:subjectid] ? headers[:subjectid] : nil bad_request_error "Invalid URI: '#{uri}'", uri unless URI.valid? uri #resource_not_found_error "URI '#{uri}' not found.", uri unless URI.accessible?(uri, @subjectid) unless URI.ssl?(uri) - bad_request_error "Headers are not a hash: #{headers.inspect}", uri unless headers==nil or headers.is_a?(Hash) # 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 @@ -36,6 +36,8 @@ module OpenTox 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 #{uri} #{payload.inspect}" if method.to_s=="post" && payload.is_a?(Hash) @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) -- cgit v1.2.3 From f1a6d9f9d5bc73d757e0864d998c95f10ebeecbb Mon Sep 17 00:00:00 2001 From: rautenberg Date: Thu, 7 Feb 2013 11:55:53 +0100 Subject: add regex filter for URL authentification --- lib/rest-client-wrapper.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 1258339..79a65d9 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -45,7 +45,8 @@ module OpenTox elsif response.code >= 400 and !URI.task?(uri) message = response.to_s parameters = request.args - parameters[:headers][:subjectid] = "REMOVED" if parameters[:headers] and parameters[:headers][:subjectid] + parameters[:headers][:subjectid] = "REMOVED" if parameters[:headers] and parameters[:headers][:subjectid] + parameters[:url] = parameters[:url].gsub(/(http|https|)\:\/\/[a-zA-Z0-9\-]+\:[a-zA-Z0-9]+\@/, "REMOVED@") if parameters[:url] message += "\nREST parameters:\n#{parameters.inspect}" error = known_errors.collect{|e| e if e[:code] == response.code}.compact.first Object.method(error[:method]).call message, uri # call error method -- cgit v1.2.3 From a54db46684680d98311631804eca367cc949a715 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Tue, 26 Mar 2013 10:56:04 +0100 Subject: code cleanup and refactoring. --- lib/rest-client-wrapper.rb | 2 -- 1 file changed, 2 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index f3c56c8..38219c1 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -37,8 +37,6 @@ module OpenTox 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 #{uri} #{payload.inspect}" if method.to_s=="post" && payload.is_a?(Hash) - @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| -- cgit v1.2.3 From 39b094bf106b35fb8fa41fd387bbb43bfe285672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20G=C3=BCtlein?= Date: Tue, 28 May 2013 12:37:52 +0300 Subject: added short comment --- lib/rest-client-wrapper.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 38219c1..f3e6300 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -57,6 +57,7 @@ module OpenTox end end + #@return [Array] of hashes with error code, method and class def self.known_errors errors = [] RestClient::STATUSES.each do |code,k| -- cgit v1.2.3 From ddb83b4302e8628b333402d24e3e05fc90b3faef Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Wed, 10 Jul 2013 14:36:39 +0200 Subject: subjectids partially removed --- lib/rest-client-wrapper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index f3e6300..2de9377 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -17,7 +17,7 @@ module OpenTox # check input bad_request_error "Headers are not a hash: #{headers.inspect}", uri unless headers==nil or headers.is_a?(Hash) - @subjectid = headers[:subjectid] ? headers[:subjectid] : nil + headers[:subjectid] ||= OpenTox::SUBJECTID bad_request_error "Invalid URI: '#{uri}'", uri unless URI.valid? uri #resource_not_found_error "URI '#{uri}' not found.", uri unless URI.accessible?(uri, @subjectid) unless URI.ssl?(uri) # make sure that no header parameters are set in the payload -- cgit v1.2.3 From 80c1821e3a55b0d9c1ee51537e109dff3bc79423 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Tue, 16 Jul 2013 17:45:08 +0200 Subject: subjectid handled by RestClientWrapper --- lib/rest-client-wrapper.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 2de9377..134dd32 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -4,6 +4,16 @@ module OpenTox attr_accessor :request, :response + @@subjectid = nil + + def self.subjectid=(subjectid) + @@subjectid = subjectid + end + + def self.subjectid + @@subjectid + end + # 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 @@ -17,7 +27,7 @@ module OpenTox # check input bad_request_error "Headers are not a hash: #{headers.inspect}", uri unless headers==nil or headers.is_a?(Hash) - headers[:subjectid] ||= OpenTox::SUBJECTID + headers[:subjectid] ||= @@subjectid bad_request_error "Invalid URI: '#{uri}'", uri unless URI.valid? uri #resource_not_found_error "URI '#{uri}' not found.", uri unless URI.accessible?(uri, @subjectid) unless URI.ssl?(uri) # make sure that no header parameters are set in the payload -- cgit v1.2.3 From b4c39cd8c4dfcc40bdabc9db205bd347c2c0d826 Mon Sep 17 00:00:00 2001 From: mguetlein Date: Thu, 8 Aug 2013 16:40:54 +0200 Subject: error handling rewrite: making sure to pass backtrace --- lib/rest-client-wrapper.rb | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 134dd32..3ed41ad 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -53,13 +53,24 @@ module OpenTox if [301, 302, 307].include? response.code and request.method == :get response.follow_redirection(request, result) elsif response.code >= 400 and !URI.task?(uri) - message = response.to_s - parameters = request.args - parameters[:headers][:subjectid] = "REMOVED" if parameters[:headers] and parameters[:headers][:subjectid] - parameters[:url] = parameters[:url].gsub(/(http|https|)\:\/\/[a-zA-Z0-9\-]+\:[a-zA-Z0-9]+\@/, "REMOVED@") if parameters[:url] - message += "\nREST parameters:\n#{parameters.inspect}" + #TODO add parameters to error-report + #parameters = request.args + #parameters[:headers][:subjectid] = "REMOVED" if parameters[:headers] and parameters[:headers][:subjectid] + #parameters[:url] = parameters[:url].gsub(/(http|https|)\:\/\/[a-zA-Z0-9\-]+\:[a-zA-Z0-9]+\@/, "REMOVED@") if parameters[:url] + #message += "\nREST parameters:\n#{parameters.inspect}" error = known_errors.collect{|e| e if e[:code] == response.code}.compact.first - Object.method(error[:method]).call message, uri # call error method + begin # errors are returned as error reports in turtle, try to parse + content = {} + RDF::Reader.for(:turtle).new(response.to_s) do |reader| + reader.each_triple{|triple| content[triple[1]] = triple[2]} + end + msg = content[RDF::OT.message].to_s + cause = content[RDF::OT.errorCause].to_s + rescue # parsing error failed, use complete content as message + msg = response.to_s + cause = nil + end + Object.method(error[:method]).call msg, uri, cause # call error method else response end -- cgit v1.2.3 From 22b353110299ab819fc53cd83b456caa35900508 Mon Sep 17 00:00:00 2001 From: mguetlein Date: Fri, 16 Aug 2013 10:35:03 +0200 Subject: fix error handling when non-ot-errors are returned (like e.g. from the 4store) --- lib/rest-client-wrapper.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 3ed41ad..94ff029 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -61,13 +61,14 @@ module OpenTox error = known_errors.collect{|e| e if e[:code] == response.code}.compact.first begin # errors are returned as error reports in turtle, try to parse content = {} - RDF::Reader.for(:turtle).new(response.to_s) do |reader| + RDF::Reader.for(:turtle).new(response) do |reader| reader.each_triple{|triple| content[triple[1]] = triple[2]} end msg = content[RDF::OT.message].to_s cause = content[RDF::OT.errorCause].to_s + raise if msg.size==0 && cause.size==0 # parsing failed rescue # parsing error failed, use complete content as message - msg = response.to_s + 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 -- cgit v1.2.3 From 6bcfe8099906ad6a2041ac258cb81c232c6f4159 Mon Sep 17 00:00:00 2001 From: gebele Date: Fri, 30 May 2014 14:58:02 +0100 Subject: increased timeout for RestClientWrapper --- lib/rest-client-wrapper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 94ff029..732f46e 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -42,7 +42,7 @@ module OpenTox args={} args[:method] = method args[:url] = uri - args[:timeout] = 600 + 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 -- cgit v1.2.3 From d465c7792c2eb563c3e3403cb887f672c394b1e5 Mon Sep 17 00:00:00 2001 From: rautenberg Date: Wed, 9 Jul 2014 15:32:02 +0200 Subject: set default ssl verification to false to allow self signed certificates --- lib/rest-client-wrapper.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 732f46e..dc1def2 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -19,7 +19,7 @@ module OpenTox # Does not wait for task to finish and returns task uri # @param [String] destination URI # @param [optional,Hash|String] Payload data posted to the service - # @param [optional,Hash] Headers with params like :accept, :content_type, :subjectid + # @param [optional,Hash] Headers with params like :accept, :content_type, :subjectid, :verify_ssl # @return [RestClient::Response] REST call response [:head,:get,:post,:put,:delete].each do |method| @@ -42,10 +42,11 @@ module OpenTox 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 + args[:headers] = headers @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) -- cgit v1.2.3 From a9d781dcd792ae9f57021cf2daf2729f6708bb70 Mon Sep 17 00:00:00 2001 From: mguetlein Date: Fri, 10 Oct 2014 12:42:58 +0200 Subject: log post requests --- lib/rest-client-wrapper.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index dc1def2..7011eca 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -47,6 +47,8 @@ module OpenTox 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) -- cgit v1.2.3 From 723d731bc08d90a82f22474c9fafcc504eb424d3 Mon Sep 17 00:00:00 2001 From: ch Date: Wed, 8 Jul 2015 10:58:24 +0200 Subject: feature, error and task (almost) working --- lib/rest-client-wrapper.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'lib/rest-client-wrapper.rb') diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 7011eca..de1b74f 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -62,13 +62,11 @@ module OpenTox #parameters[:url] = parameters[:url].gsub(/(http|https|)\:\/\/[a-zA-Z0-9\-]+\:[a-zA-Z0-9]+\@/, "REMOVED@") if parameters[:url] #message += "\nREST parameters:\n#{parameters.inspect}" error = known_errors.collect{|e| e if e[:code] == response.code}.compact.first - begin # errors are returned as error reports in turtle, try to parse - content = {} - RDF::Reader.for(:turtle).new(response) do |reader| - reader.each_triple{|triple| content[triple[1]] = triple[2]} - end - msg = content[RDF::OT.message].to_s - cause = content[RDF::OT.errorCause].to_s + begin # errors are returned as error reports in json, try to parse + # TODO: may be the reason for failure of task.rb -n test_11_wait_for_error_task + 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}" -- cgit v1.2.3