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 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 = threading.Thread(target=self.run, daemon=True)
thread.start() thread.start()
def run(self): def register_rest_other(self):
""" Method that runs forever """ """
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' First two arguments passed are /URL and 'Label'
Label can be used as a shortcut when refactoring Label can be used as a shortcut when refactoring
''' :return:
app.add_url_rule('/', 'hello', view_func=self.hello, methods=['GET']) """
app.add_url_rule('/stop', 'stop', view_func=self.stop, methods=['GET']) 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('/start', 'start', view_func=self.start, methods=['GET'])
app.add_url_rule('/daily', 'daily', view_func=self.daily, 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 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. 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 each Telegram command should have a like local substitute
""" """
def hello(self): def page_not_found(self, error):
# For simple rest server testing via browser # Return "404 not found", 404.
# cmds = 'Try uri:/daily?timescale=7 /profit /balance /status return jsonify({'status': 'error',
# /status /table /performance /count, 'reason': '''There's no API call for %s''' % request.base_url,
# /start /stop /help' '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>' \ rest_cmds = 'Commands implemented: <br>' \
'<a href=/daily?timescale=7>/daily?timescale=7</a>' \ '<a href=/daily?timescale=7>/daily?timescale=7</a>' \
'<br>' \ '<br>' \
@ -96,6 +118,11 @@ class ApiServerSuperWrap(RPC):
return rest_cmds return rest_cmds
def daily(self): def daily(self):
"""
Returns the last X days trading stats summary.
:return: stats
"""
try: try:
timescale = request.args.get('timescale') timescale = request.args.get('timescale')
logger.info("LocalRPC - Daily Command Called") logger.info("LocalRPC - Daily Command Called")
@ -114,7 +141,8 @@ class ApiServerSuperWrap(RPC):
def start(self): def start(self):
""" """
Handler for /start. Handler for /start.
Starts TradeThread
Starts TradeThread in bot if stopped.
""" """
msg = self._rpc_start() msg = self._rpc_start()
return jsonify(msg) return jsonify(msg)
@ -122,7 +150,8 @@ class ApiServerSuperWrap(RPC):
def stop(self): def stop(self):
""" """
Handler for /stop. Handler for /stop.
Stops TradeThread
Stops TradeThread in bot if running
""" """
msg = self._rpc_stop() msg = self._rpc_stop()
return jsonify(msg) return jsonify(msg)