From d0d128d20304bac9982b9f901c3f12e7ca7c1654 Mon Sep 17 00:00:00 2001 From: creslinux Date: Sat, 23 Jun 2018 09:08:39 +0000 Subject: [PATCH 1/4] Moved registering application urls out of the run def and into their own Added 404 handling Split registration of URLs that use rpc.rpc and others into own def. Seems logical to be able to register separately for later use. --- freqtrade/rpc/api_server.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/freqtrade/rpc/api_server.py b/freqtrade/rpc/api_server.py index 8927584fe..945e870bc 100644 --- a/freqtrade/rpc/api_server.py +++ b/freqtrade/rpc/api_server.py @@ -27,13 +27,19 @@ 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): + #Added as a placeholder for app URLs that are not implemented in rpc.rpc + app.register_error_handler(404, self.page_not_found) - # defines the url rules available on the api server + def register_rest_rpc_urls(self): + # register 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 @@ -43,6 +49,9 @@ class ApiServerSuperWrap(RPC): 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 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,6 +90,12 @@ class ApiServerSuperWrap(RPC): each Telegram command should have a like local substitute """ + 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): # For simple rest server testing via browser # cmds = 'Try uri:/daily?timescale=7 /profit /balance /status From 5cda78bf324838979b43b452ce7ba83cffd26d03 Mon Sep 17 00:00:00 2001 From: creslinux Date: Sat, 23 Jun 2018 09:19:34 +0000 Subject: [PATCH 2/4] moved default page "/" index into self_register_other() out of the block of URLs that call rpc.rcp functionality. --- freqtrade/rpc/api_server.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/freqtrade/rpc/api_server.py b/freqtrade/rpc/api_server.py index 945e870bc..7c8d33231 100644 --- a/freqtrade/rpc/api_server.py +++ b/freqtrade/rpc/api_server.py @@ -37,14 +37,16 @@ class ApiServerSuperWrap(RPC): def register_rest_other(self): #Added as a placeholder for app URLs that are not implemented in rpc.rpc 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): # register the url rules available on the api server + # This is where to register rest urls that make use of + # rpc.rpc functions ''' 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']) 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']) From 6c3ea7c5f92631412fe46d0b2d0350cf019386d8 Mon Sep 17 00:00:00 2001 From: creslinux Date: Sat, 23 Jun 2018 09:22:25 +0000 Subject: [PATCH 3/4] flake 8 fix --- freqtrade/rpc/api_server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/freqtrade/rpc/api_server.py b/freqtrade/rpc/api_server.py index 7c8d33231..3b51126a9 100644 --- a/freqtrade/rpc/api_server.py +++ b/freqtrade/rpc/api_server.py @@ -35,7 +35,7 @@ class ApiServerSuperWrap(RPC): thread.start() def register_rest_other(self): - #Added as a placeholder for app URLs that are not implemented in rpc.rpc + # Added as a placeholder for app URLs that are not implemented in rpc.rpc app.register_error_handler(404, self.page_not_found) app.add_url_rule('/', 'hello', view_func=self.hello, methods=['GET']) From c72397936edc526423edbd138d6f081f5b71b117 Mon Sep 17 00:00:00 2001 From: creslinux Date: Sat, 23 Jun 2018 09:48:51 +0000 Subject: [PATCH 4/4] Updated def comments to be __docstring__ compatible --- freqtrade/rpc/api_server.py | 40 ++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/freqtrade/rpc/api_server.py b/freqtrade/rpc/api_server.py index 3b51126a9..3a8391b98 100644 --- a/freqtrade/rpc/api_server.py +++ b/freqtrade/rpc/api_server.py @@ -35,24 +35,27 @@ class ApiServerSuperWrap(RPC): thread.start() def register_rest_other(self): - # Added as a placeholder for app URLs that are not implemented in rpc.rpc + """ + 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): - # register the url rules available on the api server - # This is where to register rest urls that make use of - # rpc.rpc functions - ''' + """ + Registers flask app URLs that are calls to functonality in rpc.rpc. + First two arguments passed are /URL and 'Label' Label can be used as a shortcut when refactoring - ''' + :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 forever """ + """ Method that runs flask app in its own thread forever """ """ Section to handle configuration and running of the Rest server @@ -93,17 +96,19 @@ class ApiServerSuperWrap(RPC): """ def page_not_found(self, error): - # return "404 not found", 404 + # 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): - # For simple rest server testing via browser - # cmds = 'Try uri:/daily?timescale=7 /profit /balance /status - # /status /table /performance /count, - # /start /stop /help' + """ + 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:
' \ '/daily?timescale=7' \ '
' \ @@ -113,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") @@ -131,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) @@ -139,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)