From 878f014ec6cc808af99af5045bcc1a1143cab8d9 Mon Sep 17 00:00:00 2001 From: gebele Date: Thu, 5 Jul 2018 10:38:55 +0000 Subject: updated with endpoint list; refined error handling; refined prediction input --- api/api.json | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- application.rb | 16 ++++++++--- lib/endpoint.rb | 23 ++++++++++++++++ lib/model.rb | 2 +- 4 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 lib/endpoint.rb diff --git a/api/api.json b/api/api.json index 6aa95ad..718421f 100644 --- a/api/api.json +++ b/api/api.json @@ -32,6 +32,75 @@ "url": "https://github.com/opentox/lazar-rest" }, "paths": { + "/endpoint": { + "get": { + "x-orn-@type": "x-orn:Endpoint", + "x-orn:path": "https://lazar.prod.openrisknet.org/endpoint", + "x-orn:method": "Get", + "tags": [ + "endpoint" + ], + "description": "Get a list of all available endpoints", + "parameters": [ + { + "$ref": "#/components/parameters/subjectid" + } + ], + "responses": { + "200": { + "$ref": "#/components/responses/200" + }, + "400": { + "$ref": "#/components/responses/400" + }, + "401": { + "$ref": "#/components/responses/401" + }, + "404": { + "$ref": "#/components/responses/404" + }, + "500": { + "$ref": "#/components/responses/500" + } + } + } + }, + "/endpoint/{endpoint}": { + "get": { + "x-orn-@type": "x-orn:Endpoint", + "x-orn:path": "https://lazar.prod.openrisknet.org/endpoint", + "x-orn:method": "Get", + "tags": [ + "endpoint" + ], + "description": "Get a list of all available models for an endpoint", + "parameters": [ + { + "$ref": "#/components/parameters/endpoint" + }, + { + "$ref": "#/components/parameters/subjectid" + } + ], + "responses": { + "200": { + "$ref": "#/components/responses/200" + }, + "400": { + "$ref": "#/components/responses/400" + }, + "401": { + "$ref": "#/components/responses/401" + }, + "404": { + "$ref": "#/components/responses/404" + }, + "500": { + "$ref": "#/components/responses/500" + } + } + } + }, "/model": { "get": { "x-orn-@type": "x-orn:Model", @@ -107,7 +176,7 @@ "tags": [ "model" ], - "description": "Predict a compound or a nanoparticle", + "description": "Predict a compound or a nanoparticle. Also a comma seperated list is allowed.", "parameters": [ { "$ref": "#/components/parameters/id" @@ -1030,6 +1099,10 @@ "name": "descriptor", "description": "Descriptor" }, + { + "name": "endpoint", + "description": "Endpoint" + }, { "name": "feature", "description": "Feature" @@ -1116,6 +1189,15 @@ "type": "string" } }, + "endpoint": { + "name": "endpoint", + "in": "path", + "description": "type", + "required": true, + "schema": { + "type": "string" + } + }, "InChI": { "name": "InChI", "in": "path", diff --git a/application.rb b/application.rb index ebcb0af..a5f9ff2 100644 --- a/application.rb +++ b/application.rb @@ -7,6 +7,7 @@ include OpenTox "api.rb", "compound.rb", "dataset.rb", + "endpoint.rb", "feature.rb", "model.rb", "nanoparticle.rb", @@ -28,12 +29,13 @@ configure :development do end before do - paths = [ + $paths = [ "/", "aa", "api", "compound", "dataset", + "endpoint", "feature", "model", "nanoparticle", @@ -41,7 +43,7 @@ before do "substance", "swagger", "validation"] - if request.path == "/" || paths.include?(request.path.split("/")[1]) + if request.path == "/" || $paths.include?(request.path.split("/")[1]) @accept = request.env['HTTP_ACCEPT'] response['Content-Type'] = @accept else @@ -54,8 +56,14 @@ not_found do end error do - @error = request.env['sinatra.error'] - haml :error + if request.path == "/" || $paths.include?(request.path.split("/")[1]) + @accept = request.env['HTTP_ACCEPT'] + response['Content-Type'] = @accept + @accept == "text/plain" ? request.env['sinatra.error'] : request.env['sinatra.error'].to_json + else + @error = request.env['sinatra.error'] + haml :error + end end # https://github.com/britg/sinatra-cross_origin#responding-to-options diff --git a/lib/endpoint.rb b/lib/endpoint.rb new file mode 100644 index 0000000..ef39787 --- /dev/null +++ b/lib/endpoint.rb @@ -0,0 +1,23 @@ +# Get a list of all endpoints +# @param [Header] Accept one of text/uri-list, +# @return [text/uri-list] list of all prediction models +get "/endpoint/?" do + models = Model::Validation.all + endpoints = models.collect{|m| m.endpoint}.uniq + case @accept + when "text/uri-list" + return endpoints.join("\n") + "\n" + when "application/json" + return endpoints.to_json + else + bad_request_error "Mime type #{@accept} is not supported." + end +end + +get "/endpoint/:endpoint/?" do + models = Model::Validation.where(endpoint: params[:endpoint]) + list = [] + models.each{|m| list << {m.species => uri("/model/#{m.id}")} } + not_found_error "Endpoint: #{params[:endpoint]} not found." if models.blank? + return list.to_json +end diff --git a/lib/model.rb b/lib/model.rb index 9fbd90f..3764ee2 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -27,7 +27,7 @@ end post "/model/:id/?" do identifier = params[:identifier].split(",") - compounds = identifier.collect{ |i| Compound.from_smiles i.strip } + compounds = identifier.collect{ |i| Compound.from_smiles i.strip.gsub(/\A"|"\Z/,'') } model = Model::Validation.find params[:id] batch = {} compounds.each do |compound| -- cgit v1.2.3