Skip to content

Commit

Permalink
API Fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
ivynya committed Jul 19, 2020
1 parent 195d304 commit 46d9872
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions angular/src/app/core/services/api/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,36 @@ export class APIService {

// long text -> callback(summary text)
getNLP(input: string, callback: (output: string) => void) {
var xhr = new XMLHttpRequest();

xhr.onload = () => {
// process response
if (xhr.status == 200)
callback(JSON.parse(xhr.response).output);
else
console.error('Error!');
};

xhr.open("POST", "https://api.kamiapp.ml/nlp", true);
xhr.setRequestHeader("Content-type", "application/json");
xhr.send(`{"input": "${input}"}`);
fetch("https://api.kamiapp.ml/nlp", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
mode: "cors",
body: JSON.stringify({
"input": input,
})
})
.then(response => response.text())
.then(response => {
callback(JSON.parse(response).output);
})
.catch(err => console.error(err));
}

// image bytes -> callback(long text)
async getOCR(input: ArrayBuffer): Promise<string> {
return new Promise(function (resolve, reject) {
let xhr = new XMLHttpRequest();
xhr.open("POST", "https://api.kamiapp.ml/ocr", true);

xhr.onload = () => {
if (this.status === 200) {
resolve(JSON.parse(xhr.response).output);
} else {
reject({
status: this.status,
statusText: `Unexpected: ${xhr.statusText}`
});
}
};

xhr.send(`${input}`);
fetch("https://api.kamiapp.ml/ocr", {
method: "POST",
mode: "cors",
body: input
})
.then(response => response.text())
.then(response => {
resolve(JSON.parse(response).output);
})
.catch(err => reject(err));
});
}
}

0 comments on commit 46d9872

Please sign in to comment.