"""Synthetic connectivity example. No payment or external action is executed.""" import json import os import urllib.request BASE = 'https://agent-trust-firewall.andrea-vignoni.workers.dev' def check(amount): key = os.environ.get('AGENT_GUARD_KEY', '') path = '/trial/api/trust_check_spend' if key else '/v1/trust/spend-guard' headers = {'Content-Type': 'application/json', 'User-Agent': 'AgentGuard-Python-Example/0.2.0', 'X-Agent-Source': 'docs-example', 'X-Agent-Name': 'python-example'} if key: headers['Authorization'] = 'Bearer ' + key req = urllib.request.Request(BASE + path, data=json.dumps( {'amount': amount, 'remaining_budget': 10}).encode(), headers=headers) with urllib.request.urlopen(req, timeout=15) as response: result = json.load(response) if result.get('ok') is not True or result.get('verdict') not in ('ALLOW', 'BLOCK'): raise RuntimeError('Invalid response: stop execution') return result if __name__ == '__main__': for amount, expected in [(2, 'ALLOW'), (11, 'BLOCK')]: result = check(amount) if result['verdict'] != expected: raise RuntimeError('Unexpected policy verdict: stop execution') print(json.dumps({'amount': amount, 'verdict': result['verdict']}))