From ea5e7c1afe9e4fddf908ce8530c011767b4f8b71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20H=C3=A4nsel?= Date: Wed, 4 Dec 2024 19:54:30 +0100 Subject: [PATCH] Update POST request & Error Handling --- app/controllers/projects_controller.rb | 49 ++++++++++++++++++++++++-- lib/nfdi4health/csh_client.rb | 27 ++++++++++++++ 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index ef87afd2e4..f642a6913d 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -545,7 +545,7 @@ def publish_to_csh @project = Project.find(params[:id]) data_project = Nfdi4Health::Preparation_json.new - transforming_api_data = data_project.transforming_api(@project, ProjectSerializer, 'Projects') + transforming_api_data = data_project.transforming_api(@project, ProjectSerializer, 'projects') begin endpoints = Nfdi4Health::Client.new() endpoints.send_transforming_api(transforming_api_data.to_json) @@ -662,12 +662,13 @@ def publish_to_csh end return end + if !JSON.parse(JSON.parse(endpoints.to_json)['endpoint'])['resource'].nil? identifier = JSON.parse(JSON.parse(endpoints.to_json)['endpoint'])['resource']['identifier'] if !sender_project_merged['resource']['identifier'].nil? - flash[:notice] ="#{t('project')} was successfully updated with ID #{identifier}." + flash[:notice] ="#{t('project')} was successfully updated with ID #{identifier}. Your data is still incomplete. Please consider the error message" else - flash[:notice] ="#{t('project')} was successfully published with ID #{identifier}." + flash[:notice] ="#{t('project')} was successfully created with ID #{identifier}. Your data is still incomplete. Please consider the error message" em = @project.extended_metadata jem = JSON.parse(em.json_metadata) jem['Resource_identifier_Project'] = identifier @@ -677,6 +678,48 @@ def publish_to_csh flash[:notice] = JSON.parse(JSON.parse(endpoints.to_json)['endpoint']) end + #csh confirm post request with ID + begin + + endpoints.publish_csh_confirm(identifier,one_time_token) + + rescue RestClient::ExceptionWithResponse => e + flash[:error] = endpoints.handle_restclient_error(e,'confirm_publish_csh') + respond_to do |format| + format.html { redirect_to(@project) } + format.rdf { render template: 'rdf/show' } + format.json { render json: { error: flash[:error] }, status: :unprocessable_entity } + end + return + rescue RestClient::RequestTimeout + flash[:error] = 'Request Timeout: The server took too long to respond.' + respond_to do |format| + format.html { redirect_to(@project) } + format.rdf { render template: 'rdf/show' } + format.json { render json: { error: flash[:error] }, status: :unprocessable_entity } + end + return + rescue SocketError + flash[:error] = 'Network Error: Please check your internet connection.' + respond_to do |format| + format.html { redirect_to(@project) } + format.rdf { render template: 'rdf/show' } + format.json { render json: { error: flash[:error] }, status: :unprocessable_entity } + end + return + rescue StandardError => e + flash[:error] = "An unexpected error occurred: #{e.message}" + respond_to do |format| + format.html { redirect_to(@project) } + format.rdf { render template: 'rdf/show' } + format.json { render json: { error: flash[:error] }, status: :unprocessable_entity } + end + return + end + + flash[:notice] ="#{t('project')} was successfully confirmed to be published with ID #{identifier}. Your submission will be checked by a data steward. Afterward you will be notified by email." + + respond_to do |format| format.html { redirect_to(@project) } format.rdf { render template: 'rdf/show' } diff --git a/lib/nfdi4health/csh_client.rb b/lib/nfdi4health/csh_client.rb index 50417d3a33..138828939e 100644 --- a/lib/nfdi4health/csh_client.rb +++ b/lib/nfdi4health/csh_client.rb @@ -19,6 +19,14 @@ def publish_csh(project_transformed,token) @endpoint = RestClient::Request.execute(method: :post, url: @url_publish, payload: project_transformed, headers: headers) end + def publish_csh_confirm(id,token) + headers = { content_type: 'application/json',Content_Length: '0' ,Host: 'csh.nfdi4health.de', Authorization: 'Bearer ' + token + } + + @endpoint = RestClient::Request.execute(method: :post, url: "#{@url_publish}#{id}/publish", headers: headers) + + end + def send_transforming_api(project) @transformed = RestClient::Request.execute(method: :post, url: @url,payload: project, headers: { content_type: :json, accept: :json }).body end @@ -93,6 +101,25 @@ def handle_restclient_error(e, name_server) else "An unexpected error occurred: #{e.response}" end + when 'confirm_publish_csh' + case e.response.code + when 200 + 'pub_csh:CODE200- No new draft version created because uploaded resource contains no changes' + when 400 + 'pub_csh:CODE400- Bad Request: The server could not understand the request.' + when 401 + 'pub_csh:CODE401- Not allowed to edit resource. User must either be the original creator of the resource or have been added as a collaborator.' + when 403 + 'pub_csh:CODE403- Forbidden: You do not have the necessary permissions to access this resource.' + when 404 + 'pub_csh:CODE404- Not Found: The requested resource could not be found.' + when 422 + "#{JSON.parse(JSON.parse(e.response.to_json))['error']['message']}. Error(s) caused by: #{JSON.parse(JSON.parse(e.response.to_json))['error']['paths']}" + when 500 + 'pub_csh:CODE500- Internal Server Error: The server encountered an error and could not complete your request.' + else + "An unexpected error occurred: #{e.response}" + end end end end