Merge pull request #2540 from freqtrade/rpc/fixes

Improve rest api client / status response
This commit is contained in:
hroff-1902 2019-11-20 22:18:51 +03:00 committed by GitHub
commit 5ce665f279
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 46 deletions

View File

@ -340,8 +340,11 @@ class ApiServer(RPC):
Returns the current status of the trades in json format Returns the current status of the trades in json format
""" """
results = self._rpc_trade_status() try:
return self.rest_dump(results) results = self._rpc_trade_status()
return self.rest_dump(results)
except RPCException:
return self.rest_dump([])
@require_login @require_login
@rpc_catch_errors @rpc_catch_errors

View File

@ -8,12 +8,15 @@ so it can be used as a standalone script.
""" """
import argparse import argparse
import json
import logging
import inspect import inspect
from urllib.parse import urlencode, urlparse, urlunparse import json
import re
import logging
import sys
from pathlib import Path from pathlib import Path
from urllib.parse import urlencode, urlparse, urlunparse
import rapidjson
import requests import requests
from requests.exceptions import ConnectionError from requests.exceptions import ConnectionError
@ -63,86 +66,85 @@ class FtRestClient():
return self._call("POST", apipath, params=params, data=data) return self._call("POST", apipath, params=params, data=data)
def start(self): def start(self):
""" """Start the bot if it's in the stopped state.
Start the bot if it's in stopped state.
:return: json object :return: json object
""" """
return self._post("start") return self._post("start")
def stop(self): def stop(self):
""" """Stop the bot. Use `start` to restart.
Stop the bot. Use start to restart
:return: json object :return: json object
""" """
return self._post("stop") return self._post("stop")
def stopbuy(self): 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: json object
""" """
return self._post("stopbuy") return self._post("stopbuy")
def reload_conf(self): def reload_conf(self):
""" """Reload configuration.
Reload configuration
:return: json object :return: json object
""" """
return self._post("reload_conf") return self._post("reload_conf")
def balance(self): def balance(self):
""" """Get the account balance.
Get the account balance
:return: json object :return: json object
""" """
return self._get("balance") return self._get("balance")
def count(self): def count(self):
""" """Return the amount of open trades.
Returns the amount of open trades
:return: json object :return: json object
""" """
return self._get("count") return self._get("count")
def daily(self, days=None): def daily(self, days=None):
""" """Return the amount of open trades.
Returns the amount of open trades
:return: json object :return: json object
""" """
return self._get("daily", params={"timescale": days} if days else None) return self._get("daily", params={"timescale": days} if days else None)
def edge(self): def edge(self):
""" """Return information about edge.
Returns information about edge
:return: json object :return: json object
""" """
return self._get("edge") return self._get("edge")
def profit(self): def profit(self):
""" """Return the profit summary.
Returns the profit summary
:return: json object :return: json object
""" """
return self._get("profit") return self._get("profit")
def performance(self): def performance(self):
""" """Return the performance of the different coins.
Returns the performance of the different coins
:return: json object :return: json object
""" """
return self._get("performance") return self._get("performance")
def status(self): def status(self):
""" """Get the status of open trades.
Get the status of open trades
:return: json object :return: json object
""" """
return self._get("status") return self._get("status")
def version(self): def version(self):
""" """Return the version of the bot.
Returns the version of the bot
:return: json object containing the version :return: json object containing the version
""" """
return self._get("version") return self._get("version")
@ -155,15 +157,15 @@ class FtRestClient():
return self._get("show_config") return self._get("show_config")
def whitelist(self): def whitelist(self):
""" """Show the current whitelist.
Show the current whitelist
:return: json object :return: json object
""" """
return self._get("whitelist") return self._get("whitelist")
def blacklist(self, *args): def blacklist(self, *args):
""" """Show the current blacklist.
Show the current blacklist
:param add: List of coins to add (example: "BNB/BTC") :param add: List of coins to add (example: "BNB/BTC")
:return: json object :return: json object
""" """
@ -173,8 +175,8 @@ class FtRestClient():
return self._post("blacklist", data={"blacklist": args}) return self._post("blacklist", data={"blacklist": args})
def forcebuy(self, pair, price=None): def forcebuy(self, pair, price=None):
""" """Buy an asset.
Buy an asset
:param pair: Pair to buy (ETH/BTC) :param pair: Pair to buy (ETH/BTC)
:param price: Optional - price to buy :param price: Optional - price to buy
:return: json object of the trade :return: json object of the trade
@ -185,8 +187,8 @@ class FtRestClient():
return self._post("forcebuy", data=data) return self._post("forcebuy", data=data)
def forcesell(self, tradeid): def forcesell(self, tradeid):
""" """Force-sell a trade.
Force-sell a trade
:param tradeid: Id of the trade (can be received via status command) :param tradeid: Id of the trade (can be received via status command)
:return: json object :return: json object
""" """
@ -197,7 +199,9 @@ class FtRestClient():
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.",
nargs="?"
)
parser.add_argument('--show', parser.add_argument('--show',
help='Show possible methods with this client', help='Show possible methods with this client',
@ -228,24 +232,29 @@ def load_config(configfile):
file = Path(configfile) file = Path(configfile)
if file.is_file(): if file.is_file():
with file.open("r") as f: 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 config
return {} else:
logger.warning(f"Could not load config file {file}.")
sys.exit(1)
def print_commands(): def print_commands():
# Print dynamic help for the different commands using the commands doc-strings # Print dynamic help for the different commands using the commands doc-strings
client = FtRestClient(None) client = FtRestClient(None)
print("Possible commands:") print("Possible commands:\n")
for x, y in inspect.getmembers(client): for x, y in inspect.getmembers(client):
if not x.startswith('_'): if not x.startswith('_'):
print(f"{x} {getattr(client, x).__doc__}") doc = re.sub(':return:.*', '', getattr(client, x).__doc__, flags=re.MULTILINE).rstrip()
print(f"{x}\n\t{doc}\n")
def main(args): def main(args):
if args.get("help"): if args.get("show"):
print_commands() print_commands()
sys.exit()
config = load_config(args["config"]) config = load_config(args["config"])
url = config.get("api_server", {}).get("server_url", "127.0.0.1") url = config.get("api_server", {}).get("server_url", "127.0.0.1")

View File

@ -429,8 +429,8 @@ def test_api_status(botclient, mocker, ticker, fee, markets):
) )
rc = client_get(client, f"{BASE_URI}/status") rc = client_get(client, f"{BASE_URI}/status")
assert_response(rc, 502) assert_response(rc, 200)
assert rc.json == {'error': 'Error querying _status: no active trade'} assert rc.json == []
ftbot.create_trades() ftbot.create_trades()
rc = client_get(client, f"{BASE_URI}/status") rc = client_get(client, f"{BASE_URI}/status")