Merge pull request #943 from creslinux/feature/flask-rest

Moved registering application urls out of the run def
This commit is contained in:
Michael Egger 2018-06-23 11:54:15 +02:00 committed by GitHub
commit 6389869fda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -27,22 +27,36 @@ class ApiServerSuperWrap(RPC):
self._config = freqtrade.config
# Register application handling
self.register_rest_other()
self.register_rest_rpc_urls()
thread = threading.Thread(target=self.run, daemon=True)
thread.start()
def run(self):
""" Method that runs forever """
def register_rest_other(self):
"""
Registers flask app URLs that are not calls to functionality in rpc.rpc.
:return:
"""
app.register_error_handler(404, self.page_not_found)
app.add_url_rule('/', 'hello', view_func=self.hello, methods=['GET'])
def register_rest_rpc_urls(self):
"""
Registers flask app URLs that are calls to functonality in rpc.rpc.
# defines the url rules available on the api server
'''
First two arguments passed are /URL and 'Label'
Label can be used as a shortcut when refactoring
'''
app.add_url_rule('/', 'hello', view_func=self.hello, methods=['GET'])
:return:
"""
app.add_url_rule('/stop', 'stop', view_func=self.stop, methods=['GET'])
app.add_url_rule('/start', 'start', view_func=self.start, methods=['GET'])
app.add_url_rule('/daily', 'daily', view_func=self.daily, methods=['GET'])
def run(self):
""" Method that runs flask app in its own thread forever """
"""
Section to handle configuration and running of the Rest server
also to check and warn if not bound to a loopback, warn on security risk.
@ -81,12 +95,20 @@ class ApiServerSuperWrap(RPC):
each Telegram command should have a like local substitute
"""
def hello(self):
# For simple rest server testing via browser
# cmds = 'Try uri:/daily?timescale=7 /profit /balance /status
# /status /table /performance /count,
# /start /stop /help'
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>/daily?timescale=7</a>' \
'<br>' \
@ -96,6 +118,11 @@ class ApiServerSuperWrap(RPC):
return rest_cmds
def daily(self):
"""
Returns the last X days trading stats summary.
:return: stats
"""
try:
timescale = request.args.get('timescale')
logger.info("LocalRPC - Daily Command Called")
@ -114,7 +141,8 @@ class ApiServerSuperWrap(RPC):
def start(self):
"""
Handler for /start.
Starts TradeThread
Starts TradeThread in bot if stopped.
"""
msg = self._rpc_start()
return jsonify(msg)
@ -122,7 +150,8 @@ class ApiServerSuperWrap(RPC):
def stop(self):
"""
Handler for /stop.
Stops TradeThread
Stops TradeThread in bot if running
"""
msg = self._rpc_stop()
return jsonify(msg)