Skip to content

Commit

Permalink
Merge pull request freqtrade#4426 from freqtrade/fix/4405
Browse files Browse the repository at this point in the history
Don't fail API calls when live price is not available
  • Loading branch information
xmatthias authored Feb 22, 2021
2 parents 6288516 + 228e51b commit 2b5f1ff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions freqtrade/rpc/api_server/webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from ipaddress import IPv4Address
from typing import Any, Dict

import rapidjson
import uvicorn
from fastapi import Depends, FastAPI
from fastapi.middleware.cors import CORSMiddleware
Expand All @@ -14,6 +15,17 @@
logger = logging.getLogger(__name__)


class FTJSONResponse(JSONResponse):
media_type = "application/json"

def render(self, content: Any) -> bytes:
"""
Use rapidjson for responses
Handles NaN and Inf / -Inf in a javascript way by default.
"""
return rapidjson.dumps(content).encode("utf-8")


class ApiServer(RPCHandler):

_rpc: RPC
Expand All @@ -32,6 +44,7 @@ def __init__(self, rpc: RPC, config: Dict[str, Any]) -> None:
self.app = FastAPI(title="Freqtrade API",
docs_url='/docs' if api_config.get('enable_openapi', False) else None,
redoc_url=None,
default_response_class=FTJSONResponse,
)
self.configure_app(self.app, self._config)

Expand Down
11 changes: 11 additions & 0 deletions tests/rpc/test_rpc_apiserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
from fastapi import FastAPI
from fastapi.exceptions import HTTPException
from fastapi.testclient import TestClient
from numpy import isnan
from requests.auth import _basic_auth_str

from freqtrade.__init__ import __version__
from freqtrade.exceptions import ExchangeError
from freqtrade.loggers import setup_logging, setup_logging_pre
from freqtrade.persistence import PairLocks, Trade
from freqtrade.rpc import RPC
Expand Down Expand Up @@ -789,6 +791,15 @@ def test_api_status(botclient, mocker, ticker, fee, markets):
'exchange': 'bittrex',
}]

mocker.patch('freqtrade.freqtradebot.FreqtradeBot.get_sell_rate',
MagicMock(side_effect=ExchangeError("Pair 'ETH/BTC' not available")))

rc = client_get(client, f"{BASE_URI}/status")
assert_response(rc)
resp_values = rc.json()
assert len(resp_values) == 1
assert isnan(resp_values[0]['profit_abs'])


def test_api_version(botclient):
ftbot, client = botclient
Expand Down

0 comments on commit 2b5f1ff

Please sign in to comment.