Add biomolecular toolkit API proxying endpoint [SCI-5843]

This commit is contained in:
Oleksii Kriuchykhin 2021-06-30 17:04:19 +02:00
parent 786e74e4de
commit 1c8b32dffc
2 changed files with 23 additions and 2 deletions

View file

@ -3,10 +3,10 @@
class BioEddieAssetsController < ApplicationController
include ActiveStorage::SetCurrent
before_action :load_vars, except: :create
before_action :load_vars, except: %i(create bmt_request)
before_action :load_create_vars, only: :create
before_action :check_read_permission
before_action :check_read_permission, except: %i(update create start_editing bmt_request)
before_action :check_edit_permission, only: %i(update create start_editing)
def create
@ -38,6 +38,23 @@ class BioEddieAssetsController < ApplicationController
end
end
def bmt_request
return_404 unless ENV['BIOMOLECULE_TOOLKIT_BASE_URL']
uri = URI.parse(ENV['BIOMOLECULE_TOOLKIT_BASE_URL'])
uri.path = request.original_fullpath.remove('/biomolecule_toolkit')
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
api_request = "Net::HTTP::#{request.request_method.capitalize}".constantize.new(uri)
api_request['x-api-key'] = ENV['BIOMOLECULE_TOOLKIT_API_KEY'] if ENV['BIOMOLECULE_TOOLKIT_API_KEY']
api_request['Content-Type'] = 'application/json'
request_body = request.body.read
api_request.body = request_body if request_body.present?
api_response = http.request(api_request)
render body: api_response.body, content_type: api_response.content_type, status: api_response.code
end
end
def start_editing
# Activity here
end

View file

@ -779,6 +779,10 @@ Rails.application.routes.draw do
end
end
match 'biomolecule_toolkit/*path', to: 'bio_eddie_assets#bmt_request',
via: %i(get post put delete),
defaults: { format: 'json' }
post 'global_activities', to: 'global_activities#index'
constraints WopiSubdomain do