diff --git a/js/api.js b/js/api.js index 63100b4..5d39e7c 100644 --- a/js/api.js +++ b/js/api.js @@ -1,9 +1,10 @@ const API_URL = "//xlit-api.ai4bharat.org"; const LANGS_API = API_URL + "/languages"; const LEARN_API = API_URL + "/learn"; -const TRANSLITERATE_API = https://api.dhruva.ai4bharat.org/services/inference/transliteration; // Use API_URL directly +const TRANSLITERATE_API = config.API_URL; async function getTransliterationSuggestions(lang, searchTerm) { + if (searchTerm == '.' || searchTerm == '..') { searchTerm = ' ' + searchTerm; } @@ -37,11 +38,10 @@ async function getTransliterationSuggestions(lang, searchTerm) { mode: 'cors', headers: new Headers({ 'Content-Type': 'application/json', - 'Authorization': `Bearer ${process.env.API_KEY}` + 'Authorization': config.API_KEY }) }) - .then(response => response.json()); - + .then(response => response.json()); return outputData["output"][0]; } @@ -74,10 +74,10 @@ async function getTransliterationForWholeText(inputLang, outputLang, text) { mode: 'cors', headers: new Headers({ 'Content-Type': 'application/json', - 'Authorization': `Bearer ${process.env.API_KEY}` + 'Authorization': config.API_KEY }) }) - .then(response => response.json()); + .then(response => response.json()); return outputData["output"][0]["target"]; } @@ -97,20 +97,17 @@ async function recordUserSelection(lang, input, output, id) { "input": input, "output": output, "topk_index": id - }; - - // Using fetch instead of XMLHttpRequest - await fetch(LEARN_API, { - method: 'post', - body: JSON.stringify(data), - mode: 'cors', - headers: new Headers({ - 'Content-Type': 'application/json' - }) - }) - .then(response => { - if (response.status !== 200) { - console.log("ERROR: Failed to recordUserSelection(). Status: " + response.status); + } + xmlhttp = new XMLHttpRequest(); + xmlhttp.open("POST", LEARN_API, true); + xmlhttp.withCredentials = true; + xmlhttp.onreadystatechange = function () { + if (xmlhttp.readyState == 4) { + if (xmlhttp.status != 200) { + console.log("ERROR: Failed to recordUserSelection(). Status: " + xmlhttp.status); } - }); + } + }; + xmlhttp.setRequestHeader("Content-type", "application/json"); + xmlhttp.send(JSON.stringify(data)); }