From 9e75c768c005beb2db351958d785b05f5d11c0d8 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 24 May 2023 21:01:39 +0200 Subject: [PATCH] Improve responses for evaluate get endpoints --- freqtrade/rpc/api_server/api_schemas.py | 6 ++++++ freqtrade/rpc/api_server/api_v1.py | 22 ++++++++++++++-------- tests/rpc/test_rpc_apiserver.py | 16 ++++++++-------- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/freqtrade/rpc/api_server/api_schemas.py b/freqtrade/rpc/api_server/api_schemas.py index ae9974348..e83e343dc 100644 --- a/freqtrade/rpc/api_server/api_schemas.py +++ b/freqtrade/rpc/api_server/api_schemas.py @@ -376,6 +376,12 @@ class WhitelistResponse(BaseModel): method: List[str] +class WhitelistEvaluateResponse(BaseModel): + result: Optional[WhitelistResponse] + error: Optional[str] + status: str + + class DeleteTrade(BaseModel): cancel_order_count: int result: str diff --git a/freqtrade/rpc/api_server/api_v1.py b/freqtrade/rpc/api_server/api_v1.py index 411b622b4..3a6618291 100644 --- a/freqtrade/rpc/api_server/api_v1.py +++ b/freqtrade/rpc/api_server/api_v1.py @@ -21,7 +21,8 @@ from freqtrade.rpc.api_server.api_schemas import (AvailablePairs, Balances, Blac PairListsResponse, PerformanceEntry, Ping, PlotConfig, Profit, ResultMsg, ShowConfig, Stats, StatusMsg, StrategyListResponse, StrategyResponse, - SysInfo, Version, WhitelistResponse) + SysInfo, Version, WhitelistEvaluateResponse, + WhitelistResponse) from freqtrade.rpc.api_server.deps import get_config, get_exchange, get_rpc, get_rpc_optional from freqtrade.rpc.api_server.webserver_bgwork import ApiBG from freqtrade.rpc.rpc import RPCException @@ -372,19 +373,24 @@ def pairlists_evaluate(payload: PairListsPayload, background_tasks: BackgroundTa } -@router.get('/pairlists/evaluate', response_model=WhitelistResponse, tags=['pairlists']) +@router.get('/pairlists/evaluate', response_model=WhitelistEvaluateResponse, tags=['pairlists']) def pairlists_evaluate_get(): - if ApiBG.pairlist_error: - raise HTTPException(status_code=500, - detail='Pairlist evaluation failed: ' + ApiBG.pairlist_error) if ApiBG.pairlist_running: - raise HTTPException(status_code=202, detail='Pairlist evaluation is currently running.') + return {'status': 'running'} + if ApiBG.pairlist_error: + return { + 'status': 'failed', + 'error': ApiBG.pairlist_error + } if not ApiBG.pairlist_result: - raise HTTPException(status_code=400, detail='Pairlist evaluation not started yet.') + return {'status': 'pending'} - return ApiBG.pairlist_result + return { + 'status': 'success', + 'result': ApiBG.pairlist_result + } @router.get('/freqaimodels', response_model=FreqAIModelListResponse, tags=['freqai']) diff --git a/tests/rpc/test_rpc_apiserver.py b/tests/rpc/test_rpc_apiserver.py index 6cd02f2c0..115d9ddb2 100644 --- a/tests/rpc/test_rpc_apiserver.py +++ b/tests/rpc/test_rpc_apiserver.py @@ -1645,13 +1645,13 @@ def test_api_pairlists_evaluate(botclient, tmpdir): rc = client_get(client, f"{BASE_URI}/pairlists/evaluate") - assert_response(rc, 400) - assert rc.json()['detail'] == 'Pairlist evaluation not started yet.' + assert_response(rc) + assert rc.json()['status'] == 'pending' ApiBG.pairlist_running = True rc = client_get(client, f"{BASE_URI}/pairlists/evaluate") - assert_response(rc, 202) - assert rc.json()['detail'] == 'Pairlist evaluation is currently running.' + assert_response(rc) + assert rc.json()['status'] == 'running' body = { "pairlists": [ @@ -1675,8 +1675,8 @@ def test_api_pairlists_evaluate(botclient, tmpdir): rc = client_get(client, f"{BASE_URI}/pairlists/evaluate") assert_response(rc) response = rc.json() - assert response['whitelist'] == ['ETH/BTC', 'LTC/BTC', 'XRP/BTC', 'NEO/BTC',] - assert response['length'] == 4 + assert response['result']['whitelist'] == ['ETH/BTC', 'LTC/BTC', 'XRP/BTC', 'NEO/BTC',] + assert response['result']['length'] == 4 # Restart with additional filter, reducing the list to 2 body['pairlists'].append({"method": "OffsetFilter", "number_assets": 2}) @@ -1687,8 +1687,8 @@ def test_api_pairlists_evaluate(botclient, tmpdir): rc = client_get(client, f"{BASE_URI}/pairlists/evaluate") assert_response(rc) response = rc.json() - assert response['whitelist'] == ['ETH/BTC', 'LTC/BTC', ] - assert response['length'] == 2 + assert response['result']['whitelist'] == ['ETH/BTC', 'LTC/BTC', ] + assert response['result']['length'] == 2 def test_list_available_pairs(botclient):