Skip to content

Run APIs with Benerator using javascript #456

@roxsyr

Description

@roxsyr

Is your feature request related to a problem? Please describe.
Good Afternom!
I want to execute the call to an API to insert the data or part of the data that it returns in a json within the tables of my database, I have already done different tests during the execution and I still do not have the result I expect, follow the script that creates:


<!-- Inicializa o arquivo csv para mostrar as CBs criadas -->
<bean id="in" class="CSVEntitySource">
	<property name="uri" value="Geradores/csv/cbs-out.csv"/>
	<property name="columns" value="person_code, person_id, full_name"/>
</bean>
<!-- Configura o chamado da API para listar Dados -->
<!--<execute type="js" uri="js/api_request.js"/>-->
<execute type="js">
	function endereco() {
		const data = getEndereco();
		console.log('data: ',data);
		return data.cep;
	}

	// Excecute the API with fetch
	async function getEndereco() {
		// Setup url public to find address by Zipcode
		const apiUrl = 'https://viacep.com.br/ws/01001000/json/';
		console.log('getEnderco:', apiUrl);
		// Setup the fetch with require
		const fetch = require('fetch');
		try {
			console.log('try: ', apiUrl);
			// Execute the request using fetch and wait for the response
			const response = await fetch(apiUrl);

			// Check if the response was successful (status code 200)
			if (!response.ok) {
				throw new Error('La solicitud no fue exitosa');
			}

			// Parse the response as JSON and wait for the result
			const data = await response.json();

			// Return data
			console.log(data);
			return data;
		} catch (error) {
			// Catch any errors that occur during the request
			console.log('Error al obtener los datos:', error);
		}
	}
</execute>

<!-- List People -->
<echo>Lista 20 People Address </echo>
<!-- List 20 People with their CEP -->
<iterate source="in" type="CB" count="20" consumer="ConsoleExporter">
	<attribute name="cep" script="{js:endereco()}"/>
</iterate>

Describe the solution you'd like
The function getEndereco has return this indormation:
{
"cep": "01001-000",
"logradouro": "Praça da Sé",
"complemento": "lado ímpar",
"bairro": "Sé",
"localidade": "São Paulo",
"uf": "SP",
"ibge": "3550308",
"gia": "1004",
"ddd": "11",
"siafi": "7107"
}

Currently I am not receiving any returns, because it is not executing the instructions that are after the line of the const fetch = require('fetch').

Describe alternatives you've considered
I am considering creating a java file to then be executed as a jar, can you help me or guide me on how I can execute this execution?

Additional context
It also requires help configuring Graalvm, so that the warning shown in the image does not appear.

Screenshot_Benerator

Metadata

Metadata

Assignees

Labels

help wantedExtra attention is needed

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions