apr-api
Version:
Performing queries on Business Registers Agency of Serbia data. Work in progress, currently searching only by registration number.
70 lines (63 loc) • 2.4 kB
JavaScript
require("babel-polyfill");
import qs from 'querystring';
import axios from 'axios';
import cheerio from 'cheerio';
class HttpClient {
constructor() {
this.client = axios.create({
baseURL: "http://pretraga2.apr.gov.rs/ObjedinjenePretrage/Search",
});
this.token = '';
this.cookie = '';
}
async grabToken() {
const response = await this.client.get('/Search');
const cookies = [];
response.headers["set-cookie"].forEach((cookieData) => {
cookies.push(cookieData.substring(0, cookieData.indexOf(";")));
});
this.cookie = cookies.join("; ");
const $ = cheerio.load(response.data);
const form = $("form").serializeArray();
this.token = form.find((item) => item.name === "__RequestVerificationToken").value;
if (this.token === '') throw new AssertionError("Token not found");
return this.token;
}
tidyString(input) {
return input.replace('\n', '').trim();
}
async searchByRegistrationNumber(regNo) {
if (this.token === '') await this.grabToken();
try {
const response = await this.client.request({
url: '/SearchResult',
method: 'post',
data: qs.stringify({
"__RequestVerificationToken": this.token,
"rdbtnSelectInputType": "mbr",
"SearchByRegistryCodeString": regNo,
"X-Requested-With": "XMLHttpRequest"
}),
headers: {
Cookie: "SERVERID=Server1; " + this.cookie,
'Content-type': 'application/x-www-form-urlencoded'
}
});
const $ = cheerio.load(response.data);
const row = $("table.ContentTable").find("tr")[1];
const td = $(row).find("td");
const data = {
type: this.tidyString($(td[0]).text()),
regNo: this.tidyString($(td[1]).text()),
businessName: this.tidyString($(td[2]).text()),
status: this.tidyString($(td[3]).text()),
detailsLink: $(td[4]).find("a").attr("href")
};
return data;
} catch (err) {
console.log(err);
return null;
}
}
}
export default new HttpClient();