Skip to content

Commit

Permalink
Making API requests !
Browse files Browse the repository at this point in the history
  • Loading branch information
very-smartin committed Jan 8, 2025
1 parent 6f677c6 commit c6c6217
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 11 deletions.
104 changes: 97 additions & 7 deletions lib/ProductOpener/EnvironmentalImapct.pm
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,103 @@ sub estimate_environmental_impact_service ($product_ref, $updated_product_fields
$updated_product_fields_ref->{environmental_impact} = 1;
$product_ref->{environmental_impact} = 0;

# Estimating the environmental impact
while (@ingredients) {
# Remove and process the first ingredient from the list
my $ingredient_ref = shift @ingredients;

# Dummy calculation
$product_ref->{environmental_impact}++;
# Initialisation of the payload structure
my $payload = {
ingredients => [],
transform => {},
packaging => [],
distribution => "ambient",
preparation => ["refrigeration"]
};

# Estimating the environmental impact
while (@{$product_ref->{ingredients}}) {
# Retrieving the first ingredient and deleting it
my $ingredient_ref = shift @{$product_ref->{ingredients}};

# Adding each ingredient to the payload structure
push @{$payload->{ingredients}}, {
id => $ingredient_ref->{id},
mass => $ingredient_ref->{mass}
};
}

# Adding a transformation
if (defined $product_ref->{transform}) {
$payload->{transform} = {
id => $product_ref->{transform}->{id},
mass => $product_ref->{transform}->{mass}
};
}

# Adding a packaging
if (defined $product_ref->{packaging}) {
foreach my $packaging_ref (@{$product_ref->{packaging}}) {
push @{$payload->{packaging}}, {
id => $packaging_ref->{id},
mass => $packaging_ref->{mass}
};
}
}

# URL de l'API
my $url_recipe = "https://staging-ecobalyse.incubateur.net/api/food";

# Créer un objet UserAgent pour faire la requête API
my $ua = LWP::UserAgent->new();
$ua->timeout(2); # Définir le timeout de la requête

# Préparer la requête POST avec le payload
my $request = POST $url_recipe, $payload;
$request->header('content-type' => 'application/json');
$request->content(decode_utf8(encode_json($payload)));

# Informations de débogage pour la requête
$log->debug("send_event request", {endpoint => $url_recipe, payload => $payload}) if $log->is_debug();

# Envoyer la requête et obtenir la réponse
my $response = $ua->request($request);

# Gérer la réponse en fonction du succès ou de l'échec
if ($response->is_success) {
$log->debug(
"send_event response ok",
{
endpoint => $url_recipe,
payload => $payload,
is_success => $response->is_success,
code => $response->code,
status_line => $response->status_line
}
) if $log->is_debug();

# Analyser la réponse JSON
my $response_data = decode_json($response->decoded_content);

# Accéder à la valeur spécifique "ecs"
my $ecs_value = $response_data->{results} // {}->{total} // {}->{ecs};

# Vérifier si ecs existe et le stocker dans le champ de produit
if (defined $ecs_value) {
$product_ref->{ecs} = $ecs_value;
$log->debug("ecs value stored", {ecs => $product_ref->{ecs}}) if $log->is_debug();
}
else {
$log->warn("La clé 'ecs' n'a pas été trouvée dans la réponse") if $log->is_warn();
}
}
else {
$log->warn(
"send_event response not ok",
{
endpoint => $url_recipe,
payload => $payload,
is_success => $response->is_success,
code => $response->code,
status_line => $response->status_line,
response => $response
}
) if $log->is_warn();
}

# If necessary, return error as well
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ my $tests_ref = [
"fields": ["product_name_en","product_name_fr"],'
. $product_hazelnut_spread_json . '}',
},
# compute the environmental impact of the product
# return a new field
# compute the environmental impact of the product
# return a new field
{
test_case => 'echo-service-hazelnut-spread-enviro-field',
method => 'POST',
Expand All @@ -126,8 +126,8 @@ my $tests_ref = [
"fields": ["estimate_environmental_impact_service"],'
. $product_hazelnut_spread_json . '}',
},
# compute the environmental impact of the product
# return the whole product object
# compute the environmental impact of the product
# return the whole product object
{
test_case => 'echo-service-hazelnut-spread-enviro-field',
method => 'POST',
Expand Down

0 comments on commit c6c6217

Please sign in to comment.