Last active
July 28, 2025 13:53
-
-
Save erajuan/1d43b275290ee3962a8a2f07e80f48c3 to your computer and use it in GitHub Desktop.
Consulta de api ruc sunat, api dni reniec y tipo de cambio sunat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import List, Optional | |
import logging | |
import requests | |
class DecolectaAPIClient: | |
""" | |
Cliente para consumir los servicios públicos de la API de Decolecta. | |
""" | |
def __init__(self, api_token: str = None) -> None: | |
self.api_token = api_token | |
self.base_url = "https://api.decolecta.com" | |
def _get(self, endpoint: str, params: dict) -> Optional[dict]: | |
""" | |
Realiza una solicitud GET autenticada al endpoint indicado. | |
""" | |
url = f"{self.base_url}{endpoint}" | |
headers = { | |
"Authorization": f"Bearer {self.api_token}", | |
"Referer": "python-decolecta" | |
} | |
response = requests.get(url, headers=headers, params=params) | |
if response.status_code == 200: | |
return response.json() | |
elif response.status_code == 422: | |
logging.warning(f"{response.url} - Parámetros inválidos: {params}") | |
logging.warning(response.text) | |
elif response.status_code == 403: | |
logging.warning(f"{response.url} - Acceso denegado: IP bloqueada") | |
elif response.status_code == 429: | |
logging.warning(f"{response.url} - Demasiadas solicitudes: aplicar retardo") | |
elif response.status_code == 401: | |
logging.warning(f"{response.url} - Token inválido o sin permisos") | |
else: | |
logging.warning(f"{response.url} - Error del servidor: código {response.status_code}") | |
return None | |
def get_person_by_dni(self, dni: str) -> Optional[dict]: | |
""" | |
Consulta datos personales por DNI (RENIEC). | |
""" | |
return self._get("/v1/reniec/dni", {"numero": dni}) | |
def get_company_by_ruc(self, ruc: str) -> Optional[dict]: | |
""" | |
Consulta información de empresa por RUC (SUNAT). | |
""" | |
return self._get("/v1/sunat/ruc", {"numero": ruc}) | |
def get_exchange_rate_by_date(self, date: str) -> Optional[dict]: | |
""" | |
Obtiene el tipo de cambio para una fecha específica (formato YYYY-MM-DD). | |
""" | |
return self._get("/v1/tipo-cambio/sunat", {"date": date}) | |
def get_exchange_rate_today(self) -> Optional[dict]: | |
""" | |
Obtiene el tipo de cambio del día actual. | |
""" | |
return self._get("/v1/tipo-cambio/sunat", {}) | |
def get_exchange_rate_by_month(self, month: int, year: int) -> Optional[List[dict]]: | |
""" | |
Obtiene el tipo de cambio para un mes y año específicos. | |
""" | |
return self._get("/v1/tipo-cambio/sunat", {"month": month, "year": year}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
para consultar DNI, como lo valido en el postman?