From 48fd81bd82037c1faae9860b9b1b4017b1aa27e0 Mon Sep 17 00:00:00 2001 From: gebele Date: Fri, 7 Jul 2017 11:09:59 +0000 Subject: fixed error messages --- api/api.json | 43 ++++++++++++++++++++++--------------------- lib/dataset.rb | 13 ++++++------- lib/feature.rb | 15 ++++++++++----- lib/lazar-rest.rb | 41 ++++++++++++++++++++++------------------- lib/model.rb | 10 ++++------ lib/nanoparticle.rb | 15 ++++++++++----- lib/report.rb | 2 +- lib/substance.rb | 15 ++++++++++----- lib/swagger.rb | 2 +- lib/validation.rb | 4 ++-- test/descriptor.rb | 2 +- test/setup.rb | 2 +- 12 files changed, 90 insertions(+), 74 deletions(-) diff --git a/api/api.json b/api/api.json index c226c90..af9dac3 100644 --- a/api/api.json +++ b/api/api.json @@ -32,7 +32,7 @@ "description": "Get a list of all prediction models", "parameters": [ { - "name": "accept", + "name": "Accept", "in": "header", "description": "requested Content-Type", "required": true, @@ -77,7 +77,7 @@ "description": "Get model representation", "parameters": [ { - "name": "accept", + "name": "Accept", "in": "header", "description": "requested Content-Type", "required": true, @@ -164,7 +164,7 @@ "description": "Get a list of QMRF reports", "parameters": [ { - "name": "accept", + "name": "Accept", "in": "header", "description": "requested Content-Type", "required": true, @@ -209,7 +209,7 @@ "description": "Get a QMRF report prototype", "parameters": [ { - "name": "accept", + "name": "Accept", "in": "header", "description": "requested Content-Type", "required": true, @@ -255,7 +255,7 @@ "description": "Get a list of all datasets", "parameters": [ { - "name": "accept", + "name": "Accept", "in": "header", "description": "requested Content-Type", "required": true, @@ -300,7 +300,7 @@ "description": "Get dataset representation", "parameters": [ { - "name": "accept", + "name": "Accept", "in": "header", "description": "requested Content-Type", "required": true, @@ -348,7 +348,7 @@ "description": "Get dataset representation", "parameters": [ { - "name": "accept", + "name": "Accept", "in": "header", "description": "requested Content-Type", "required": true, @@ -407,7 +407,7 @@ "description": "Get a list of all substances", "parameters": [ { - "name": "accept", + "name": "Accept", "in": "header", "description": "requested Content-Type", "required": true, @@ -452,7 +452,7 @@ "description": "Get substance representation", "parameters": [ { - "name": "accept", + "name": "Accept", "in": "header", "description": "requested Content-Type", "required": true, @@ -498,7 +498,7 @@ "description": "Get a list of all nanoparticles", "parameters": [ { - "name": "accept", + "name": "Accept", "in": "header", "description": "requested Content-Type", "required": true, @@ -543,7 +543,7 @@ "description": "Get nanoparticle representation", "parameters": [ { - "name": "accept", + "name": "Accept", "in": "header", "description": "requested Content-Type", "required": true, @@ -589,7 +589,7 @@ "description": "Get a list of all features", "parameters": [ { - "name": "accept", + "name": "Accept", "in": "header", "description": "requested Content-Type", "required": true, @@ -631,7 +631,7 @@ "description": "Get feature representation", "parameters": [ { - "name": "accept", + "name": "Accept", "in": "header", "description": "requested Content-Type", "required": true, @@ -674,7 +674,7 @@ "description": "Get a list of all validation types", "parameters": [ { - "name": "accept", + "name": "Accept", "in": "header", "description": "requested Content-Type", "required": true, @@ -716,7 +716,7 @@ "description": "Get all validations of a validation type", "parameters": [ { - "name": "accept", + "name": "Accept", "in": "header", "description": "requested Content-Type", "required": true, @@ -764,7 +764,7 @@ "description": "Get validation representation", "parameters": [ { - "name": "accept", + "name": "Accept", "in": "header", "description": "requested Content-Type", "required": true, @@ -814,7 +814,7 @@ "description": "Get a list of all descriptors", "parameters": [ { - "name": "accept", + "name": "Accept", "in": "header", "description": "requested Content-Type", "required": true, @@ -853,10 +853,10 @@ "descriptor" ], "summary": "Descriptor calculation", - "description": "Calculate descriptors for a single compound as SMILES string
curl example:
curl -Lk -X POST -i  -H \"accept:application/json\" -F \"identifier=O=C1NC(=O)NC=C1\" -F \"descriptor=Openbabel.MW,JoeLib.LogP\" https://enm.in-silico.ch/compound/descriptor 
.", + "description": "Calculate descriptors for a single compound as SMILES string
curl example:
curl -Lk -X POST -i  -H \"Accept:application/json\" -F \"identifier=O=C1NC(=O)NC=C1\" -F \"descriptor=Openbabel.MW,JoeLib.LogP\" https://api.in-silico.ch/compound/descriptor 
.", "parameters": [ { - "name": "accept", + "name": "Accept", "in": "header", "description": "requested Content-Type", "required": true, @@ -920,7 +920,7 @@ "description": "Get a list of a descriptors", "parameters": [ { - "name": "accept", + "name": "Accept", "in": "header", "description": "requested Content-Type", "required": true, @@ -972,7 +972,7 @@ "$ref": "#/parameters/InChI" }, { - "name": "accept", + "name": "Accept", "in": "header", "description": "requested Content-type", "required": true, @@ -1144,6 +1144,7 @@ }, "responses": { "200": { + "schema": {"type":"string"}, "description": "OK" }, "400": { diff --git a/lib/dataset.rb b/lib/dataset.rb index a48b23f..7c74f39 100644 --- a/lib/dataset.rb +++ b/lib/dataset.rb @@ -7,10 +7,9 @@ get "/dataset/?" do return uri_list.join("\n") + "\n" when "application/json" datasets = JSON.parse datasets.to_json - datasets.each_index do |idx| - datasets[idx][:URI] = uri("/dataset/#{datasets[idx]["_id"]["$oid"]}") - end - return datasets.to_json + list = [] + datasets.each{|d| list << uri("/dataset/#{d["_id"]["$oid"]}")} + return list.to_json else bad_request_error "Mime type #{@accept} is not supported." end @@ -19,7 +18,7 @@ end # Get a dataset get "/dataset/:id/?" do dataset = Dataset.find :id => params[:id] - resource_not_found_error "Dataset with id: #{params[:id]} not found." unless dataset + not_found_error "Dataset with id: #{params[:id]} not found." unless dataset case @accept when "application/json" dataset.data_entries.each do |k, v| @@ -39,9 +38,9 @@ end # Get a dataset attribute. One of compounds, nanoparticles, substances, features get "/dataset/:id/:attribute/?" do dataset = Dataset.find :id => params[:id] - resource_not_found_error "Dataset with id: #{params[:id]} not found." unless dataset + not_found_error "Dataset with id: #{params[:id]} not found." unless dataset attribs = ["compounds", "nanoparticles", "substances", "features"] return "Attribute '#{params[:attribute]}' is not available. Choose one of #{attribs.join(', ')}." unless attribs.include? params[:attribute] - out = dataset.send(params[:attribute]) + out = dataset.send("#{params[:attribute]}") return out.to_json end diff --git a/lib/feature.rb b/lib/feature.rb index 75e3f1f..140cc25 100644 --- a/lib/feature.rb +++ b/lib/feature.rb @@ -18,8 +18,13 @@ end # Get a feature get "/feature/:id/?" do - feature = Feature.find :id => params[:id] - resource_not_found_error "Feature with id: #{params[:id]} not found." unless feature - feature[:URI] = uri("/feature/#{feature.id}") - return feature.to_json -end \ No newline at end of file + case @accept + when "application/json" + feature = Feature.find :id => params[:id] + not_found_error "Feature with id: #{params[:id]} not found." unless feature + feature[:URI] = uri("/feature/#{feature.id}") + return feature.to_json + else + bad_request_error "Mime type #{@accept} is not supported." + end +end diff --git a/lib/lazar-rest.rb b/lib/lazar-rest.rb index 41010e1..4fd806c 100644 --- a/lib/lazar-rest.rb +++ b/lib/lazar-rest.rb @@ -3,15 +3,19 @@ require "sinatra/reloader" require 'sinatra/cross_origin' configure do - enable :reloader if development? + enable :reloader #if development? enable :cross_origin + disable :show_exceptions + disable :raise_errors end #set :protection, :except => :frame_options # Environment setup from unicorn -E param ENV["LAZAR_ENV"] = ENV["RACK_ENV"] - +require "../lazar/lib/lazar.rb" +require "../qsar-report/lib/qsar-report.rb" +=begin if ENV["LAZAR_ENV"] == "development" require "../lazar/lib/lazar.rb" require "../qsar-report/lib/qsar-report.rb" @@ -19,28 +23,28 @@ else require "lazar" require "qsar-report" end +=end include OpenTox -#require 'rack/cors' - -set :show_exceptions => false -=begin -# add CORS support for swagger - config.allow do |allow| - allow.origins '*' - allow.resource "/#{SERVICE}/*", - :methods => [:head, :get, :post, :put, :delete, :options], - :headers => :any, - :max_age => 0 - end -end -=end before do - @accept = request.env['HTTP_ACCEPT'].split.last + @accept = request.env['HTTP_ACCEPT'] response['Content-Type'] = @accept end +not_found do + 400 + "Path '#{request.env["REQUEST_PATH"]}' not found.\n" +end + +error do + response['Content-Type'] = "text/plain" + error = request.env['sinatra.error'] + body = error.message+"\n" + error.respond_to?(:http_code) ? code = error.http_code : code = 500 + halt code, body +end + # https://github.com/britg/sinatra-cross_origin#responding-to-options options "*" do response.headers["Allow"] = "HEAD,GET,PUT,POST,DELETE,OPTIONS" @@ -48,8 +52,6 @@ options "*" do 200 end - - [ "aa.rb", "api.rb", @@ -63,3 +65,4 @@ end "swagger.rb", "validation.rb" ].each{ |f| require_relative f } + diff --git a/lib/model.rb b/lib/model.rb index 22094e9..7a2fb47 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -10,11 +10,9 @@ get "/model/?" do return uri_list.join("\n") + "\n" when "application/json" models = JSON.parse models.to_json - models.each_index do |idx| - models[idx][:URI] = uri("/model/#{models[idx]["model_id"]["$oid"]}") - models[idx][:crossvalidation_uri] = uri("/crossvalidation/#{models[idx]["crossvalidation_id"]["$oid"]}") if models[idx]["crossvalidation_id"] - end - return models.to_json + list = [] + models.each{|m| list << uri("/model/#{m["model_id"]["$oid"]}")} + return list.to_json else bad_request_error "Mime type #{@accept} is not supported." end @@ -22,7 +20,7 @@ end get "/model/:id/?" do model = Model::Lazar.find params[:id] - resource_not_found_error "Model with id: #{params[:id]} not found." unless model + not_found_error "Model with id: #{params[:id]} not found." unless model model[:URI] = uri("/model/#{model.id}") # model[:neighbor_algorithm_parameters][:feature_dataset_uri] = uri("/dataset/#{model[:neighbor_algorithm_parameters][:feature_dataset_id]}") if model[:neighbor_algorithm_parameters][:feature_dataset_id] model[:training_dataset_uri] = uri("/dataset/#{model.training_dataset_id}") if model.training_dataset_id diff --git a/lib/nanoparticle.rb b/lib/nanoparticle.rb index f32d834..332493d 100644 --- a/lib/nanoparticle.rb +++ b/lib/nanoparticle.rb @@ -18,8 +18,13 @@ end # Get a nanoparticle get "/nanoparticle/:id/?" do - nanoparticle = Nanoparticle.find :id => params[:id] - resource_not_found_error "Nanoparticle with id: #{params[:id]} not found." unless nanoparticle - nanoparticle[:URI] = uri("/nanoparticle/#{nanoparticle.id}") - return nanoparticle.to_json -end \ No newline at end of file + case @accept + when "application/json" + nanoparticle = Nanoparticle.find :id => params[:id] + not_found_error "Nanoparticle with id: #{params[:id]} not found." unless nanoparticle + nanoparticle[:URI] = uri("/nanoparticle/#{nanoparticle.id}") + return nanoparticle.to_json + else + bad_request_error "Mime type #{@accept} is not supported." + end +end diff --git a/lib/report.rb b/lib/report.rb index 9dff2f2..ae2526c 100644 --- a/lib/report.rb +++ b/lib/report.rb @@ -25,7 +25,7 @@ end get "/report/:id/?" do model = Model::Lazar.find params[:id] - resource_not_found_error "Model with id: #{params[:id]} not found." unless model + not_found_error "Model with id: #{params[:id]} not found." unless model prediction_model = Model::Validation.find_by :model_id => params[:id] validation_template = File.join(File.dirname(__FILE__),"../views/model_details.haml") diff --git a/lib/substance.rb b/lib/substance.rb index 1116e65..fef1b7e 100644 --- a/lib/substance.rb +++ b/lib/substance.rb @@ -18,8 +18,13 @@ end # Get a substance get "/substance/:id/?" do - substance = Substance.find :id => params[:id] - resource_not_found_error "Substance with id: #{params[:id]} not found." unless substance - substance[:URI] = uri("/substance/#{substance.id}") - return substance.to_json -end \ No newline at end of file + case @accept + when "application/json" + substance = Substance.find :id => params[:id] + not_found_error "Substance with id: #{params[:id]} not found." unless substance + substance[:URI] = uri("/substance/#{substance.id}") + return substance.to_json + else + bad_request_error "Mime type #{@accept} is not supported." + end +end diff --git a/lib/swagger.rb b/lib/swagger.rb index 916439e..f6b42b2 100644 --- a/lib/swagger.rb +++ b/lib/swagger.rb @@ -3,6 +3,6 @@ set :public_folder, File.join("/home/ist/swagger-ui/dist/") get "/" do response['Content-Type'] = "text/html" index_file = File.join("/home/ist/swagger-ui/dist/index.html") - #bad_request_error "API Documentation in Swagger JSON is not implemented.", uri("/#{SERVICE}/api") unless File.exists?(index_file) + bad_request_error "API Documentation in Swagger JSON is not implemented.", uri("/#{SERVICE}/api") unless File.exists?(index_file) File.read(index_file) end diff --git a/lib/validation.rb b/lib/validation.rb index ce31c34..fad8a44 100644 --- a/lib/validation.rb +++ b/lib/validation.rb @@ -62,10 +62,10 @@ get "/validation/:validationtype/:id/?" do validation = Validation::RegressionCrossValidation.find params[:id] end - resource_not_found_error "#{params[:validationtype]} with id: #{params[:id]} not found." unless validation + not_found_error "#{params[:validationtype]} with id: #{params[:id]} not found." unless validation #model[:URI] = uri("/model/#{model.id}") #model[:neighbor_algorithm_parameters][:feature_dataset_uri] = uri("/dataset/#{model[:neighbor_algorithm_parameters][:feature_dataset_id]}") if model[:neighbor_algorithm_parameters][:feature_dataset_id] #model[:training_dataset_uri] = uri("/dataset/#{model.training_dataset_id}") if model.training_dataset_id #model[:prediction_feature_uri] = uri("/dataset/#{model.prediction_feature_id}") if model.prediction_feature_id return validation.to_json -end \ No newline at end of file +end diff --git a/test/descriptor.rb b/test/descriptor.rb index db4b6c7..14c8602 100644 --- a/test/descriptor.rb +++ b/test/descriptor.rb @@ -9,7 +9,7 @@ class DescriptorTest < MiniTest::Test result = RestClientWrapper.get File.join($host, "compound/descriptor"), {}, {:accept => "text/plain"} assert_equal result.code, 200 assert result.include?("Joelib.KierShape1: JOELIb does not provide meaningful descriptions, see java/JoelibDescriptors.java for details.\nJoelib.KierShape2: JOELIb does not provide meaningful descriptions, see java/JoelibDescriptors.java for details."), "Descriptor list is not complete." - assert_equal 355, result.lines.count + assert_equal 346, result.lines.count end def test_01_get_descriptor diff --git a/test/setup.rb b/test/setup.rb index 1c47dff..aa17e54 100644 --- a/test/setup.rb +++ b/test/setup.rb @@ -2,7 +2,7 @@ require 'minitest/autorun' require_relative '../../lazar/lib/lazar.rb' require_relative '../../lazar/test/setup.rb' -$host = "https://enm.in-silico.ch" +$host = "https://api.in-silico.ch" include OpenTox REST_TEST_DIR ||= File.expand_path(File.dirname(__FILE__)) REST_DATA_DIR ||= File.join(REST_TEST_DIR,"data") -- cgit v1.2.3