diff --git a/scripts/signup.py b/scripts/signup.py index 028eb2367..755304e5c 100644 --- a/scripts/signup.py +++ b/scripts/signup.py @@ -1,10 +1,14 @@ import argparse -from constants import URL, DB_NAME, MOUNT import requests +import sys +from urllib.parse import urljoin + from auth_tools import get_headers, create_credentials_file +from constants import URL + -def signup(user, password, email): - url = URL + f"/user/signup" +def signup(user, password, email, url): + url = urljoin(url, "/user/signup") body = { "user": user, "passwd": password, @@ -21,7 +25,7 @@ def signup(user, password, email): def register_producer(description, url, logo, eth_address): headers = get_headers(None) - url = URL + "/producer" + url = urljoin(url, "/producer") body = {"description": description} if url is not None: body["url"] = url @@ -29,32 +33,46 @@ def register_producer(description, url, logo, eth_address): body["logo"] = logo if eth_address is not None: body["eth_address"] = eth_address + response = requests.post(url, json=body, headers=headers) if response.status_code != 200: print(f"Error: {response.status_code} {response.text}") - else: - print(response.text) + return False + + print(response.text) + return True -def signup_parser(args): - response = signup(args.user, args.password, args.email) +def signup_parser(args) -> bool: + response = signup(args.user, args.password, args.email, args.url) if response.status_code == 200: create_credentials_file("secret", args.password) create_credentials_file("user", args.user) + return response.status_code in [200] -def register_producer_parser(args): - register_producer(args.description, args.url, args.logo, args.eth_address) +def register_producer_parser(args) -> bool: + return register_producer( + args.description, args.url, args.logo, args.eth_address, + ) -if __name__ == "__main__": + +def main(): parser = argparse.ArgumentParser() + parent_parser = argparse.ArgumentParser(add_help=False) + parent_parser.add_argument( + "--url", action="store", default=URL, help="url of a producer" + ) + subparsers = parser.add_subparsers(help="sub-command help") - parser_user = subparsers.add_parser("user", help="user signup") + + parser_user = subparsers.add_parser( + "user", help="user signup", parents=[parent_parser] + ) parser_user.set_defaults(func=signup_parser) - parser_producer = subparsers.add_parser("producer", help="producer signup") - parser_producer.set_defaults(func=register_producer_parser) + parser_producer = subparsers.add_parser("producer", help="producer signup", parents=[parent_parser]) parser_user.add_argument( "-u", "--user", action="store", required=True, help="user name" ) @@ -71,15 +89,22 @@ def register_producer_parser(args): default="Generic Producer", help="description of a producer", ) - parser_producer.add_argument( - "-u", "--url", action="store", default=None, help="url of a producer" - ) parser_producer.add_argument( "-l", "--logo", action="store", default=None, help="logo of a producer" ) parser_producer.add_argument( "-e", "--eth_address", action="store", default=None, help="eth address" ) + parser_producer.set_defaults(func=register_producer_parser) + args = parser.parse_args() + if hasattr(args, 'func'): + # valid submodules + sys.exit(0 if args.func(args) else 1) + + parser.print_help() + sys.exit(1) - args.func(args) + +if __name__ == "__main__": + main()