File size: 1,550 Bytes
5f2460b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50666c5
 
 
 
5f2460b
50666c5
5f2460b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
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())