From f8084b59d9f320c7181c856b9c070052dde4c623 Mon Sep 17 00:00:00 2001 From: Rick Companje Date: Tue, 11 Jul 2017 12:33:28 +0200 Subject: [PATCH] added fetch and fetchHeader in ServerAPI --- js/api/ServerAPI.js | 50 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/js/api/ServerAPI.js b/js/api/ServerAPI.js index 00ac9dd..1e0edcf 100644 --- a/js/api/ServerAPI.js +++ b/js/api/ServerAPI.js @@ -27,12 +27,56 @@ function ServerAPI() { if (completeHandler) completeHandler(response); } }).fail(function(response) { - console.log("fail"); - if (failedHandler) failedHandler(); + if (failedHandler) failedHandler(response.responseJSON); }); }; - // '/fetch/'+uid.value+'/'+line.value; + this.fetch = function(uuid, chunk, completeHandler, failedHandler) { + console.log("ServerAPI:fetch",uuid,chunk); + if (!uuid) { + console.log("Server.fetch failed: no uuid"); + if (failedHandler) failedHandler(); + return; + } + + if (!chunk) { + chunk = 0; + } + + $.ajax({ + url: _apiURL + "/fetch/" + uuid + "/" + chunk, + type: "GET", + dataType: 'text', //no json but text + timeout: _timeoutTime, + cache: false, + success: function(response) { + if (completeHandler) completeHandler(response); + } + }).fail(function(response) { + console.log("ServerAPI:fetch fail response:",response); + if (failedHandler) failedHandler(response.responseJSON); + }); + }; + + this.fetchHeader = function(uuid,completeHandler,failedHandler) { + this.fetch(uuid,0,function(successData) { + console.log("_serverAPI fetchHeader"); + if (successData) { + var items = successData.split("\n"); + try { + var headerText = items[0].substr(1); + var headerJSON = JSON.parse(headerText); + // console.log("fetchHeader",headerJSON) + if (completeHandler) completeHandler(headerJSON); + } catch (e) { + console.log("fetchHeader failed to parse JSON",e); + if (failedHandler) failedHandler(); + } + } + }, function(failData) { + console.log("_serverAPI fetchHeader failed",failData); + }); + }; }