-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiInvoker.js
More file actions
475 lines (420 loc) · 16.5 KB
/
Copy pathApiInvoker.js
File metadata and controls
475 lines (420 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
import { GetAuth, GetPermitedURLs, GetPermitedURLsPromisified, IsSuperUser } from "./security";
import { RegenerateRefreshAndAccessToken, IsAuthenticated, IsRefreshTokenValid } from "../util/security"
import { RemoveFromArray } from "./util";
import { UpdateClusterDataForNavigation, UpdateUserDataForNavigation } from "./GlobalVars";
import { createBrowserHistory } from 'history';
function validateTokenAndInvokePostStreamAPI(url, data, onEvent, onDone, onError, opts = {}) {
const call = () => invokePostStreamRequest(url, data, onEvent, onDone, onError, opts);
if (!IsAuthenticated()) {
if (IsRefreshTokenValid()) {
RegenerateRefreshAndAccessToken(call, onError);
} else {
onError("Session expired. Login again");
}
} else {
call();
}
}
export function PostDataStream(url, data, onEvent, onDone, onError, doNotValidateToken, opts = {}) {
if (doNotValidateToken) {
return invokePostStreamRequest(url, data, onEvent, onDone, onError, opts);
}
return validateTokenAndInvokePostStreamAPI(url, data, onEvent, onDone, onError, opts);
}
async function invokePostStreamRequest(url, data, onEvent, onDone, onError, opts = {}) {
const auth = GetAuth();
const controller = opts.controller || new AbortController();
try {
const res = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "text/event-stream",
"Authorization": auth ? `Bearer ${auth.access}` : null
},
body: JSON.stringify(data),
signal: controller.signal
});
if (!res.ok) {
const errText = await res.text().catch(() => "");
onError(errText || `HTTP error ${res.status}`);
return { abort: () => controller.abort() };
}
const reader = res.body.getReader();
const decoder = new TextDecoder("utf-8");
// Important: SSE frames can be split across chunks, so keep a buffer.
let buffer = "";
let aggregated = "";
while (true) {
const { value, done } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
// SSE events are separated by a blank line
const parts = buffer.split("\n\n");
buffer = parts.pop() || ""; // remainder stays in buffer
for (const evt of parts) {
const lines = evt.split("\n");
for (const line of lines) {
if (line.startsWith("data:")) {
const dataPart = line.slice(5).trim();
if (!dataPart) continue;
if (dataPart === "[DONE]") continue;
// If your backend sends json in data:, parse here. Otherwise treat as token/text.
// const payload = JSON.parse(dataPart);
aggregated += dataPart;
onEvent({ delta: dataPart, aggregated });
}
}
}
}
onDone({ aggregated });
return { abort: () => controller.abort() };
} catch (e) {
if (e?.name === "AbortError") return { abort: () => controller.abort() };
onError(e?.message || "Stream failed");
return { abort: () => controller.abort() };
}
}
function InvokeApi(requestInfo, callBackFunction, errorCallBackFunction, doNotValidateToken, parseText) {
console.log(requestInfo, "requestInfo___")
if (doNotValidateToken) {
invokeGetRequest(requestInfo, callBackFunction, errorCallBackFunction, parseText);
} else {
validateTokenAndInvokeGetAPI(requestInfo, callBackFunction, errorCallBackFunction, parseText);
}
}
function validateTokenAndInvokeGetAPI(requestInfo, callBackFunction, errorCallBackFunction, parseText) {
const callBackFunctionLocal = () => {
invokeGetRequest(requestInfo, callBackFunction, errorCallBackFunction, parseText)
}
if (!IsAuthenticated()) {
if (IsRefreshTokenValid()) {
RegenerateRefreshAndAccessToken(callBackFunctionLocal, errorCallBackFunction);
} else {
errorCallBackFunction("Session expired. Login again");
}
} else {
invokeGetRequest(requestInfo, callBackFunction, errorCallBackFunction, parseText)
}
}
function validateTokenAndInvokePostAPI(url, data, callBackFunction, errorCallBackFunction, doNotFetchPermission) {
console.log("responsefjsdjfkjskafjs", doNotFetchPermission)
const callBackFunctionLocal = () => {
invokePostRequest(url, data, callBackFunction, errorCallBackFunction);
}
if (!IsAuthenticated()) {
if (IsRefreshTokenValid()) {
RegenerateRefreshAndAccessToken(callBackFunctionLocal, errorCallBackFunction);
} else {
errorCallBackFunction("Session expired. Login again");
}
} else {
console.log("responsefjsdjfkjskafjs", doNotFetchPermission)
invokePostRequest(url, data, callBackFunction, errorCallBackFunction, doNotFetchPermission);
}
}
function validateTokenAndInvokeDeleteAPI(url, data, callBackFunction, errorCallBackFunction) {
const callBackFunctionLocal = () => {
invokeDeleteRequest(url, data, callBackFunction, errorCallBackFunction);
}
if (!IsAuthenticated()) {
if (IsRefreshTokenValid()) {
RegenerateRefreshAndAccessToken(callBackFunctionLocal, errorCallBackFunction);
} else {
errorCallBackFunction("Session expired. Login again");
}
} else {
invokeDeleteRequest(url, data, callBackFunction, errorCallBackFunction)
}
}
export const PostData = (url, data, callBackFunction, errorCallBackFunction, doNotValidateToken, doNotFetchPermission = true) => {
console.log("responsefjsdjfkjskafjs", doNotFetchPermission, doNotValidateToken)
if (doNotValidateToken) {
invokePostRequest(url, data, callBackFunction, errorCallBackFunction, true);
} else {
validateTokenAndInvokePostAPI(url, data, callBackFunction, errorCallBackFunction, doNotFetchPermission);
}
}
export const DeleteData = (url, data, callBackFunction, errorCallBackFunction, doNotValidateToken) => {
if (doNotValidateToken) {
invokeDeleteRequest(url, data, callBackFunction, errorCallBackFunction, true);
} else {
validateTokenAndInvokeDeleteAPI(url, data, callBackFunction, errorCallBackFunction);
}
}
function invokeGetRequest(requestInfo, callBackFunction, errorCallBackFunction, parseText) {
var status = null;
var bearerToken = "Bearer " + GetAuth().access;
var fetchOptions = {
method: requestInfo.httpMethod,
headers: { ...requestInfo.httpHeaders, "Authorization": bearerToken },
};
console.log(requestInfo, 'requestInfo_212332232')
fetch(requestInfo.endPoint, fetchOptions)
.then(response => {
// console.log(response.text(),'sbvjshbvhjsbvhdbhfd');
console.log(parseText, 'pt_001')
const contentType = response.headers.get("content-type")
console.log(contentType, 'content_type_030243');
if (contentType === 'application/zip' || contentType === 'application/zip; charset=UTF-8') {
return response.blob();
}
status = response.status;
var response_json = parseText ? response.text() : response.json();
return response_json;
})
.then((data) => {
console.log(data, status, ApiIsUnSubsribed(requestInfo.endPoint), "sbvjshbvhjsbvhdbhfd")
if (!ApiIsUnSubsribed(requestInfo.endPoint)) {
console.log(data, "sbvjshbvhjsbvhdbhfd", status)
if (data.type === 'application/zip') {
callBackFunction(data);
}
else if (status === 200) {
callBackFunction(data);
}
else if (status == 403) {
const error_status = resolveErrorStatus(status, data);
errorCallBackFunction({ error: error_status, status: status }, status);
}
else {
var error_status = resolveErrorStatus(status, data);
errorCallBackFunction(error_status, status);
}
}
})
.catch((exception) => {
console.log(exception, "fdsafjkasjkfask");
handleException();
if (typeof errorCallBackFunction === 'function') {
errorCallBackFunction(exception);
}
});
}
function resolveErrorStatus(status, data) {
let genericMessage = "Server is currently unavailable. Please try again later."
switch (status) {
case 401:
return data.detail ?? genericMessage;
case 500:
return data.detail ?? genericMessage;
case 501:
return data.error ?? genericMessage;
case 404:
return "404: The requested resource was not found on this server.";
case 405:
return data.detail ?? genericMessage;
case 403:
return data.detail ?? "You do not have permission to perform this action.";
case 400:
if (data.non_field_errors) {
return data.non_field_errors[0]
}
return data ?? genericMessage;
default:
return data ?? genericMessage;
}
}
// function invokePostRequest(url, data, callBackFunction, errorCallBackFunction, doNotRefreshPermission) {
// console.log(url, data, callBackFunction, errorCallBackFunction, doNotRefreshPermission,'post_api_00psd')
// var status = null;
// var auth = GetAuth();
// var fetchOptions = {
// method: "POST",
// headers: { "Content-Type": "application/json", "Authorization": auth ? "Bearer " + GetAuth().access : null },
// body: JSON.stringify(data)
// };
// console.log(url, fetchOptions,"fdskfdasa")
// fetch(url, fetchOptions)
// .then(response => {
// status = response.status;
// var response_json = response.json();
// console.log(status, response_json,"fkldsafs, datfklsdkla")
// return response_json;
// })
// .then((data) => {
// if (!ApiIsUnSubsribed(url)) {
// if ((status == 200) || (status == 201) || (status == 204)) {
// if (!doNotRefreshPermission) {
// GetPermitedURLsPromisified()
// .then(() => callBackFunction(data)) // only call back when permissions successfully fetched
// .catch((error) => { errorCallBackFunction(error) })
// if (IsSuperUser()) {
// UpdateClusterDataForNavigation();
// } else {
// UpdateUserDataForNavigation();
// }
// }
// else {
// console.log(status, data,"fkldsafs, datfklsdkla")
// callBackFunction(data);
// }
// }
// else if (status == 403) {
// const error_status = resolveErrorStatus(status, data);
// errorCallBackFunction({ error: error_status, status: status });
// }else if (status == 404) {
// console.log(status, data,"fkldsafs, datfklsdkla")
// const error_status = resolveErrorStatus(status, data);
// errorCallBackFunction({ error: error_status, status: status });
// }
// else {
// console.log(status, data,"fkldsafs, datfklsdkla")
// var error_status = resolveErrorStatus(status, data);
// handleErrorResponse();
// errorCallBackFunction(error_status);
// }
// }
// })
// .catch((exception) => {
// console.log(exception,"exceptionexceptionexception")
// handleException();
// // errorCallBackFunction(exception,true);
// });
// }
const pendingPostRequests = new Set();
function invokePostRequest(url, data, callBackFunction, errorCallBackFunction, doNotRefreshPermission) {
const requestSignature = `${url}|${JSON.stringify(data)}`;
if (pendingPostRequests.has(requestSignature)) {
console.warn("Duplicate request prevented:", requestSignature);
return;
}
pendingPostRequests.add(requestSignature);
console.log("responsefjsdjfkjskafjs", doNotRefreshPermission)
console.log(url, data, callBackFunction, errorCallBackFunction, doNotRefreshPermission, 'post_api_00psd');
let status = null;
const auth = GetAuth();
const fetchOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": auth ? "Bearer " + auth.access : null
},
body: JSON.stringify(data)
};
console.log(url, fetchOptions, "fdskfdasa");
fetch(url, fetchOptions)
.then(async (response) => {
status = response.status;
let responseData;
try {
// Safely parse JSON only if content-type is JSON
const contentType = response.headers.get("Content-Type");
if (contentType && contentType.includes("application/json")) {
responseData = await response.json();
} else {
responseData = await response.text(); // fallback to text
}
} catch (err) {
console.error("JSON parse error:", err);
responseData = { error: "Invalid response format" };
}
if (!ApiIsUnSubsribed(url)) {
if ([200, 201, 204].includes(status)) {
if (!doNotRefreshPermission) {
GetPermitedURLsPromisified()
.then(() => callBackFunction(responseData))
.catch(error => errorCallBackFunction(error));
if (IsSuperUser()) {
UpdateClusterDataForNavigation();
} else {
UpdateUserDataForNavigation();
}
} else {
// Wait for next tick so any sync UI updates complete first, preventing double clicks when UI states change across React renders
setTimeout(() => callBackFunction(responseData), 0);
}
} else if ([403, 404].includes(status)) {
console.log(status, responseData, "fkjjdkfjsdfjsdajfsfkjsafs")
const error_status = resolveErrorStatus(status, responseData);
errorCallBackFunction(error_status);
} else {
const error_status = resolveErrorStatus(status, responseData);
handleErrorResponse();
errorCallBackFunction(error_status);
}
}
})
.catch((exception) => {
console.error("Fetch exception:", exception);
handleException();
// Optional: errorCallBackFunction(exception, true);
}).finally(() => {
// Always remove from pending requests when request finishes (success or fail)
pendingPostRequests.delete(requestSignature);
});
}
function invokeDeleteRequest(url, data, callBackFunction, errorCallBackFunction, doNotRefreshPermission) {
var status = null;
var auth = GetAuth();
var fetchOptions = {
method: "delete",
headers: { "Content-Type": "application/json", "Authorization": auth ? "Bearer " + GetAuth().access : null },
body: JSON.stringify(data)
};
fetch(url, fetchOptions)
.then(response => {
status = response.status;
var response_json = response.json();
return response_json;
})
.then((data) => {
if (!ApiIsUnSubsribed(url)) {
if (status == 200) {
if (!doNotRefreshPermission) {
// GetPermitedURLs();
if (IsSuperUser()) {
UpdateClusterDataForNavigation();
} else {
UpdateUserDataForNavigation();
}
}
callBackFunction(data);
}
else if (status == 403) {
const error_status = resolveErrorStatus(status, data);
errorCallBackFunction({ error: error_status, status: status });
}
else {
var error_status = resolveErrorStatus(status, data);
handleErrorResponse();
errorCallBackFunction(error_status);
}
}
}).catch((exception) => {
console.log(exception, "fdsafjkasjkfask");
handleException();
// errorCallBackFunction(exception,true);
});
}
export function UnsubscribeToApi(url) {
var unsubscribed_api_ist = (window.localStorage.getItem("unsubscribed_api_ist"));
unsubscribed_api_ist = unsubscribed_api_ist ? unsubscribed_api_ist.split(",") : [];
if (!unsubscribed_api_ist.includes(url)) {
unsubscribed_api_ist.push(url);
}
window.localStorage.setItem("unsubscribed_api_ist", unsubscribed_api_ist.toString());
}
export function SubscribeToApi(url) {
var unsubscribed_api_ist = (window.localStorage.getItem("unsubscribed_api_ist"));
unsubscribed_api_ist = unsubscribed_api_ist ? unsubscribed_api_ist.split(",") : [];
if (unsubscribed_api_ist) {
RemoveFromArray(unsubscribed_api_ist, url);
}
window.localStorage.setItem("unsubscribed_api_ist", unsubscribed_api_ist.toString());
}
export function ApiIsUnSubsribed(url) {
var unsubscribed_api_ist = window.localStorage.getItem("unsubscribed_api_ist");
unsubscribed_api_ist = unsubscribed_api_ist ? unsubscribed_api_ist.split(",") : [];
return unsubscribed_api_ist.includes(url);
}
var handleErrorResponse = () => { return null };
var handleException = () => { return null };
var handlePermissionNotFoundResponse = () => {
const history = createBrowserHistory();
history.go("/permission-not-found")
}
export function SetHandleError(handle_error_callback) {
handleErrorResponse = handle_error_callback;
}
export default InvokeApi;