freqtrade_origin/freqtrade/rpc/api_server/deps.py

62 lines
1.5 KiB
Python
Raw Normal View History

2023-03-19 14:06:56 +00:00
from typing import Any, AsyncIterator, Dict, Optional
from uuid import uuid4
2021-01-02 11:54:40 +00:00
2022-01-22 06:11:59 +00:00
from fastapi import Depends
from freqtrade.enums import RunMode
from freqtrade.persistence import Trade
from freqtrade.persistence.models import _request_id_ctx_var
from freqtrade.rpc.api_server.webserver_bgwork import ApiBG
2021-01-02 12:12:49 +00:00
from freqtrade.rpc.rpc import RPC, RPCException
2021-01-02 11:54:40 +00:00
from .webserver import ApiServer
2021-01-02 12:12:49 +00:00
def get_rpc_optional() -> Optional[RPC]:
if ApiServer._has_rpc:
return ApiServer._rpc
return None
2023-03-19 14:06:56 +00:00
async def get_rpc() -> Optional[AsyncIterator[RPC]]:
2021-01-02 12:12:49 +00:00
_rpc = get_rpc_optional()
if _rpc:
request_id = str(uuid4())
ctx_token = _request_id_ctx_var.set(request_id)
Trade.rollback()
try:
yield _rpc
finally:
Trade.session.remove()
_request_id_ctx_var.reset(ctx_token)
2021-01-02 12:12:49 +00:00
else:
raise RPCException('Bot is not in the correct state')
2021-01-02 11:54:40 +00:00
def get_config() -> Dict[str, Any]:
return ApiServer._config
2020-12-27 14:24:49 +00:00
2021-01-02 11:54:40 +00:00
def get_api_config() -> Dict[str, Any]:
2020-12-27 14:24:49 +00:00
return ApiServer._config['api_server']
2022-01-22 06:11:59 +00:00
def get_exchange(config=Depends(get_config)):
if not ApiBG.exchange:
2022-01-22 06:11:59 +00:00
from freqtrade.resolvers import ExchangeResolver
ApiBG.exchange = ExchangeResolver.load_exchange(
2023-05-13 06:27:27 +00:00
config, load_leverage_tiers=False)
return ApiBG.exchange
2022-11-15 03:27:45 +00:00
def get_message_stream():
return ApiServer._message_stream
def is_webserver_mode(config=Depends(get_config)):
if config['runmode'] != RunMode.WEBSERVER:
raise RPCException('Bot is not in the correct state')
return None