2021-09-06 10:49:12 +02:00
|
|
|
class APIParticulier::API
|
|
|
|
include APIParticulier::Error
|
|
|
|
|
|
|
|
INTROSPECT_RESOURCE_NAME = "introspect"
|
2021-09-17 20:42:02 +02:00
|
|
|
COMPOSITION_FAMILIALE_RESOURCE_NAME = "v2/composition-familiale"
|
2021-09-06 10:49:12 +02:00
|
|
|
|
|
|
|
TIMEOUT = 20
|
|
|
|
|
|
|
|
def initialize(token)
|
|
|
|
@token = token
|
|
|
|
end
|
|
|
|
|
|
|
|
def scopes
|
2021-09-21 12:14:07 +02:00
|
|
|
get(INTROSPECT_RESOURCE_NAME)['scopes']
|
2021-09-06 10:49:12 +02:00
|
|
|
end
|
|
|
|
|
2021-09-17 20:42:02 +02:00
|
|
|
def composition_familiale(numero_allocataire, code_postal)
|
|
|
|
get(COMPOSITION_FAMILIALE_RESOURCE_NAME,
|
|
|
|
numeroAllocataire: numero_allocataire,
|
|
|
|
codePostal: code_postal)
|
|
|
|
end
|
|
|
|
|
2021-09-06 10:49:12 +02:00
|
|
|
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?
|
2021-09-21 12:14:07 +02:00
|
|
|
JSON.parse(response.body)
|
2021-09-06 10:49:12 +02:00
|
|
|
elsif response.code == 401
|
|
|
|
raise Unauthorized.new(response)
|
|
|
|
else
|
|
|
|
raise RequestFailed.new(response)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|