Load rest-client config from file

This commit is contained in:
Matthias 2019-04-09 06:40:15 +02:00
parent d8549fe09a
commit 01c93a2ee3

View File

@ -8,11 +8,10 @@ so it can be used as a standalone script.
""" """
import argparse import argparse
import json
import logging import logging
import time
from sys import argv from sys import argv
from pathlib import Path
import click
from requests import get from requests import get
from requests.exceptions import ConnectionError from requests.exceptions import ConnectionError
@ -23,21 +22,27 @@ logging.basicConfig(
) )
logger = logging.getLogger("ft_rest_client") logger = logging.getLogger("ft_rest_client")
# TODO - use IP and Port from config.json not hardcode
COMMANDS_NO_ARGS = ["start", COMMANDS_NO_ARGS = ["start",
"stop", "stop",
"stopbuy",
"reload_conf"
] ]
COMMANDS_ARGS = ["daily", COMMANDS_ARGS = ["daily",
] ]
SERVER_URL = "http://localhost:5002"
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('-c', '--config',
help='Specify configuration file (default: %(default)s). ',
dest='config',
type=str,
metavar='PATH',
default='config.json'
)
args = parser.parse_args() args = parser.parse_args()
# if len(argv) == 1: # if len(argv) == 1:
# print('\nThis script accepts the following arguments') # print('\nThis script accepts the following arguments')
@ -48,6 +53,14 @@ def add_arguments():
return vars(args) return vars(args)
def load_config(configfile):
file = Path(configfile)
if file.is_file():
with file.open("r") as f:
config = json.load(f)
return config
def call_authorized(url): def call_authorized(url):
try: try:
return get(url).json() return get(url).json()
@ -55,21 +68,26 @@ def call_authorized(url):
logger.warning("Connection error") logger.warning("Connection error")
def call_command_noargs(command): def call_command_noargs(server_url, command):
logger.info(f"Running command `{command}` at {SERVER_URL}") logger.info(f"Running command `{command}` at {server_url}")
r = call_authorized(f"{SERVER_URL}/{command}") r = call_authorized(f"{server_url}/{command}")
logger.info(r) logger.info(r)
def main(args): def main(args):
config = load_config(args["config"])
url = config.get("api_server", {}).get("server_url", "127.0.0.1")
port = config.get("api_server", {}).get("listen_port", "8080")
server_url = f"http://{url}:{port}"
# Call commands without arguments # Call commands without arguments
if args["command"] in COMMANDS_NO_ARGS: if args["command"] in COMMANDS_NO_ARGS:
call_command_noargs(args["command"]) call_command_noargs(server_url, args["command"])
if args["command"] == "daily": if args["command"] == "daily":
if str.isnumeric(argv[2]): if str.isnumeric(argv[2]):
get_url = SERVER_URL + '/daily?timescale=' + argv[2] get_url = server_url + '/daily?timescale=' + argv[2]
d = get(get_url).json() d = get(get_url).json()
print(d) print(d)
else: else: