"""XQustoms Tariff API v2 - minimal Python client (requests only).
Contract: https://xqustoms.com/developer/openapi-v2.json. Missing values are None and listed in `missing`."""
import requests

class XQustomsError(Exception):
    def __init__(self, status, body):
        super().__init__(f"XQustoms API {status}"); self.status, self.body = status, body

class XQustoms:
    def __init__(self, api_key, base="https://xqustoms.com/api/v2/tariff", timeout=30):
        self.base, self.timeout = base, timeout
        self.s = requests.Session(); self.s.headers.update({"x-api-key": api_key, "content-type": "application/json"})

    def _call(self, path, body=None):
        r = self.s.request("GET" if body is None else "POST", f"{self.base}/{path}", json=body, timeout=self.timeout)
        try: j = r.json()
        except ValueError: j = None
        if not r.ok: raise XQustomsError(r.status_code, j)
        return j

    def analyze(self, **req):  # one quota unit; landed cost when customs_value is given
        return self._call("analyze", req)
    def search(self, **b): return self._call("search", b)
    def calculate(self, **b): return self._call("calculate", b)
    def validate(self, **b): return self._call("validate", b)
    def lookup(self, **b): return self._call("lookup", b)
    def batch(self, **b): return self._call("batch", b)
    def sources(self): return self._call("sources")  # free
