yandex-predictor
Version:
The API Client for Yandex Predictor
51 lines (46 loc) • 1.26 kB
JavaScript
const request = require("request");
module.exports = class Client {
constructor(key) {
this.apikey = key;
this.header = {
"User-Agent": "Super Agent/0.0.1",
"Content-Type": "application/x-www-form-urlencoded"
};
this.method = 'GET'
}
async getLangs(callback) {
this.urlQuery =
"https://predictor.yandex.net/api/v1/predict.json/getLangs?key=" +
this.apikey;
request({
url: this.urlQuery,
method: this.method,
headers: this.header
}, (error, response, body) => {
if (!error && response.statusCode == 200) {
let langObj = JSON.parse(body);
callback(langObj);
} else console.log(error);
});
}
async complete(q, callback, lang = "en", limit = 5) {
request({
url: "https://predictor.yandex.net/api/v1/predict.json/complete?key=" +
this.apikey +
"&q=" +
encodeURI(q) +
"&lang=" +
lang +
"&type=json" +
"&limit=" +
limit,
method: this.method,
headers: this.header
}, (error, response, body) => {
if (!error && response.statusCode == 200) {
let resObj = JSON.parse(body)
callback(resObj);
} else console.log(error);
});
}
};