Moved from decorators to app.add_url_rule

This has the benefit of creating a label which may be helpful if later
refactoring.

This change misses the main thrust of requests from both Gcarq and Shusso to
better layout the code

Im running into a challenge with 'self' not being available, or able to be
passed in either to decorators or view_func

This may simply be how I've instantiated an RPC in the wuperwrap or im within a thread
 - my very limited exposure to programming is at play! After moving code around
 lots of ways to no success and google not being helpful im committing for further feeback
This commit is contained in:
creslinux 2018-06-15 09:14:17 +00:00
parent 9aa08ec3c1
commit d7dfa00cb3

View File

@ -1,9 +1,9 @@
import threading import threading
import logging import logging
import json # import json
from flask import Flask, request from flask import Flask, request
from flask_restful import Resource, Api # from flask_restful import Resource, Api
from json import dumps from json import dumps
from freqtrade.rpc.rpc import RPC, RPCException from freqtrade.rpc.rpc import RPC, RPCException
from ipaddress import IPv4Address from ipaddress import IPv4Address
@ -36,10 +36,10 @@ class ApiServerSuperWrap(RPC):
self._config = freqtrade.config self._config = freqtrade.config
""" """
Define the application routes here Define the application methods here, called by app.add_url_rule
each Telegram command should have a like local substitute each Telegram command should have a like local substitute
""" """
@app.route("/") # @app.route("/")
def hello(): def hello():
# For simple rest server testing via browser # For simple rest server testing via browser
# cmds = 'Try uri:/daily?timescale=7 /profit /balance /status # cmds = 'Try uri:/daily?timescale=7 /profit /balance /status
@ -54,7 +54,6 @@ class ApiServerSuperWrap(RPC):
'<a href=/start>/start</a>' '<a href=/start>/start</a>'
return rest_cmds return rest_cmds
@app.route('/daily', methods=['GET'])
def daily(): def daily():
try: try:
timescale = request.args.get('timescale') timescale = request.args.get('timescale')
@ -71,29 +70,36 @@ class ApiServerSuperWrap(RPC):
except RPCException as e: except RPCException as e:
return e return e
@app.route('/start', methods=['GET'])
def start(): def start():
""" """
Handler for /start. Handler for /start.
Starts TradeThread Starts TradeThread
""" """
msg = self._rpc_start() msg = self._rpc_start()
print("msg is", msg)
return msg return msg
@app.route('/stop', methods=['GET'])
def stop(): def stop():
""" """
Handler for /stop. Handler for /stop.
Stops TradeThread Stops TradeThread
""" """
msg = self._rpc_stop() msg = self._rpc_stop()
print("msg is", msg)
return msg return msg
## 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=hello, methods=['GET'])
app.add_url_rule('/stop', 'stop', view_func=stop, methods=['GET'])
app.add_url_rule('/start', 'start', view_func=start, methods=['GET'])
app.add_url_rule('/daily', 'daily', view_func=daily, methods=['GET'])
""" """
Section to handle configuration and running of the Rest serve Section to handle configuration and running of the Rest server
also to check and warn if not bound to 127.0.0.1 as a security risk. also to check and warn if not bound to a loopback, warn on security risk.
""" """
rest_ip = self._config['api_server']['listen_ip_address'] rest_ip = self._config['api_server']['listen_ip_address']
rest_port = self._config['api_server']['listen_port'] rest_port = self._config['api_server']['listen_port']
@ -104,5 +110,9 @@ class ApiServerSuperWrap(RPC):
# Run the Server # Run the Server
logger.info('Starting Local Rest Server') logger.info('Starting Local Rest Server')
app.run(host=rest_ip, port=rest_port) try:
app.run(host=rest_ip, port=rest_port)
except:
logger.exception("Api server failed to start, exception message is:")