"""Chamada única, Python 3, servidor. Não registra respostas financeiras."""
import json
import os
import urllib.request
from urllib.parse import urlsplit

base = os.environ['RURALZAP_BASE_URL']
key = os.environ['RURALZAP_API_KEY']
origin = urlsplit(base)
if origin.scheme != 'https' or not origin.netloc or origin.username or origin.password or origin.path not in ('', '/') or origin.query or origin.fragment:
    raise ValueError('Informe uma origem HTTPS sem caminho ou credenciais.')

class NoRedirect(urllib.request.HTTPRedirectHandler):
    def redirect_request(self, req, fp, code, msg, headers, newurl):
        return None

request = urllib.request.Request(base.rstrip('/') + '/api/v1/integration/reports/summary?startDate=2026-01-01&endDate=2026-01-31', headers={'Authorization': 'Bearer ' + key})
with urllib.request.build_opener(NoRedirect).open(request, timeout=30) as response:
    data = json.load(response)
# Valide data com /openapi.json. Preserve dinheiro como string ou Decimal.
