This commit is contained in:
Matthias 2021-02-22 12:11:27 +01:00
parent 5e4730b73b
commit 228e51b60b
2 changed files with 15 additions and 1 deletions

View File

@ -2,6 +2,7 @@ import logging
from ipaddress import IPv4Address from ipaddress import IPv4Address
from typing import Any, Dict from typing import Any, Dict
import rapidjson
import uvicorn import uvicorn
from fastapi import Depends, FastAPI from fastapi import Depends, FastAPI
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
@ -14,6 +15,17 @@ from freqtrade.rpc.rpc import RPC, RPCException, RPCHandler
logger = logging.getLogger(__name__) 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): class ApiServer(RPCHandler):
_rpc: RPC _rpc: RPC
@ -32,6 +44,7 @@ class ApiServer(RPCHandler):
self.app = FastAPI(title="Freqtrade API", self.app = FastAPI(title="Freqtrade API",
docs_url='/docs' if api_config.get('enable_openapi', False) else None, docs_url='/docs' if api_config.get('enable_openapi', False) else None,
redoc_url=None, redoc_url=None,
default_response_class=FTJSONResponse,
) )
self.configure_app(self.app, self._config) self.configure_app(self.app, self._config)

View File

@ -11,6 +11,7 @@ import uvicorn
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.exceptions import HTTPException from fastapi.exceptions import HTTPException
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from numpy import isnan
from requests.auth import _basic_auth_str from requests.auth import _basic_auth_str
from freqtrade.__init__ import __version__ from freqtrade.__init__ import __version__
@ -797,7 +798,7 @@ def test_api_status(botclient, mocker, ticker, fee, markets):
assert_response(rc) assert_response(rc)
resp_values = rc.json() resp_values = rc.json()
assert len(resp_values) == 1 assert len(resp_values) == 1
assert resp_values[0]['profit_abs'] is None assert isnan(resp_values[0]['profit_abs'])
def test_api_version(botclient): def test_api_version(botclient):