Add more commands to rest client, fix bug in config handling
This commit is contained in:
parent
d2c2811249
commit
5ba189ffb4
@ -26,16 +26,28 @@ logger = logging.getLogger("ft_rest_client")
|
|||||||
COMMANDS_NO_ARGS = ["start",
|
COMMANDS_NO_ARGS = ["start",
|
||||||
"stop",
|
"stop",
|
||||||
"stopbuy",
|
"stopbuy",
|
||||||
"reload_conf"
|
"reload_conf",
|
||||||
]
|
|
||||||
COMMANDS_ARGS = ["daily",
|
|
||||||
]
|
]
|
||||||
|
INFO_COMMANDS = {"version": [],
|
||||||
|
"count": [],
|
||||||
|
"daily": ["timescale"],
|
||||||
|
"profit": [],
|
||||||
|
"status": [],
|
||||||
|
"balance": []
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def add_arguments():
|
def add_arguments():
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("command",
|
parser.add_argument("command",
|
||||||
help="Positional argument defining the command to execute.")
|
help="Positional argument defining the command to execute.")
|
||||||
|
|
||||||
|
parser.add_argument("command_arguments",
|
||||||
|
help="Positional arguments for the parameters for [command]",
|
||||||
|
nargs="*",
|
||||||
|
default=[]
|
||||||
|
)
|
||||||
|
|
||||||
parser.add_argument('-c', '--config',
|
parser.add_argument('-c', '--config',
|
||||||
help='Specify configuration file (default: %(default)s). ',
|
help='Specify configuration file (default: %(default)s). ',
|
||||||
dest='config',
|
dest='config',
|
||||||
@ -59,6 +71,7 @@ def load_config(configfile):
|
|||||||
with file.open("r") as f:
|
with file.open("r") as f:
|
||||||
config = json.load(f)
|
config = json.load(f)
|
||||||
return config
|
return config
|
||||||
|
return {}
|
||||||
|
|
||||||
|
|
||||||
def call_authorized(url):
|
def call_authorized(url):
|
||||||
@ -74,6 +87,22 @@ def call_command_noargs(server_url, command):
|
|||||||
logger.info(r)
|
logger.info(r)
|
||||||
|
|
||||||
|
|
||||||
|
def call_info(server_url, command, command_args):
|
||||||
|
logger.info(f"Running command `{command}` with parameters `{command_args}` at {server_url}")
|
||||||
|
call = f"{server_url}/{command}?"
|
||||||
|
args = INFO_COMMANDS[command]
|
||||||
|
if len(args) < len(command_args):
|
||||||
|
logger.error(f"Command {command} does only support {len(args)} arguments.")
|
||||||
|
return
|
||||||
|
for idx, arg in enumerate(command_args):
|
||||||
|
|
||||||
|
call += f"{args[idx]}={arg}"
|
||||||
|
logger.debug(call)
|
||||||
|
r = call_authorized(call)
|
||||||
|
|
||||||
|
logger.info(r)
|
||||||
|
|
||||||
|
|
||||||
def main(args):
|
def main(args):
|
||||||
|
|
||||||
config = load_config(args["config"])
|
config = load_config(args["config"])
|
||||||
@ -85,13 +114,8 @@ def main(args):
|
|||||||
if args["command"] in COMMANDS_NO_ARGS:
|
if args["command"] in COMMANDS_NO_ARGS:
|
||||||
call_command_noargs(server_url, args["command"])
|
call_command_noargs(server_url, args["command"])
|
||||||
|
|
||||||
if args["command"] == "daily":
|
if args["command"] in INFO_COMMANDS:
|
||||||
if str.isnumeric(argv[2]):
|
call_info(server_url, args["command"], args["command_arguments"])
|
||||||
get_url = server_url + '/daily?timescale=' + argv[2]
|
|
||||||
d = get(get_url).json()
|
|
||||||
print(d)
|
|
||||||
else:
|
|
||||||
print("\nThe second argument to daily must be an integer, 1,2,3 etc")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user