In ROmania ANAF is providing a WebAPI that allows access to some of the information that any enterprise should provide. The API is described at https://webservicesp.anaf.ro/PlatitorTvaRest/api/v3/,
I have made a C# console and a node,js script.
The differences:
1.it is easiear in node to make an http request.
2. For C# – I think in components. For node – I think in application.
Without further ado,here is the code
using RestSharp;
using System;
namespace InfoAnafWS
{
class Program
{
static void Main(string[] args)
{
if(args.Length == 0)
{
Console.WriteLine("lipseste CUI ");
return;
}
var client = new RestClient("https://webservicesp.anaf.ro");
var request = new RestRequest("PlatitorTvaRest/api/v3/ws/tva",Method.POST);
request.RequestFormat = DataFormat.Json;
string date = DateTime.Now.AddDays(-5).ToString("yyyy-MM-dd");
string req = "";
foreach (var item in args)
{
req += string.Format(@"{{'cui': {1},'data':'{0}'}}",date,item);
}
req = req.Replace("'","\"");
req = "[" + req + "]";
request.AddParameter("application/json",req,ParameterType.RequestBody);
var data = client.Execute(request);
Console.WriteLine(data.Content);
}
}
}
const start = async () => {
let request = require('request');
const d = require('delay');
await d(1000);
var fs = require('fs');
var array = fs.readFileSync('CUI.txt').toString().split("\r\n");
var start=0;
while(start<array.length){
var nr = Math.floor(Math.random() * Math.floor(495))+1;
var jsonData=[];
console.log('start at ' + start+ ' for nr ' + nr);
var i=0;
while(i<nr && i+start<array.length){
jsonData.push({"cui": array[i+start],"data":"2018-03-26"});
i++;
}
var finish=false;
console.log('array :'+ jsonData[0].cui + '-' + jsonData[nr-1].cui);
request.post(
'https://webservicesp.anaf.ro/PlatitorTvaRest/api/v3/ws/tva',
{ json: jsonData },
function (error,response,body) {
finish=true;
if (!error && response.statusCode == 200) {
console.log(body);
fs.writeFile(`text${start}_${nr}.json`,JSON.stringify( body ),function (err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!" + `text${start}_${nr}.json`);
});
}
});
start = start+nr;
while(!finish){
console.log('waiting');
await d(13000);
}
}
console.log('finish')
return;
}
start();
Leave a Reply