-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.py
More file actions
42 lines (34 loc) · 1.26 KB
/
Copy pathpython.py
File metadata and controls
42 lines (34 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""CFE API - Python example.
Requirements: requests (`pip install requests`).
"""
import os
import requests
API = "https://cfe-api.fly.dev"
API_KEY = os.environ["CFE_API_KEY"]
def consulta(rpu: str, nombre: str, provider: str | None = None,
total_a_pagar: str | None = None) -> dict:
body = {"rpu": rpu, "nombre": nombre}
if provider:
body["provider"] = provider
# total_a_pagar (monto actual a pagar, sin decimales) es requerido cuando el
# proveedor efectivo es "micfe": CFE valida rpu + nombre (razón social) + total.
if total_a_pagar:
body["total_a_pagar"] = total_a_pagar
r = requests.post(
f"{API}/api/v1/consulta",
headers={"X-API-Key": API_KEY},
json=body,
timeout=120,
)
r.raise_for_status()
return r.json()
def balance() -> dict:
r = requests.get(f"{API}/api/v1/balance", headers={"X-API-Key": API_KEY})
r.raise_for_status()
return r.json()
if __name__ == "__main__":
result = consulta("123456789012", "JUAN PEREZ")
data = result["data"]
print(f"Tarifa: {data['tarifa']} Anual: {data['annual_kwh']} kWh")
print(f"Hilos: {data.get('hilos') or 'no disponible'}")
print(f"Cobrado: {result['charged_cents']/100:.2f} MXN Cache: {result['cached']}")