File size: 1,424 Bytes
0d71eae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import httpx
import asyncio
import json

async def test_full_api():
    ticket = "99B4CA8C-C1DF-4E3F-B5CF-C1672D432A91"
    
    # 1. Fetch active tenders
    url_active = f"https://api.mercadopublico.cl/servicios/v1/publico/licitaciones.json?estado=activas&ticket={ticket}"
    print(f"Fetching active tenders: {url_active}")
    
    async with httpx.AsyncClient(timeout=30) as client:
        try:
            resp = await client.get(url_active)
            data = resp.json()
            items = data.get("Listado", [])
            print(f"Found {len(items)} active items.")
            
            if items:
                code = items[0].get("CodigoExterno")
                print(f"Fetching details for code: {code}")
                
                url_detail = f"https://api.mercadopublico.cl/servicios/v1/publico/licitaciones.json?codigo={code}&ticket={ticket}"
                resp_detail = await client.get(url_detail)
                detail_data = resp_detail.json()
                
                print("Detail sample:")
                print(json.dumps(detail_data, indent=2))
                
                # Save to file for reference
                with open("api_sample_detail.json", "w") as f:
                    json.dump(detail_data, f, indent=2)
                    
        except Exception as e:
            print(f"Error: {e}")

if __name__ == "__main__":
    asyncio.run(test_full_api())