From 1c8b32dffc1f9002f5285ad56cb26b5a92a24663 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Wed, 30 Jun 2021 17:04:19 +0200 Subject: [PATCH] Add biomolecular toolkit API proxying endpoint [SCI-5843] --- .../bio_eddie_assets_controller.rb | 21 +++++++++++++++++-- config/routes.rb | 4 ++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/app/controllers/bio_eddie_assets_controller.rb b/app/controllers/bio_eddie_assets_controller.rb index d282031c0..010f0263e 100644 --- a/app/controllers/bio_eddie_assets_controller.rb +++ b/app/controllers/bio_eddie_assets_controller.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 7ee8e92ef..315caeea6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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