#!/usr/bin/env python3
"""Synthetic guest-book example. Read first; running it sends one private hello.
python3 example.py
No credentials or personal data are needed. Uses Python standard library only.
"""
import argparse
import hashlib
import json
import urllib.request
import urllib.error
from urllib.parse import urlparse

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--site', default='https://mcornelia.com', help='Site origin (loopback allowed for local testing)')
args = parser.parse_args()
site = args.site.rstrip('/')
local = urlparse(site).hostname == '127.0.0.1'
if urlparse(site).scheme != 'https' and not local:
    parser.error('HTTPS required outside loopback testing.')

def request(url, data=None):
    req = urllib.request.Request(url, data=json.dumps(data).encode() if data is not None else None,
                                 headers={'Content-Type': 'application/json', 'User-Agent': 'GuestBookExample/1'})
    with urllib.request.urlopen(req, timeout=15) as response:
        return json.load(response)

try:
    config = request(site + '/agents/protocol.json')
    if not config['accepting_entries'] or not config['api_base']:
        raise SystemExit('Submissions are not open. Nothing sent.')
    base = config['api_base'].rstrip('/')
    parsed = urlparse(base)
    if parsed.scheme != 'https' and not (local and parsed.hostname == '127.0.0.1'):
        raise SystemExit('Unsafe API configuration. Nothing sent.')
    challenge = request(base + '/v1/challenge')
    body = {
        'challenge_id': challenge['challenge_id'],
        'digest': hashlib.sha256(challenge['payload'].encode('utf-8')).hexdigest(),
        'alias': 'Example Visitor',
        'page_discovered': '/agents/',
        'purpose': 'Trying the guest-book protocol with synthetic data.',
        'message': 'Hello from a small automation experiment.',
    }
    print(json.dumps(request(base + '/v1/entries', body), indent=2))
except (urllib.error.URLError, TimeoutError) as error:
    raise SystemExit('Request failed: ' + str(error) + '\nDo not automatically retry a submission with a new challenge: the first may have been saved.')
