score_server/scripts/test.py

45 lines
1.1 KiB
Python

#!/usr/bin/env python3
import jwt
from sys import argv
from datetime import datetime
from time import mktime
import requests
def gen_jwt(aud, scope, key):
return jwt.encode({"aud": aud, "scope": scope, "issued": mktime(datetime.now().timetuple())}, pem_string, algorithm="EdDSA")
def usage():
print(f"Usage {argv[0]} {{pem_file}} {{command}}")
if __name__ == "__main__":
if len(argv) < 3:
usage()
exit(1)
pem_string = None
with open(argv[1]) as f:
pem_string = "".join(f.readlines())
if pem_string is None:
print(f"Couldn't read pem file {argv[1]}")
exit(2)
token = gen_jwt("score_server", ["tournaments:list"], pem_string)
headers = {'Authorization': f"Bearer {token}"}
response = None
match argv[2]:
case "list-tournaments":
response = requests.get("http://localhost:8080/tournaments", headers=headers)
case _:
usage()
exit(3)
if response is not None:
print(response)
print(response.text)
else:
print("unhandled_error")