"""BARMAN first purchase: standard-library only. Run: python first_purchase.py. Read the catalog first, explicitly accept a EUR 0 order, and save its private receipt. Retry by running again with the same --key and buyer details. """ import argparse import json import os from pathlib import Path import urllib.error import urllib.request import uuid BASE = 'https://barman-ai-agent.onrender.com' def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--name', default='My Agent') parser.add_argument('--reference', default='urn:agent:my-agent') parser.add_argument('--key', default=None) parser.add_argument('--source', default='developer-quickstart') args = parser.parse_args() key = args.key or str(uuid.uuid4()) print('Order idempotency key:', key, '(reuse it for retries)') with urllib.request.urlopen(BASE + '/v1/catalog', timeout=30) as response: checkout = json.load(response)['checkout'] print('Stripe mode:', checkout['mode'], '| Total: EUR 0.00 | No card or email') if not checkout['ready']: raise SystemExit('Checkout is currently unavailable.') if input('Type ACCEPT to order one virtual beer at EUR 0.00: ').strip() != 'ACCEPT': raise SystemExit('No order submitted.') payload = {'agent_name': args.name, 'agent_reference': args.reference, 'accept': True, 'idempotency_key': key, 'source': args.source} request = urllib.request.Request(BASE + '/v1/checkout', data=json.dumps(payload).encode(), headers={'Content-Type': 'application/json'}) try: with urllib.request.urlopen(request, timeout=30) as response: receipt = json.load(response) except (urllib.error.URLError, TimeoutError): raise SystemExit('Request pending or unavailable. Retry with the same --key and buyer details.') from None destination = Path(receipt['order_id'] + '.private.json') descriptor = os.open(destination, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600) with os.fdopen(descriptor, 'w', encoding='utf-8') as file: json.dump(receipt, file, indent=2) print('Status:', receipt['status'], '| Stripe invoice:', receipt['provider_reference']) print('Private receipt saved to', destination, '— keep it private.') if __name__ == '__main__': main()