1
- from flask import Flask , Response
1
+ from flask import Flask , Response , app , render_template
2
2
from flask_restful import reqparse , abort , Api , Resource
3
3
from flask_cors import CORS
4
4
import json
5
5
6
6
from API .APIResult import APIResult
7
+ from Bot .ExchangeInfo import ExchangeInfo
7
8
from Bot .ConfigLoader import ConfigLoader
8
9
from Bot .TradeHandler import TradeHandler
9
10
from Utils .Logger import Logger
10
11
11
12
13
+ app = Flask (__name__ , static_folder = 'templates' , static_url_path = '' )
14
+
15
+ @app .route ('/' )
16
+ @app .route ('/trades' )
17
+ def index ():
18
+ return render_template ('index.html' )
19
+
20
+
12
21
class APIServer :
13
22
VERSION = 'v1'
14
23
API_PREFIX = '/api/' + VERSION
15
24
16
25
def __init__ (self , trade_handler : TradeHandler ):
17
26
self .th = trade_handler
18
- self .app = Flask ( __name__ )
27
+ self .app = app
19
28
self .api = Api (self .app )
20
29
CORS (self .app )
21
30
@@ -25,12 +34,14 @@ def __init__(self, trade_handler: TradeHandler):
25
34
resource_class_kwargs = {'trade_handler' : self .th })
26
35
self .api .add_resource (Trade , APIServer .API_PREFIX + '/trade/<id>' ,
27
36
resource_class_kwargs = {'trade_handler' : self .th })
28
- self .api .add_resource (Managment , APIServer .API_PREFIX + '/management/<action>' ,
29
- resource_class_kwargs = {'trade_handler' : self .th })
37
+ # self.api.add_resource(Managment, APIServer.API_PREFIX + '/management/<action>',
38
+ # resource_class_kwargs={'trade_handler': self.th})
30
39
40
+ self .api .add_resource (APIExchangeInfo , APIServer .API_PREFIX + '/info' ,
41
+ resource_class_kwargs = {'trade_handler' : self .th })
31
42
32
43
def run (self , port ):
33
- self .app .run (debug = True , port = port , use_reloader = False )
44
+ self .app .run (host = '0.0.0.0' , debug = True , port = port , use_reloader = False )
34
45
35
46
36
47
class BotAPIREsource (Resource , Logger ):
@@ -40,6 +51,21 @@ def __init__(self, trade_handler: TradeHandler):
40
51
self .th : TradeHandler = trade_handler
41
52
42
53
54
+ class APIExchangeInfo (BotAPIREsource ):
55
+ def get (self ):
56
+ return list (ExchangeInfo ().get_all_symbols ())
57
+
58
+ def post (self ):
59
+ args = self .parser .parse_args ()
60
+ action = args ['action' ]
61
+
62
+ if not action :
63
+ return APIResult .ErrorResult (100 , msg = 'No "action" was provided' ), 403
64
+
65
+ if action == 'reconnect' :
66
+ self .th .force_reconnect_sockets ()
67
+
68
+
43
69
class TradeList (BotAPIREsource ):
44
70
def get (self ):
45
71
return [{
@@ -70,8 +96,8 @@ def delete(self, id):
70
96
if not strategies :
71
97
return APIResult .ErrorResult (101 , msg = 'No strategies were found' ), 404
72
98
73
- # for strategy in strategies:
74
- # self.th.remove_trade_by_strategy(strategy, True)
99
+ for strategy in strategies :
100
+ self .th .remove_trade_by_strategy (strategy , True )
75
101
76
102
return APIResult .OKResult ()
77
103
@@ -119,27 +145,26 @@ def post(self, id=None):
119
145
self .th .updated_trade (trade )
120
146
self .th .fire_trade_updated (trade , True )
121
147
except Exception as e :
122
- # return Response(APIResult.ErrorResult(100, e), 500, mimetype='application/json')
123
148
return json .dumps (APIResult .ErrorResult (100 , str (e ))), 500
124
149
125
150
return APIResult .OKResult (ids ), 201
126
151
127
- return APIResult .OKResult ()
128
-
129
- class Managment (BotAPIREsource ):
130
- def post (self , action ):
131
- if not action :
132
- return 'action should be \' pause\' or \' start\' '
133
-
134
- action = action .lower ()
135
-
136
- if action == 'pause' :
137
- self .th .pause ()
138
-
139
- elif action == 'start' :
140
- self .th .resume ()
141
-
142
- elif action == 'cancel' :
143
- pass
144
-
145
- return {}
152
+ return APIResult .OKResult (), 200
153
+
154
+ # class Managment(BotAPIREsource):
155
+ # def post(self, action):
156
+ # if not action:
157
+ # return 'action should be \'pause\' or \'start\''
158
+ #
159
+ # action = action.lower()
160
+ #
161
+ # if action == 'pause':
162
+ # self.th.pause()
163
+ #
164
+ # elif action == 'start':
165
+ # self.th.resume()
166
+ #
167
+ # elif action == 'cancel':
168
+ # pass
169
+ #
170
+ # return {}
0 commit comments