"""First call: search the collection for "pattanam" and print the two best matches.

Standard library only, Python 3.9 or later. Set LTS_BASE_URL to call another host.
"""
import json
import os
import urllib.request

BASE = os.environ.get("LTS_BASE_URL", "https://layersofthesouth.org")

request = urllib.request.Request(
    f"{BASE}/api/v1/search?q=pattanam&limit=2",
    headers={"Accept": "application/json"},
)
with urllib.request.urlopen(request, timeout=15) as response:
    result = json.load(response)

print(f"{result['data']['totalMatches']} matches in snapshot {result['snapshotId']}")
for study in result["data"]["results"]:
    print(f"{study['id']}\t{study['title']}\t{study['url']}")
