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-11-10 18:02:56 +01:00
|
|
|
AVIS_IMPOSITION_RESOURCE_NAME = "v2/avis-imposition"
|
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-11-10 18:02:56 +01:00
|
|
|
def avis_imposition(numero_fiscal, reference_avis)
|
|
|
|
# NOTE: Il est possible que l'utilisateur ajoute une quatorzième lettre à la fin de sa référence d'avis.
|
|
|
|
# Il s'agit d'une clé de vérification qu'il est nécessaire de'enlever avant de contacter API Particulier.
|
|
|
|
get(AVIS_IMPOSITION_RESOURCE_NAME,
|
|
|
|
numeroFiscal: numero_fiscal.to_i.to_s.rjust(13, "0"),
|
|
|
|
referenceAvis: reference_avis.to_i.to_s.rjust(13, "0"))
|
|
|
|
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
|