const https = require('https');
const queryString = require('querystring');
const authUtils = require('./utils/auth-utils');
const serverConfig = {
protocol: 'https:',
hostname: 'translate.rozetta-api.io',
port: 443
};
const authConfig = {
accessKey: 'YOUR_ACCESS_KEY',
secretKey: 'YOUR_SECRET_KEY',
nonce: new Date().getTime().toString()
};
const listConfig = {
engine: 't4oo',
language: 'ja'
};
const getTextResult = async (serverConfig, authConfig, listConfig) => {
const queryParameters = queryString.stringify({
engine: listConfig.engine,
language: listConfig.language
})
const path = `/api/v1/field/list?${queryParameters}`;
const signature = authUtils.generateSignature(
path,
authConfig.secretKey,
authConfig.nonce
);
const requestOptions = {
protocol: serverConfig.protocol,
host: serverConfig.hostname,
port: serverConfig.port,
method: 'GET',
path,
headers: {
accessKey: authConfig.accessKey,
signature,
nonce: authConfig.nonce
}
};
return new Promise((resolve, reject) => {
const request = https.request(requestOptions, (response) => {
response.setEncoding('utf8');
let data = '';
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
resolve(data);
});
});
request.on('error', (error) => {
reject(error);
});
request.end();
});
};
const main = async () => {
try {
const response = await getTextResult(
serverConfig,
authConfig,
listConfig
);
console.log('Server response:');
console.log(response);
} catch (error) {
console.error(error);
}
};
main();