import httpx import asyncio import json import os from dotenv import load_dotenv load_dotenv() async def test_oc_api(): ticket = os.getenv("MERCADO_PUBLICO_TICKET") if not ticket: print("No ticket found in .env") return # 1. Fetch today's OCs url_list = f"https://api.mercadopublico.cl/servicios/v1/publico/ordenesdecompra.json?ticket={ticket}" print(f"Fetching OCs: {url_list}") async with httpx.AsyncClient(timeout=30) as client: try: resp = await client.get(url_list) data = resp.json() items = data.get("Listado", []) print(f"Found {len(items)} OCs today.") if items: print(f"List response sample (item 0):") print(json.dumps(items[0], indent=2)) with open("oc_list_sample.json", "w") as f: json.dump(items[0], f, indent=2) code = items[0].get("Codigo") resp_detail = await client.get(url_detail) detail_data = resp_detail.json() print("OC Detail sample:") # print(json.dumps(detail_data, indent=2)) with open("oc_sample_detail.json", "w") as f: json.dump(detail_data, f, indent=2) print("Saved to oc_sample_detail.json") except Exception as e: print(f"Error: {e}") if __name__ == "__main__": asyncio.run(test_oc_api())