From 3aee8d2b2a347d2366088119e13c65c7b656f1ff Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 17 Nov 2019 14:40:59 +0100 Subject: [PATCH 1/3] 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") From 633996216a2640a19454aa8bbdd40c86021bbbac Mon Sep 17 00:00:00 2001 From: hroff-1902 Date: Wed, 20 Nov 2019 15:20:39 +0300 Subject: [PATCH 2/3] Improve commands help list --- scripts/rest_client.py | 69 +++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/scripts/rest_client.py b/scripts/rest_client.py index 096286013..03e4fc76b 100755 --- a/scripts/rest_client.py +++ b/scripts/rest_client.py @@ -65,100 +65,99 @@ class FtRestClient(): return self._call("POST", apipath, params=params, data=data) def start(self): - """ - Start the bot if it's in stopped state. + """Start the bot if it's in the stopped state. + :return: json object """ return self._post("start") def stop(self): - """ - Stop the bot. Use start to restart + """Stop the bot. Use `start` to restart. + :return: json object """ return self._post("stop") def stopbuy(self): - """ - Stop buying (but handle sells gracefully). - use reload_conf to reset + """Stop buying (but handle sells gracefully). Use `reload_conf` to reset. + :return: json object """ return self._post("stopbuy") def reload_conf(self): - """ - Reload configuration + """Reload configuration. + :return: json object """ return self._post("reload_conf") def balance(self): - """ - Get the account balance + """Get the account balance. + :return: json object """ return self._get("balance") def count(self): - """ - Returns the amount of open trades + """Return the amount of open trades. + :return: json object """ return self._get("count") def daily(self, days=None): - """ - Returns the amount of open trades + """Return the amount of open trades. + :return: json object """ return self._get("daily", params={"timescale": days} if days else None) def edge(self): - """ - Returns information about edge + """Return information about edge. + :return: json object """ return self._get("edge") def profit(self): - """ - Returns the profit summary + """Return the profit summary. + :return: json object """ return self._get("profit") def performance(self): - """ - Returns the performance of the different coins + """Return the performance of the different coins. + :return: json object """ return self._get("performance") def status(self): - """ - Get the status of open trades + """Get the status of open trades. + :return: json object """ return self._get("status") def version(self): - """ - Returns the version of the bot + """Return the version of the bot. + :return: json object containing the version """ return self._get("version") def whitelist(self): - """ - Show the current whitelist + """Show the current whitelist. + :return: json object """ return self._get("whitelist") def blacklist(self, *args): - """ - Show the current blacklist + """Show the current blacklist. + :param add: List of coins to add (example: "BNB/BTC") :return: json object """ @@ -168,8 +167,8 @@ class FtRestClient(): return self._post("blacklist", data={"blacklist": args}) def forcebuy(self, pair, price=None): - """ - Buy an asset + """Buy an asset. + :param pair: Pair to buy (ETH/BTC) :param price: Optional - price to buy :return: json object of the trade @@ -180,8 +179,8 @@ class FtRestClient(): return self._post("forcebuy", data=data) def forcesell(self, tradeid): - """ - Force-sell a trade + """Force-sell a trade. + :param tradeid: Id of the trade (can be received via status command) :return: json object """ @@ -236,10 +235,10 @@ def load_config(configfile): def print_commands(): # Print dynamic help for the different commands using the commands doc-strings client = FtRestClient(None) - print("Possible commands:") + print("Possible commands:\n") for x, y in inspect.getmembers(client): if not x.startswith('_'): - print(f"{x} {getattr(client, x).__doc__}") + print(f"{x}""\n "f"{getattr(client, x).__doc__.splitlines()[0]}""\n") def main(args): From 8b639b5026a2b7cd745de08cf764f7ddefcea9fa Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 20 Nov 2019 19:54:00 +0100 Subject: [PATCH 3/3] Remove only :return: --- scripts/rest_client.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/rest_client.py b/scripts/rest_client.py index 03e4fc76b..9e52de2bb 100755 --- a/scripts/rest_client.py +++ b/scripts/rest_client.py @@ -10,6 +10,7 @@ so it can be used as a standalone script. import argparse import inspect import json +import re import logging import sys from pathlib import Path @@ -238,7 +239,8 @@ def print_commands(): print("Possible commands:\n") for x, y in inspect.getmembers(client): if not x.startswith('_'): - print(f"{x}""\n "f"{getattr(client, x).__doc__.splitlines()[0]}""\n") + doc = re.sub(':return:.*', '', getattr(client, x).__doc__, flags=re.MULTILINE).rstrip() + print(f"{x}\n\t{doc}\n") def main(args):