const FormData = require('form-data');
const fs = require('fs');
const crypto = require('crypto');
const authUtils = require('./utils/auth-utils');
const serverConfig = {
protocol: 'https:',
hostname: 'translate.rozetta-api.io',
port: 443
};
const authConfig = {
accessKey: 'ACCESS_KEY',
secretKey: 'SECRET_KEY',
nonce: Date.now().toString()
};
const speechConfig = {
lang: 'ja',
audioFile: 'speech.wav',
contractId: 'your contractId'
};
const sendRequest = (serverConfig, authConfig, speechConfig) => {
const form = new FormData();
form.append('sourceLang', speechConfig.lang);
form.append('audioFile', fs.createReadStream(speechConfig.audioFile));
form.append('contractId', speechConfig.contractId);
const path = '/api/v1/translate/stt';
const signature = authUtils.generateSignature(
path,
authConfig.secretKey,
authConfig.nonce
);
return new Promise((resolve, reject) => {
form.submit(
{
protocol: serverConfig.protocol,
host: serverConfig.hostname,
port: serverConfig.port,
path,
headers: {
accessKey: authConfig.accessKey,
signature,
nonce: authConfig.nonce
}
},
(error, response) => {
if (error !== null) {
reject(error);
return;
}
response.setEncoding('utf8');
let data = '';
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
resolve(data);
});
}
);
});
};
const main = async () => {
try {
const response = await sendRequest(
serverConfig,
authConfig,
speechConfig
);
console.log('Server response:');
console.log(response);
} catch (error) {
console.error(error);
}
};
main();