| 
									
										
										
										
											2017-11-28 20:52:38 -05:00
										 |  |  | const server = (function() { | 
					
						
							| 
									
										
										
										
											2017-11-29 23:30:35 -05:00
										 |  |  |     function getHeaders() { | 
					
						
							| 
									
										
										
										
											2017-11-30 19:58:00 -05:00
										 |  |  |         let protectedSessionId = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try { // this is because protected session might not be declared in some cases - like when it's included in migration page
 | 
					
						
							|  |  |  |             protectedSessionId = protected_session.getProtectedSessionId(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         catch(e) {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 23:30:35 -05:00
										 |  |  |         return { | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |             protected_session_id: protectedSessionId, | 
					
						
							|  |  |  |             source_id: glob.sourceId | 
					
						
							| 
									
										
										
										
											2017-11-29 23:30:35 -05:00
										 |  |  |         }; | 
					
						
							| 
									
										
										
										
											2017-11-28 20:52:38 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function get(url) { | 
					
						
							| 
									
										
										
										
											2017-11-28 22:01:02 -05:00
										 |  |  |         return await call('GET', url); | 
					
						
							| 
									
										
										
										
											2017-11-28 20:52:38 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function post(url, data) { | 
					
						
							| 
									
										
										
										
											2017-11-28 22:01:02 -05:00
										 |  |  |         return await call('POST', url, data); | 
					
						
							| 
									
										
										
										
											2017-11-28 20:52:38 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function put(url, data) { | 
					
						
							| 
									
										
										
										
											2017-11-28 22:01:02 -05:00
										 |  |  |         return await call('PUT', url, data); | 
					
						
							| 
									
										
										
										
											2017-11-28 20:52:38 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function remove(url) { | 
					
						
							| 
									
										
										
										
											2017-11-28 22:01:02 -05:00
										 |  |  |         return await call('DELETE', url); | 
					
						
							| 
									
										
										
										
											2017-11-28 20:52:38 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 22:01:02 -05:00
										 |  |  |     let i = 1; | 
					
						
							|  |  |  |     const reqResolves = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function call(method, url, data) { | 
					
						
							|  |  |  |         if (isElectron()) { | 
					
						
							|  |  |  |             const ipc = require('electron').ipcRenderer; | 
					
						
							|  |  |  |             const requestId = i++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return new Promise((resolve, reject) => { | 
					
						
							|  |  |  |                 reqResolves[requestId] = resolve; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 23:30:35 -05:00
										 |  |  |                 console.log("Request #" + requestId + " to " + method + " " + url); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 22:01:02 -05:00
										 |  |  |                 ipc.send('server-request', { | 
					
						
							|  |  |  |                     requestId: requestId, | 
					
						
							| 
									
										
										
										
											2017-11-29 23:30:35 -05:00
										 |  |  |                     headers: getHeaders(), | 
					
						
							| 
									
										
										
										
											2017-11-28 22:01:02 -05:00
										 |  |  |                     method: method, | 
					
						
							|  |  |  |                     url: "/" + baseApiUrl + url, | 
					
						
							|  |  |  |                     data: data | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             return await ajax(url, method, data); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (isElectron()) { | 
					
						
							|  |  |  |         const ipc = require('electron').ipcRenderer; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ipc.on('server-response', (event, arg) => { | 
					
						
							| 
									
										
										
										
											2017-11-29 23:30:35 -05:00
										 |  |  |             console.log("Response #" + arg.requestId + ": " + arg.statusCode); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 22:01:02 -05:00
										 |  |  |             reqResolves[arg.requestId](arg.body); | 
					
						
							| 
									
										
										
										
											2017-11-29 23:30:35 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |             delete reqResolves[arg.requestId]; | 
					
						
							| 
									
										
										
										
											2017-11-28 22:01:02 -05:00
										 |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function ajax(url, method, data) { | 
					
						
							| 
									
										
										
										
											2017-11-28 20:52:38 -05:00
										 |  |  |         const options = { | 
					
						
							|  |  |  |             url: baseApiUrl + url, | 
					
						
							| 
									
										
										
										
											2017-11-29 23:30:35 -05:00
										 |  |  |             type: method, | 
					
						
							|  |  |  |             headers: getHeaders() | 
					
						
							| 
									
										
										
										
											2017-11-28 20:52:38 -05:00
										 |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (data) { | 
					
						
							|  |  |  |             options.data = JSON.stringify(data); | 
					
						
							|  |  |  |             options.contentType = "application/json"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return await $.ajax(options).catch(e => { | 
					
						
							| 
									
										
										
										
											2017-11-29 22:03:03 -05:00
										 |  |  |             showError("Error when calling " + method + " " + url + ": " + e.status + " - " + e.statusText); | 
					
						
							| 
									
										
										
										
											2017-11-28 20:52:38 -05:00
										 |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         get, | 
					
						
							|  |  |  |         post, | 
					
						
							|  |  |  |         put, | 
					
						
							| 
									
										
										
										
											2017-11-29 23:30:35 -05:00
										 |  |  |         remove | 
					
						
							| 
									
										
										
										
											2017-11-28 20:52:38 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | })(); |