Add 404 when strategy is not found

This commit is contained in:
Matthias 2020-09-17 07:53:22 +02:00
parent becccca3d1
commit b38f68b3b0
4 changed files with 36 additions and 1 deletions

View File

@ -133,6 +133,7 @@ python3 scripts/rest_client.py --config rest_config.json <command> [optional par
| `pair_history` | Returns an analyzed dataframe for a given timerange, analyzed by a given strategy. **Alpha** | `pair_history` | Returns an analyzed dataframe for a given timerange, analyzed by a given strategy. **Alpha**
| `plot_config` | Get plot config from the strategy (or nothing if not configured). **Alpha** | `plot_config` | Get plot config from the strategy (or nothing if not configured). **Alpha**
| `strategies` | List strategies in strategy directory. **Alpha** | `strategies` | List strategies in strategy directory. **Alpha**
| `strategy <strategy>` | Get specific Strategy content. **Alpha**
| `available_pairs` | List available backtest data. **Alpha** | `available_pairs` | List available backtest data. **Alpha**
| `version` | Show version | `version` | Show version
@ -239,6 +240,11 @@ stopbuy
strategies strategies
Lists available strategies Lists available strategies
strategy
Get strategy details
:param strategy: Strategy class name
trades trades
Return trades history. Return trades history.

View File

@ -1,3 +1,4 @@
from freqtrade.exceptions import OperationalException
import logging import logging
import threading import threading
from copy import deepcopy from copy import deepcopy
@ -599,7 +600,11 @@ class ApiServer(RPC):
""" """
config = deepcopy(self._config) config = deepcopy(self._config)
from freqtrade.resolvers.strategy_resolver import StrategyResolver from freqtrade.resolvers.strategy_resolver import StrategyResolver
strategy_obj = StrategyResolver._load_strategy(strategy, config, None) try:
strategy_obj = StrategyResolver._load_strategy(strategy, config,
extra_dir=config.get('strategy_path'))
except OperationalException:
return self.rest_error("Strategy not found.", 404)
return self.rest_dump({ return self.rest_dump({
'strategy': strategy_obj.get_strategy_name(), 'strategy': strategy_obj.get_strategy_name(),

View File

@ -231,6 +231,14 @@ class FtRestClient():
""" """
return self._get("strategies") return self._get("strategies")
def strategy(self, strategy):
"""Get strategy details
:param strategy: Strategy class name
:return: json object
"""
return self._get(f"strategy/{strategy}")
def plot_config(self): def plot_config(self):
"""Return plot configuration if the strategy defines one. """Return plot configuration if the strategy defines one.

View File

@ -3,6 +3,7 @@ Unit test file for rpc/api_server.py
""" """
from datetime import datetime from datetime import datetime
from pathlib import Path
from unittest.mock import ANY, MagicMock, PropertyMock from unittest.mock import ANY, MagicMock, PropertyMock
import pytest import pytest
@ -945,6 +946,21 @@ def test_api_strategies(botclient):
assert rc.json == {'strategies': ['DefaultStrategy', 'TestStrategyLegacy']} assert rc.json == {'strategies': ['DefaultStrategy', 'TestStrategyLegacy']}
def test_api_strategy(botclient):
ftbot, client = botclient
rc = client_get(client, f"{BASE_URI}/strategy/DefaultStrategy")
assert_response(rc)
assert rc.json['strategy'] == 'DefaultStrategy'
data = (Path(__file__).parents[1] / "strategy/strats/default_strategy.py").read_text()
assert rc.json['code'] == data
rc = client_get(client, f"{BASE_URI}/strategy/NoStrat")
assert_response(rc, 404)
def test_list_available_pairs(botclient): def test_list_available_pairs(botclient):
ftbot, client = botclient ftbot, client = botclient