Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run APIs with Benerator using javascript #456

Open
roxsyr opened this issue May 23, 2024 · 0 comments
Open

Run APIs with Benerator using javascript #456

roxsyr opened this issue May 23, 2024 · 0 comments
Assignees
Labels
help wanted Extra attention is needed

Comments

@roxsyr
Copy link

roxsyr commented May 23, 2024

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

@ake2l ake2l self-assigned this May 26, 2024
@ake2l ake2l added the help wanted Extra attention is needed label May 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants