demarches-normaliennes/app/lib/api_particulier/api.rb
simon lehericey 87cb16093f fetch token introspection
Co-authored-by: François VANTOMME <akarzim@gmail.com>
2021-09-15 14:37:04 +02:00

34 lines
703 B
Ruby

class APIParticulier::API
include APIParticulier::Error
INTROSPECT_RESOURCE_NAME = "introspect"
TIMEOUT = 20
def initialize(token)
@token = token
end
def scopes
get(INTROSPECT_RESOURCE_NAME)[:scopes]
end
private
def get(resource_name, params = {})
url = [API_PARTICULIER_URL, resource_name].join("/")
response = Typhoeus.get(url,
headers: { accept: "application/json", "X-API-Key": @token },
params: params,
timeout: TIMEOUT)
if response.success?
JSON.parse(response.body, symbolize_names: true)
elsif response.code == 401
raise Unauthorized.new(response)
else
raise RequestFailed.new(response)
end
end
end