#!/usr/bin/python3

import json
import requests
import sys

if len(sys.argv) < 3 or len(sys.argv) > 4:
    print("Test if an api returns a given value")
    print("Usage: %s URL KEY [VALUE=True]" % sys.argv[0])
    sys.exit(1)

url = sys.argv[1]
key = sys.argv[2]
try:
    value = sys.argv[3]
except IndexError:
    value = str(True)

response = requests.get(url)
json_response = json.loads(response.content)

# assert key is in response
if key not in json_response.keys():
    print("Key %s not in response" % key)
    sys.exit(1)

# assert value for key is value
if str(json_response[key]) != value:
    print("Value for %s is %s instead of %s" % (key, str(json_response[key]), value))
    sys.exit(2)
