// requestSend
async function requestSend(data) {
// errorMsg
var errorMsg = {result: false};
// errorData
if (!data && !data.paramData) {
return errorMsg;
};
var data = data.paramData;
// request options
let requestOptions = {
url: data.url,
method: data.method,
headers: {
'Authorization': 'OAuth '+global.config.settings.YandexMetrikaKey,
'Content-Type': 'multipart/form-data',
},
};
async function yandexResponse(data) {
return new Promise(resolve => {
request(data, (err, res, responseBody) => {
console.log(err)
console.log(res)
console.log(responseBody)
if (!err && res.statusCode == 200) {
resolve(responseBody)
}
})
})
}
const requestResponse = await yandexResponse(requestOptions);
console.log(requestResponse)
if (requestResponse && requestResponse !== 'undefined') {
return {result: true, resultData: requestResponse};
}
return errorMsg;
}
{
url: 'https://api-metrika.yandex.net/management/v1/counter/НомерСчетчика/offline_conversions/upload?client_id_type=CLIENT_ID',
file: 'полный_путь_к_файлу_csv.csv',
fileName: 'название_файла.csv',
method: 'POST'
}
{"errors":[{"error_type":"invalid_uploading","message":"Request parameter not found","location":"file"}],"code":400,"message":"Request parameter not found"}
var createCsvWriter = require('csv-writer').createObjectCsvWriter
// настройки csv writer
var csvWriter = createCsvWriter({
path: tagetFile,
header: [
{id: 'ClientId', title: 'ClientId'},
{id: 'Target', title: 'Target'},
{id: 'DateTime', title: 'DateTime'},
{id: 'Price', title: 'Price'},
{id: 'Currency', title: 'Currency'},
]
});
// данные для файла
var yandexData = [
{
ClientId: data.yaId,
Target: data.yaTarget,
DateTime: targetDate,
Price: data.billSum,
Currency: 'RUB'
},
];
// записываем файл
csvWriter.writeRecords(yandexData)
// ВОТ тут как раз нужен delay в 1-3 сек.
await delay(3500);
var request = require('request');
var fs = require('fs');
var options = {
'method': 'POST',
'url': 'https://api-metrika.yandex.net/management/v1/counter/99999999/offline_conversions/upload?client_id_type=CLIENT_ID',
'headers': {
'Authorization': 'Bearer AQAAAAAF0DAAC3Q40j9_TDsnhNZ4sv6CusA',
'Cookie': 'JSESSIONID=node0855nv0504w0qvdvb3z0zqfat2243678.node0'
},
formData: {
'file': {
'value': fs.createReadStream('/patch/to/file/PTgt-zouHsE.csv'),
'options': {
'filename': 'PTgt-zouHsE.csv',
'contentType': null
}
}
}
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
function delay(timeout) {
return new Promise((resolve) => {
setTimeout(resolve, timeout);
});
}