demarches-normaliennes/app/services/rnf_service.rb

40 lines
865 B
Ruby
Raw Permalink Normal View History

# frozen_string_literal: true
2023-10-27 14:20:40 +02:00
class RNFService
include Dry::Monads[:result]
def call(rnf_id:)
result = API::Client.new.(url: "#{url}/#{rnf_id}", schema:, headers:, typhoeus_options:)
2023-10-27 14:20:40 +02:00
case result
in Success(body:)
Success(body)
in Failure(code:, reason:) if code.in?(401..403)
Failure(API::Client::Error[:unauthorized, code, false, reason])
2024-03-29 12:20:04 +01:00
# 400 errors are due to invalid rfn code
# it cannot be fixed so we do not retry
in Failure(code: 400, reason:)
Failure(API::Client::Error[:bad_request, code, false, reason])
2023-10-27 14:20:40 +02:00
else
result
end
end
private
def headers
{ Token: ENV['RNF_TOKEN'] }
end
def typhoeus_options
{ ssl_verifyhost: 0 }
end
2023-10-27 14:20:40 +02:00
def schema
JSONSchemer.schema(Rails.root.join('app/schemas/rnf.json'))
end
def url
"#{API_RNF_URL}/api/foundations"
end
end