Skip to content

Commit

Permalink
Tiny signup CLI improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
x-mass committed Dec 26, 2023
1 parent 54129bd commit c37a4c0
Showing 1 changed file with 43 additions and 18 deletions.
61 changes: 43 additions & 18 deletions scripts/signup.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -21,40 +25,54 @@ 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
if logo is not None:
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"
)
Expand All @@ -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()

0 comments on commit c37a4c0

Please sign in to comment.