110 lines
2.9 KiB
Python
110 lines
2.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Simple CLI to call the running Space API (no external deps).
|
|
Usage:
|
|
python api_client.py status [--url URL]
|
|
python api_client.py start [--url URL]
|
|
python api_client.py stop [--url URL]
|
|
python api_client.py screenshot [--url URL] [--out FILE]
|
|
|
|
Defaults URL: http://localhost:5000
|
|
"""
|
|
import sys
|
|
import argparse
|
|
import json
|
|
from urllib import request, error, parse
|
|
|
|
DEFAULT_URL = 'http://localhost:5000'
|
|
|
|
|
|
def get(url):
|
|
try:
|
|
with request.urlopen(url, timeout=5) as resp:
|
|
return resp.read(), resp.getcode(), resp.info().get_content_type()
|
|
except error.HTTPError as e:
|
|
return e.read(), e.code, None
|
|
except Exception as e:
|
|
print('Request error:', e)
|
|
return None, None, None
|
|
|
|
|
|
def post(url):
|
|
req = request.Request(url, method='POST')
|
|
try:
|
|
with request.urlopen(req, timeout=5) as resp:
|
|
return resp.read(), resp.getcode()
|
|
except error.HTTPError as e:
|
|
return e.read(), e.code
|
|
except Exception as e:
|
|
print('Request error:', e)
|
|
return None, None
|
|
|
|
|
|
def cmd_status(base_url):
|
|
data, code, ctype = get(f"{base_url.rstrip('/')}/api/status")
|
|
if data is None:
|
|
print('No response')
|
|
return 2
|
|
try:
|
|
j = json.loads(data.decode())
|
|
print(json.dumps(j, indent=2))
|
|
except Exception:
|
|
print(data.decode(errors='ignore'))
|
|
return 0
|
|
|
|
|
|
def cmd_start(base_url):
|
|
data, code = post(f"{base_url.rstrip('/')}/api/start")
|
|
print('HTTP', code)
|
|
if data:
|
|
print(data.decode(errors='ignore'))
|
|
return 0
|
|
|
|
|
|
def cmd_stop(base_url):
|
|
data, code = post(f"{base_url.rstrip('/')}/api/stop")
|
|
print('HTTP', code)
|
|
if data:
|
|
print(data.decode(errors='ignore'))
|
|
return 0
|
|
|
|
|
|
def cmd_screenshot(base_url, out):
|
|
data, code, ctype = get(f"{base_url.rstrip('/')}/api/screenshot")
|
|
if data is None or code != 200:
|
|
print('Failed to get screenshot, HTTP', code)
|
|
if data:
|
|
print(data.decode(errors='ignore'))
|
|
return 2
|
|
with open(out, 'wb') as f:
|
|
f.write(data)
|
|
print('Saved screenshot to', out)
|
|
return 0
|
|
|
|
|
|
def main(argv):
|
|
p = argparse.ArgumentParser()
|
|
p.add_argument('--url', '-u', default=DEFAULT_URL, help='Base URL of the API')
|
|
sub = p.add_subparsers(dest='cmd')
|
|
sub.add_parser('status')
|
|
sub.add_parser('start')
|
|
sub.add_parser('stop')
|
|
ss = sub.add_parser('screenshot')
|
|
ss.add_argument('--out', '-o', default='latest.jpg')
|
|
args = p.parse_args(argv)
|
|
if not args.cmd:
|
|
p.print_help()
|
|
return 1
|
|
if args.cmd == 'status':
|
|
return cmd_status(args.url)
|
|
if args.cmd == 'start':
|
|
return cmd_start(args.url)
|
|
if args.cmd == 'stop':
|
|
return cmd_stop(args.url)
|
|
if args.cmd == 'screenshot':
|
|
return cmd_screenshot(args.url, args.out)
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
raise SystemExit(main(sys.argv[1:]))
|