From 3aee8d2b2a347d2366088119e13c65c7b656f1ff Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 17 Nov 2019 14:40:59 +0100 Subject: [PATCH] Improve rest api client / status response --- freqtrade/rpc/api_server.py | 7 +++++-- scripts/rest_client.py | 20 ++++++++++++++------ tests/rpc/test_rpc_apiserver.py | 4 ++-- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/freqtrade/rpc/api_server.py b/freqtrade/rpc/api_server.py index 3b59c9592..851806ec2 100644 --- a/freqtrade/rpc/api_server.py +++ b/freqtrade/rpc/api_server.py @@ -330,8 +330,11 @@ class ApiServer(RPC): Returns the current status of the trades in json format """ - results = self._rpc_trade_status() - return self.rest_dump(results) + try: + results = self._rpc_trade_status() + return self.rest_dump(results) + except RPCException: + return self.rest_dump([]) @require_login @rpc_catch_errors diff --git a/scripts/rest_client.py b/scripts/rest_client.py index a46b3ebfb..096286013 100755 --- a/scripts/rest_client.py +++ b/scripts/rest_client.py @@ -8,12 +8,14 @@ so it can be used as a standalone script. """ import argparse +import inspect import json import logging -import inspect -from urllib.parse import urlencode, urlparse, urlunparse +import sys from pathlib import Path +from urllib.parse import urlencode, urlparse, urlunparse +import rapidjson import requests from requests.exceptions import ConnectionError @@ -190,7 +192,9 @@ class FtRestClient(): def add_arguments(): parser = argparse.ArgumentParser() parser.add_argument("command", - help="Positional argument defining the command to execute.") + help="Positional argument defining the command to execute.", + nargs="?" + ) parser.add_argument('--show', help='Show possible methods with this client', @@ -221,9 +225,12 @@ def load_config(configfile): file = Path(configfile) if file.is_file(): with file.open("r") as f: - config = json.load(f) + config = rapidjson.load(f, parse_mode=rapidjson.PM_COMMENTS | + rapidjson.PM_TRAILING_COMMAS) return config - return {} + else: + logger.warning(f"Could not load config file {file}.") + sys.exit(1) def print_commands(): @@ -237,8 +244,9 @@ def print_commands(): def main(args): - if args.get("help"): + if args.get("show"): print_commands() + sys.exit() config = load_config(args["config"]) url = config.get("api_server", {}).get("server_url", "127.0.0.1") diff --git a/tests/rpc/test_rpc_apiserver.py b/tests/rpc/test_rpc_apiserver.py index 6e65cf934..cbca7e3d5 100644 --- a/tests/rpc/test_rpc_apiserver.py +++ b/tests/rpc/test_rpc_apiserver.py @@ -417,8 +417,8 @@ def test_api_status(botclient, mocker, ticker, fee, markets): ) rc = client_get(client, f"{BASE_URI}/status") - assert_response(rc, 502) - assert rc.json == {'error': 'Error querying _status: no active trade'} + assert_response(rc, 200) + assert rc.json == [] ftbot.create_trades() rc = client_get(client, f"{BASE_URI}/status")