diff --git a/app/controllers/madmp_codebase_controller.rb b/app/controllers/madmp_codebase_controller.rb index aacef8d..b2c829c 100644 --- a/app/controllers/madmp_codebase_controller.rb +++ b/app/controllers/madmp_codebase_controller.rb @@ -7,7 +7,7 @@ def run fragment = MadmpFragment.find(params[:fragment_id]) script_id = params[:script_id] - response = fetch_run_data(fragment, script_id) + @response = fetch_run_data(fragment, script_id) # fragment.save_codebase_fragment(response.data, fragment.madmp_schema) # EXAMPLE DATA : CODEBASE NEEDS FIXES @@ -16,7 +16,7 @@ # @fragment.save_codebase_fragment(response, @fragment.madmp_schema) authorize fragment - render json: response.to_json + render json: @response.to_json end private @@ -24,12 +24,14 @@ def fetch_run_data(fragment, script_id) return {} unless fragment.present? && script_id.present? - ExternalApis::MadmpCodebaseService.run(script_id, body: - { - "data": fragment.data, - "schema": {}, - "dmp_id": fragment.dmp_id - }) + Rails.cache.fetch(["codebase_run", fragment.id], expires_in: 86_400) do + ExternalApis::MadmpCodebaseService.run(script_id, body: + { + "data": fragment.data, + "schema": {}, + "dmp_id": fragment.dmp_id + }) + end end end diff --git a/app/services/external_apis/base_service.rb b/app/services/external_apis/base_service.rb index b80e839..fefcdee 100644 --- a/app/services/external_apis/base_service.rb +++ b/app/services/external_apis/base_service.rb @@ -108,16 +108,16 @@ resp end - # Makes a GET request to the specified uri with the additional headers and body. + # Makes a POST request to the specified uri with the additional headers. # Additional headers are combined with the base headers defined above. - def http_post(uri:, body: {}, additional_headers: {}) + # rubocop:disable Metrics/MethodLength + def http_post(uri:, additional_headers: {}, data: {}, basic_auth: nil, debug: false) return nil unless uri.present? - HTTParty.post( - uri, - body: body, - headers: headers.merge(additional_headers) - ) + opts = options(additional_headers: additional_headers, debug: debug) + opts[:body] = data + opts[:basic_auth] = basic_auth if basic_auth.present? + HTTParty.post(uri, opts) rescue URI::InvalidURIError => e handle_uri_failure(method: "BaseService.http_post #{e.message}", uri: uri) @@ -127,6 +127,7 @@ http_response: resp) resp end + # rubocop:enable Metrics/MethodLength # Options for the HTTParty call def options(additional_headers: {}, debug: false) diff --git a/app/services/external_apis/madmp_codebase_service.rb b/app/services/external_apis/madmp_codebase_service.rb index e969a22..4a0902a 100644 --- a/app/services/external_apis/madmp_codebase_service.rb +++ b/app/services/external_apis/madmp_codebase_service.rb @@ -46,7 +46,12 @@ target = "#{api_base_url}#{run_path % script_id}" - resp = http_post(uri: target, body: body.to_json) + resp = http_post( + uri: target, + additional_headers: hdrs, debug: false, + data: body.to_json + ) + unless resp.code == 200 handle_http_failure(method: "MadmpCodebase run", http_response: resp) return [] diff --git a/config/routes.rb b/config/routes.rb index 0cf5e6b..6765388 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -136,7 +136,7 @@ get "create_contributor", action: :create_contributor, on: :collection end - get "/codebase/run", to: "madmp_codebase#run" + get "/codebase/run", to: "madmp_codebase#run", constraints: {format: [:json]} resources :research_outputs, only: [] do post "sort", on: :collection