· 6 years ago · Jan 22, 2020, 10:54 PM
1import 'whatwg-fetch';
2import cookies from 'react-cookies'
3import query_string from 'query-string'
4import AlertActions from 'actions/Alert.js';
5import CurrentUserActions from 'actions/CurrentUser.js'
6
7const METHODS = [
8 ['GET', 'orders'],
9 ['DELETE', 'orders/:id', 'delete_order'],
10 ['PUT', 'orders/:id', 'update_order'],
11 ['GET', 'orders/:id', 'get_order'],
12 ['POST', 'orders', 'create_order'],
13 ['POST', 'orders/bulk', 'create_order_bulk'],
14 ['POST', 'orders/delete_all', 'delete_all_orders'],
15
16 ['GET', 'products/:id/market_info', 'product_market_info'],
17 ['GET', 'products/:id/trade_stats', 'product_trade_stats'],
18 ['GET', 'products/:id', 'get_product'],
19 ['GET', 'products'],
20 ['GET', 'products/recently_viewed', 'recently_viewed_products'],
21 ['GET', 'products/suggestions', 'product_suggestions'],
22 ['GET', 'products/:id/average_price_stats', 'average_price_stats'],
23
24 ['GET', 'wallet_transactions'],
25 ['POST', 'wallet_transactions/add_funds', 'add_funds'],
26
27 ['GET', 'current_user'],
28 ['PUT', 'current_user', 'update_current_user'],
29 ['POST', 'current_user/logout', 'current_user_logout'],
30 ['GET', 'current_user/ensure_permissions', 'ensure_permissions'],
31 ['POST', 'current_user/close_sessions', 'current_user_close_sessions'],
32 ['POST', 'current_user/confirm_code', 'current_user_confirm_code'],
33 ['POST', 'current_user/change_email', 'current_user_change_email'],
34 ['POST', 'current_user/change_phone', 'current_user_change_phone'],
35 ['POST', 'current_user/change_phone_start', 'current_user_change_phone_start'],
36 ['POST', 'current_user/change_phone_check_code', 'current_user_change_phone_check_code'],
37 ['POST', 'current_user/verify_phone', 'current_user_verify_phone'],
38 ['POST', 'current_user/resend_code', 'current_user_resend_code'],
39 ['POST', 'current_user/get_paywin_url', 'current_user_paywin_url'],
40 ['POST', 'current_user/send_email_confirmation', 'current_user_send_email_confirmation'],
41 ['POST', 'current_user/confirm_email', 'current_user_confirm_email'],
42 ['POST', 'current_user/invalidate_token', 'current_user_invalidate_token'],
43 ['POST', 'current_user/close_item_warning', 'current_user_close_item_warning'],
44 ['GET', 'region_is_allowed', 'region_is_allowed'],
45 ['GET', 'geo_info', 'geo_info'],
46
47 ['GET', 'current_user/get_binds', 'current_user_get_binds'],
48 ['POST', 'current_user/bind_create', 'current_user_bind_create'],
49 ['POST', 'current_user/bind_delete', 'current_user_bind_delete'],
50 ['POST', 'current_user/bind_game', 'current_user_bind_game'],
51 ['POST', 'current_user/withdraw', 'current_user_withdraw'],
52 ['POST', 'current_user/identify', 'current_user_identify'],
53 ['POST', 'current_user/check_namescan', 'current_user_check_namescan'],
54 ['POST', 'current_user/check_documents', 'current_user_check_documents'],
55 ['POST', 'current_user/offer_accept', 'current_user_accept_offer'],
56 ['POST', 'current_user/offer_decline', 'current_user_decline_offer'],
57 ['GET', 'current_user/namescan_status', 'current_user_namescan_status'],
58 ['POST', 'current_user/transfer_accept', 'current_user_transfer_accept'],
59
60 ['GET', 'current_language', 'current_user_get_language'],
61 ['POST', 'current_language', 'current_user_change_language'],
62
63 ['GET', 'users/:id', 'user'],
64 ['GET', 'users/:id/feedback', 'user_feedback'],
65 ['GET', 'users/:id/my_feedback', 'my_feedback'],
66 ['POST', 'users/:id/my_feedback', 'post_my_feedback'],
67 ['POST', 'users/notifications_settings', 'notifications_settings_update'],
68
69 ['GET', 'notifications', 'get_notifications'],
70 ['POST', 'notifications/:id/close_notification', 'close_notification'],
71 ['POST', 'notifications/close_notification_all', 'close_notification_all'],
72 ['GET', 'notifications/ws_settings', 'notif_ws_settings'],
73
74 ['GET', 'messages/:id/get_chat', 'get_chat'],
75 ['POST', 'messages/:id/send_chat_message', 'send_chat_message'],
76
77 ['GET', 'user_inventory'],
78 ['POST', 'user_inventory/:id/code', 'user_inventory_open_code'],
79 ['POST', 'user_inventory/:id/close_code', 'user_inventory_close_code'],
80 ['POST', 'user_inventory/:id/return_code', 'user_inventory_return_code'],
81 ['POST', 'user_inventory/:id/activate_code', 'user_inventory_activate_code'],
82 ['POST', 'user_inventory/:id/delete_item', 'user_inventory_delete_item'],
83 ['GET', 'steam_inventory'],
84
85 ['GET', 'order_transactions'],
86 ['GET', 'order_transactions/:id', 'get_order_transaction'],
87 ['GET', 'order_transactions/:id/check', 'check_transaction'],
88
89 ['GET', 'gifts/:id', 'get_gift'],
90 ['POST', 'gifts/:id/activate', 'activate_gift'],
91
92 ['GET', 'user_tokens'],
93 ['POST', 'user_tokens/:id/deactivate', 'deactivate_token'],
94
95 ['GET', 'platforms'],
96 ['GET', 'games'],
97 ['GET', 'games/:id', 'get_game'],
98 ['POST', 'instant_buy'],
99 ['POST', 'add_steam_item'],
100 ['POST', 'add_steam_product'],
101 ['POST', 'retrieve_steam_item'],
102 ['GET', 'steam_trade_offer/:id'],
103 ['POST', 'add_key_product'],
104 ['POST', 'add_key_item'],
105 ['POST', 'generate_token'],
106 ['GET', 'market_stats'],
107 ['GET', 'face'],
108 ['GET', 'face/promos', 'promos'],
109 ['POST', 'subscribe'],
110 ['GET', 'commissions'],
111 ['GET', 'settings'],
112 ['POST', 'send_feedback'],
113 ['GET', 'partners'],
114 ['GET', 'cauri_status'],
115 ['POST', 'cauri_send'],
116 ['GET', 'withdraw_games'],
117
118 ['GET', 'interviews', 'get_active_interview'],
119 ['GET', 'interviews/available_dates', 'available_dates'],
120 ['POST', 'interviews', 'create_interview'],
121 ['POST', 'interviews/cancel', 'cancel_interview'],
122
123 ['GET', 'landing/stats', 'gamers_landing_stats'],
124 ['GET', 'landing/comments', 'gamers_landing_comments'],
125 ['POST', 'landing/gamers/feedback', 'gamers_landing_request'],
126
127 ['GET', 'withdraw_game_invoices/:id', 'withdraw_game_invoice'],
128
129 ['GET', 'landing/stats', 'get_developers_stats'],
130 ['POST', 'landing/partners/feedback', 'send_developers_feedback'],
131
132 ['GET', 'faq', 'get_faq_questions'],
133 ['GET', 'docs/partner_api', 'get_partner_api_content'],
134];
135
136const BASE_PATH = '/api/';
137
138function isEmpty(v) {
139 return v === null || v === undefined
140}
141
142const Backend = {
143
144 request(method, path, params, opts) {
145 let disableAlarm = opts.disableAlarm;
146 delete opts.disableAlarm;
147 let codesCatched = new Set(opts.codesCatched);
148 delete opts.codesCatched;
149
150 opts = Object.assign({
151 method: method,
152 credentials: 'same-origin',
153 }, opts);
154
155 if (!/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)) {
156 opts = Object.assign({
157 headers: {
158 "X-CSRFToken": cookies.load('csrftoken'),
159 }
160 }, opts);
161 }
162
163 path = path.replace(new RegExp("/(?::([^/]+))", "g"), function (m, key) {
164 return '/' + params[key];
165 });
166 let url = BASE_PATH + path + '/';
167 let enc = encodeURIComponent;
168
169 if (method.toUpperCase() === 'GET') {
170 params = Object.assign({
171 format: 'json'
172 }, params);
173 let qs = query_string.parse(window.location.search)
174 if (qs.lang) {
175 params.lang = qs.lang
176 }
177 let pairs = [];
178 for (let [k, v] of Object.entries(params)) {
179 if (Array.isArray(v)) {
180 for (let v2 of v) {
181 if (!isEmpty(v2)) {
182 pairs.push(enc(k) + '=' + enc(v2))
183 }
184 }
185 } else {
186 if (!isEmpty(v)) {
187 pairs.push(enc(k) + '=' + enc(v))
188 }
189 }
190 }
191 params = pairs.join('&');
192 url = url + '?' + params
193 } else {
194 let form = new FormData();
195 for (let [k, v] of Object.entries(params)) {
196 if (Array.isArray(v)) {
197 for (let v2 of v) {
198 if (!isEmpty(v2)) {
199 form.append(k, v2)
200 }
201 }
202 } else {
203 if (!isEmpty(v)) {
204 form.append(k, v)
205 }
206 }
207 }
208 opts.body = form;
209 }
210
211 return fetch(url, opts)
212 .then(resp => {
213 return new Promise(function (resolve, reject) {
214 if (resp.ok) {
215 if (resp.status == 204) {
216 resolve(null)
217 } else {
218 resolve(resp.json())
219 }
220 } else {
221 resp.text().then(text => {
222 let details
223 try {
224 details = JSON.parse(text)
225 } catch (err) {
226 if (resp.status == 502 || resp.status == 503 || resp.status == 504) {
227 details = {
228 code: 'Unavailable',
229 detail: text,
230 }
231 } else {
232 details = {
233 code: 'InternalError',
234 detail: text,
235 }
236 }
237 }
238
239 if (details && details.code && details.code === 'YouShallNotPass') {
240 CurrentUserActions.banned();
241 }
242
243 if (!disableAlarm && !codesCatched.has(details.code)) {
244 AlertActions.add(details)
245 } else {
246 reject(details)
247 }
248 })
249 }
250 })
251 }
252 )
253 },
254
255 reraise(err) {
256 AlertActions.add(err)
257 }
258};
259
260METHODS.forEach(([method, path, name]) => {
261 if (!name) {
262 name = path.replace(new RegExp("/(:[^/]+/?)?", "g"), '_').replace(new RegExp('_$'), '');
263 }
264 Backend[name] = (params, opts) => Backend.request(method, path, params || {}, opts || {})
265});
266
267export default Backend