support better errorhandling for codeharbor
This commit is contained in:
34
app/services/exercise_service/check_external.rb
Normal file
34
app/services/exercise_service/check_external.rb
Normal file
@ -0,0 +1,34 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module ExerciseService
|
||||
class CheckExternal < ServiceBase
|
||||
def initialize(uuid:, codeharbor_link:)
|
||||
@uuid = uuid
|
||||
@codeharbor_link = codeharbor_link
|
||||
end
|
||||
|
||||
def execute
|
||||
response = connection.post do |req|
|
||||
req.headers['Content-Type'] = 'application/json'
|
||||
req.headers['Authorization'] = 'Bearer ' + @codeharbor_link.api_key
|
||||
req.body = {uuid: @uuid}.to_json
|
||||
end
|
||||
response_hash = JSON.parse(response.body, symbolize_names: true)
|
||||
|
||||
{error: false}.merge(response_hash.slice(:message, :exercise_found, :update_right))
|
||||
rescue Faraday::Error => e
|
||||
{error: true, message: t('exercises.export_exercise.error', message: e.message)}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def connection
|
||||
Faraday.new(url: @codeharbor_link.check_uuid_url) do |faraday|
|
||||
faraday.options[:open_timeout] = 5
|
||||
faraday.options[:timeout] = 5
|
||||
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -10,11 +10,7 @@ module ExerciseService
|
||||
def execute
|
||||
body = @zip.string
|
||||
begin
|
||||
conn = Faraday.new(url: @codeharbor_link.push_url) do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
response = conn.post do |request|
|
||||
response = connection.post do |request|
|
||||
request.headers['Content-Type'] = 'application/zip'
|
||||
request.headers['Content-Length'] = body.length.to_s
|
||||
request.headers['Authorization'] = 'Bearer ' + @codeharbor_link.api_key
|
||||
@ -26,5 +22,13 @@ module ExerciseService
|
||||
return e.message
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def connection
|
||||
Faraday.new(url: @codeharbor_link.push_url) do |faraday|
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user