rest_dump
This commit is contained in:
parent
6bb2fad9b0
commit
96a260b027
@ -4,7 +4,7 @@ import logging
|
||||
# import json
|
||||
from typing import Dict
|
||||
|
||||
from flask import Flask, request
|
||||
from flask import Flask, request, jsonify
|
||||
# from flask_restful import Resource, Api
|
||||
from freqtrade.rpc.rpc import RPC, RPCException
|
||||
from ipaddress import IPv4Address
|
||||
@ -39,11 +39,15 @@ class ApiServer(RPC):
|
||||
thread.start()
|
||||
|
||||
def cleanup(self) -> None:
|
||||
pass
|
||||
logger.info("Stopping API Server")
|
||||
|
||||
def send_msg(self, msg: Dict[str, str]) -> None:
|
||||
pass
|
||||
|
||||
def rest_dump(self, return_value):
|
||||
""" Helper function to jsonify object for a webserver """
|
||||
return jsonify(return_value)
|
||||
|
||||
def register_rest_other(self):
|
||||
"""
|
||||
Registers flask app URLs that are not calls to functionality in rpc.rpc.
|
||||
@ -89,6 +93,7 @@ class ApiServer(RPC):
|
||||
app.run(host=rest_ip, port=rest_port)
|
||||
except Exception:
|
||||
logger.exception("Api server failed to start, exception message is:")
|
||||
logger.info('Starting Local Rest Server_end')
|
||||
|
||||
"""
|
||||
Define the application methods here, called by app.add_url_rule
|
||||
@ -99,7 +104,7 @@ class ApiServer(RPC):
|
||||
"""
|
||||
Return "404 not found", 404.
|
||||
"""
|
||||
return json.dumps({
|
||||
return self.rest_dump({
|
||||
'status': 'error',
|
||||
'reason': '''There's no API call for %s''' % request.base_url,
|
||||
'code': 404
|
||||
@ -113,20 +118,14 @@ class ApiServer(RPC):
|
||||
This may be deprecated at any time.
|
||||
:return: index.html
|
||||
"""
|
||||
rest_cmds = 'Commands implemented: <br>' \
|
||||
'<a href=/daily?timescale=7>Show 7 days of stats</a>' \
|
||||
'<br>' \
|
||||
'<a href=/stop>Stop the Trade thread</a>' \
|
||||
'<br>' \
|
||||
'<a href=/start>Start the Traded thread</a>' \
|
||||
'<br>' \
|
||||
'<a href=/profit>Show profit summary</a>' \
|
||||
'<br>' \
|
||||
'<a href=/status_table>Show status table - Open trades</a>' \
|
||||
'<br>' \
|
||||
'<a href=/paypal> 404 page does not exist</a>' \
|
||||
'<br>'
|
||||
|
||||
rest_cmds = ('Commands implemented: <br>'
|
||||
'<a href=/daily?timescale=7>Show 7 days of stats</a><br>'
|
||||
'<a href=/stop>Stop the Trade thread</a><br>'
|
||||
'<a href=/start>Start the Traded thread</a><br>'
|
||||
'<a href=/profit>Show profit summary</a><br>'
|
||||
'<a href=/status_table>Show status table - Open trades</a><br>'
|
||||
'<a href=/paypal> 404 page does not exist</a><br>'
|
||||
)
|
||||
return rest_cmds
|
||||
|
||||
def daily(self):
|
||||
@ -145,7 +144,7 @@ class ApiServer(RPC):
|
||||
self._config['fiat_display_currency']
|
||||
)
|
||||
|
||||
return json.dumps(stats, indent=4, sort_keys=True, default=str)
|
||||
return self.rest_dump(stats)
|
||||
except RPCException as e:
|
||||
logger.exception("API Error querying daily:", e)
|
||||
return "Error querying daily"
|
||||
@ -164,7 +163,7 @@ class ApiServer(RPC):
|
||||
self._config['fiat_display_currency']
|
||||
)
|
||||
|
||||
return json.dumps(stats, indent=4, sort_keys=True, default=str)
|
||||
return self.rest_dump(stats)
|
||||
except RPCException as e:
|
||||
logger.exception("API Error calling profit", e)
|
||||
return "Error querying closed trades - maybe there are none"
|
||||
@ -178,7 +177,7 @@ class ApiServer(RPC):
|
||||
"""
|
||||
try:
|
||||
results = self._rpc_trade_status()
|
||||
return json.dumps(results, indent=4, sort_keys=True, default=str)
|
||||
return self.rest_dump(results)
|
||||
|
||||
except RPCException as e:
|
||||
logger.exception("API Error calling status table", e)
|
||||
@ -191,7 +190,7 @@ class ApiServer(RPC):
|
||||
Starts TradeThread in bot if stopped.
|
||||
"""
|
||||
msg = self._rpc_start()
|
||||
return json.dumps(msg)
|
||||
return self.rest_dump(msg)
|
||||
|
||||
def stop(self):
|
||||
"""
|
||||
@ -200,4 +199,4 @@ class ApiServer(RPC):
|
||||
Stops TradeThread in bot if running
|
||||
"""
|
||||
msg = self._rpc_stop()
|
||||
return json.dumps(msg)
|
||||
return self.rest_dump(msg)
|
||||
|
@ -1,74 +0,0 @@
|
||||
import logging
|
||||
import flask
|
||||
from flask import request, jsonify
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class MyApiApp(flask.Flask):
|
||||
def __init__(self, import_name):
|
||||
"""
|
||||
Contains common rest routes and resource that do not need
|
||||
to access to rpc.rpc functionality
|
||||
"""
|
||||
super(MyApiApp, self).__init__(import_name)
|
||||
|
||||
"""
|
||||
Registers flask app URLs that are not calls to functionality in rpc.rpc.
|
||||
:return:
|
||||
"""
|
||||
self.before_request(self.my_preprocessing)
|
||||
self.register_error_handler(404, self.page_not_found)
|
||||
self.add_url_rule('/', 'hello', view_func=self.hello, methods=['GET'])
|
||||
self.add_url_rule('/stop_api', 'stop_api', view_func=self.stop_api, methods=['GET'])
|
||||
|
||||
def my_preprocessing(self):
|
||||
# Do stuff to flask.request
|
||||
pass
|
||||
|
||||
def page_not_found(self, error):
|
||||
# Return "404 not found", 404.
|
||||
return jsonify({'status': 'error',
|
||||
'reason': '''There's no API call for %s''' % request.base_url,
|
||||
'code': 404}), 404
|
||||
|
||||
def hello(self):
|
||||
"""
|
||||
None critical but helpful default index page.
|
||||
|
||||
That lists URLs added to the flask server.
|
||||
This may be deprecated at any time.
|
||||
:return: index.html
|
||||
"""
|
||||
rest_cmds = 'Commands implemented: <br>' \
|
||||
'<a href=/daily?timescale=7>Show 7 days of stats</a>' \
|
||||
'<br>' \
|
||||
'<a href=/stop>Stop the Trade thread</a>' \
|
||||
'<br>' \
|
||||
'<a href=/start>Start the Traded thread</a>' \
|
||||
'<br>' \
|
||||
'<a href=/paypal> 404 page does not exist</a>' \
|
||||
'<br>' \
|
||||
'<br>' \
|
||||
'<a href=/stop_api>Shut down the api server - be sure</a>'
|
||||
return rest_cmds
|
||||
|
||||
def stop_api(self):
|
||||
""" For calling shutdown_api_server over via api server HTTP"""
|
||||
self.shutdown_api_server()
|
||||
return 'Api Server shutting down... '
|
||||
|
||||
def shutdown_api_server(self):
|
||||
"""
|
||||
Stop the running flask application
|
||||
|
||||
Records the shutdown in logger.info
|
||||
:return:
|
||||
"""
|
||||
func = request.environ.get('werkzeug.server.shutdown')
|
||||
if func is None:
|
||||
raise RuntimeError('Not running the Flask Werkzeug Server')
|
||||
if func is not None:
|
||||
logger.info('Stopping the Local Rest Server')
|
||||
func()
|
||||
return
|
Loading…
Reference in New Issue
Block a user