]>
Commit | Line | Data |
---|---|---|
7a9e5112 | 1 | 'use strict'; |
2 | ||
989fb5c7 | 3 | import App from './app.js'; |
7a9e5112 | 4 | |
5 | var Api = {}; | |
6 | ||
7 | Api.API_HOST = process.env.API_HOST; | |
8 | Api.API_PORT = process.env.API_PORT; | |
9 | ||
10 | if (process.env.API_HTTPS === 'true') { | |
11 | Api.API_ROOT = 'https://'; | |
12 | } else { | |
13 | Api.API_ROOT = 'http://'; | |
14 | } | |
15 | ||
16 | Api.API_ROOT += Api.API_HOST; | |
17 | if (Api.API_PORT !== '80') { | |
18 | Api.API_ROOT += ':' + Api.API_PORT; | |
19 | } | |
20 | ||
21 | Api.API_ROOT += '/api'; | |
22 | ||
23 | var ApiEndpoints = { | |
24 | 'SIGNUP': { | |
25 | 'type': 'POST', | |
26 | 'auth': false, | |
27 | 'parameters': [ | |
28 | {'name': 'email', 'mandatory': true, 'inquery': true}, | |
29 | {'name': 'password', 'mandatory': true, 'inquery': true} | |
30 | ], | |
989fb5c7 | 31 | 'buildUrl': function() { |
7a9e5112 | 32 | return '/signup'; |
33 | } | |
34 | }, | |
35 | 'SIGNIN': { | |
36 | 'type': 'POST', | |
37 | 'auth': false, | |
38 | 'parameters': [ | |
39 | {'name': 'email', 'mandatory': true, 'inquery': true}, | |
40 | {'name': 'password', 'mandatory': true, 'inquery': true} | |
41 | ], | |
989fb5c7 | 42 | 'buildUrl': function() { |
7a9e5112 | 43 | return '/signin'; |
44 | } | |
45 | }, | |
85545aba | 46 | 'RESET_PASSWORD': { |
47 | 'type': 'POST', | |
48 | 'auth': false, | |
49 | 'parameters': [ | |
50 | {'name': 'email', 'mandatory': true, 'inquery': true}, | |
51 | ], | |
52 | 'buildUrl': function() { | |
53 | return '/passwordreset'; | |
54 | } | |
55 | }, | |
56 | 'CHANGE_PASSWORD': { | |
57 | 'type': 'POST', | |
58 | 'auth': false, | |
59 | 'parameters': [ | |
60 | {'name': 'token', 'mandatory': true, 'inquery': true}, | |
61 | {'name': 'password', 'mandatory': true, 'inquery': true}, | |
62 | ], | |
63 | 'buildUrl': function() { | |
64 | return '/changepassword'; | |
65 | } | |
66 | }, | |
7a9e5112 | 67 | 'MARKET': { |
68 | 'type': 'GET', | |
69 | 'auth': true, | |
70 | 'parameters': [ | |
71 | {'name': 'name', 'mandatory': true, 'inquery': false}, | |
72 | ], | |
73 | 'buildUrl': function(params) { | |
74 | return '/market/' + params.name; | |
75 | } | |
76 | }, | |
24e47979 | 77 | 'MARKET_TEST_CREDENTIALS': { |
2f91f20a | 78 | 'type': 'GET', |
79 | 'auth': true, | |
80 | 'parameters': [ | |
24e47979 | 81 | {'name': 'name', 'mandatory': true, 'inquery': false}, |
82 | ], | |
83 | 'buildUrl': function(params) { | |
84 | return '/market/' + params.name + '/test-credentials'; | |
85 | } | |
86 | }, | |
87 | 'MARKET_GET_PORTFOLIO': { | |
88 | 'type': 'GET', | |
89 | 'auth': true, | |
90 | 'parameters': [ | |
91 | {'name': 'name', 'mandatory': true, 'inquery': false}, | |
2f91f20a | 92 | ], |
93 | 'buildUrl': function(params) { | |
24e47979 | 94 | return '/market/' + params.name + '/portfolio'; |
2f91f20a | 95 | } |
96 | }, | |
7a9e5112 | 97 | 'UPDATE_MARKET': { |
98 | 'type': 'POST', | |
99 | 'auth': true, | |
100 | 'parameters': [ | |
101 | {'name': 'name', 'mandatory': true, 'inquery': false}, | |
102 | {'name': 'key', 'mandatory': true, 'inquery': true}, | |
103 | {'name': 'secret', 'mandatory': true, 'inquery': true}, | |
104 | ], | |
105 | 'buildUrl': function(params) { | |
106 | return '/market/' + params.name + '/update'; | |
107 | } | |
108 | }, | |
109 | 'OTP_ENROLL': { | |
110 | 'type': 'GET', | |
111 | 'auth': true, | |
112 | 'parameters': [], | |
989fb5c7 | 113 | 'buildUrl': function() { |
7a9e5112 | 114 | return '/otp/enroll'; |
115 | } | |
116 | }, | |
117 | 'OTP_VALIDATE': { | |
118 | 'type': 'POST', | |
119 | 'auth': true, | |
120 | 'parameters': [ | |
121 | {'name': 'pass', 'mandatory': true, 'inquery': true}, | |
122 | ], | |
989fb5c7 | 123 | 'buildUrl': function() { |
7a9e5112 | 124 | return '/otp/validate'; |
125 | } | |
126 | }, | |
127 | }; | |
128 | ||
129 | Api.BuildRequest = function(endpointId, params) { | |
130 | var endpoint = ApiEndpoints[endpointId]; | |
131 | var query = {}; | |
132 | var url = ''; | |
133 | var headers = {}; | |
134 | var jwt = App.getUserToken(); | |
135 | ||
136 | if (endpoint === undefined) { | |
137 | return {'err': 'cannot find endpoint ' + endpointId}; | |
138 | } | |
139 | ||
140 | if (endpoint.auth === true && (jwt === undefined || jwt === null)) { | |
141 | return {'err': 'this endpoint needs auth'}; | |
142 | } | |
143 | ||
144 | if (jwt !== undefined && jwt !== null) { | |
145 | headers.Authorization = 'Bearer ' + jwt; | |
146 | } | |
147 | ||
148 | for (var i = 0; i < endpoint.parameters.length; i++) { | |
149 | var parameter = endpoint.parameters[i]; | |
150 | if (parameter.mandatory === true && params[parameter.name] === undefined) { | |
151 | return {'err': 'parameter \'' + parameter.name + '\' is mandatory'}; | |
152 | } | |
153 | ||
154 | if (parameter.inquery === true) { | |
155 | query[parameter.name] = params[parameter.name]; | |
156 | } | |
157 | } | |
158 | ||
159 | url = endpoint.buildUrl(params); | |
160 | ||
161 | return {'err': null, 'url': Api.API_ROOT + url, 'headers': headers, 'params': query, 'type': endpoint.type}; | |
162 | }; | |
163 | ||
164 | Api.Call = function(endpointId, params, fn) { | |
165 | var request = Api.BuildRequest(endpointId, params); | |
166 | ||
167 | if (request.err !== null) { | |
168 | console.error('Api BuildRequest error', request.err); | |
169 | fn({'err': request.err}, -1, {}); | |
170 | return null; | |
171 | } | |
172 | ||
173 | return Api.DoRequest(request.type, request.url, request.params, request.headers, fn); | |
174 | }; | |
175 | ||
176 | Api.DoRequest = function(type, url, params, headers, callback) { | |
177 | return $.ajax({ | |
178 | data: params, | |
179 | headers: headers, | |
180 | timeout: 30000, | |
181 | dataType: 'json', | |
182 | error: function(xhr, status, err) { | |
183 | if (status === 'abort') { | |
184 | return; | |
185 | } | |
186 | ||
187 | var apiStatus = null; | |
188 | var apiResponse = null; | |
189 | var apiCode = null; | |
190 | if (xhr.responseJSON) { | |
191 | apiStatus = xhr.responseJSON.status; | |
192 | apiResponse = xhr.responseJSON.response; | |
193 | apiCode = xhr.responseJSON.code; | |
194 | } | |
195 | ||
196 | if (xhr.status === 401 || xhr.status === 403) { | |
197 | if (App.onUserNotAuthorized(xhr.status, apiCode) === false) { | |
198 | return; | |
199 | } | |
200 | } | |
201 | ||
202 | callback({'url': url, 'err': err, 'status': status, 'code': apiCode, 'xhr': xhr}, apiStatus, apiResponse); | |
203 | }, | |
204 | success: function(data) { | |
205 | var err = null; | |
206 | if (data.status !== 'ok') { | |
207 | err = {'url': url, 'status': status}; | |
208 | } | |
209 | callback(err, data.status, data.response); | |
210 | }, | |
211 | type: type, | |
212 | url: url | |
213 | }); | |
214 | }; | |
215 | ||
989fb5c7 | 216 | export default Api; |
7a9e5112 | 217 |