Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update POST request & Error Handling #75

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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' }
Expand Down
27 changes: 27 additions & 0 deletions lib/nfdi4health/csh_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading