Skip to content

Commit

Permalink
Update api.js
Browse files Browse the repository at this point in the history
  • Loading branch information
srihari07-web authored Nov 27, 2023
1 parent 4a33e3f commit 95ebc61
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions js/api.js
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down Expand Up @@ -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];
}

Expand Down Expand Up @@ -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"];
}

Expand All @@ -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));
}

0 comments on commit 95ebc61

Please sign in to comment.