#!/bin/bash
# Test all WHM API functions, categorize responses
python3 -c "import json; d=json.load(open('/usr/local/cpanel/whostmgr/docroot/openapi/whm.openapi.json')); print('\n'.join(d.keys()))" > /tmp/whm_funcs.txt
wc -l /tmp/whm_funcs.txt
> /tmp/whm_results.txt
while read fn; do
  r=$(curl -sk -u 'john:Ip7*sunfO0#P5xxg' "https://127.0.0.1:2087/json-api/$fn?api.version=1" | head -c 200)
  # categorize
  if echo "$r" | grep -q '"result":1'; then cat="OK";
  elif echo "$r" | grep -q 'Permission denied'; then cat="PERM";
  elif echo "$r" | grep -q 'Unknown app'; then cat="UNKNOWN";
  elif echo "$r" | grep -q 'requires a JSON'; then cat="NEEDSJSON";
  elif echo "$r" | grep -qi 'access to a domain\|requires\|missing\|invalid\|XID'; then cat="NEEDSPARAM";
  else cat="OTHER"; fi
  echo "$cat|$fn|$r" >> /tmp/whm_results.txt
done < /tmp/whm_funcs.txt
echo "DONE"
