<<

NAME

ProductOpener::APIProductServices - Microservices to enrich a product object

DESCRIPTION

This module implements a microservice API for operations done on an product object.

Applications can send product data (for instance a nested list of ingredients), ask for one or more services to be executed on the input product data (for instance computing the min, max and estimated percentages of each ingredient), and get back resulting product data (possibly filtered to get only specific fields back).

INTERFACE

Request

The Routing.pm and API.pm module offer an HTTP interface of this form: POST /api/v3/product_services

The POST body is a JSON object with those fields:

services

An array list of services to perform.

Currently implemented services:

- echo : does nothing, mostly for testing - parse_ingredients_text : parse the ingredients text list and return an ingredients object - extend_ingredients : extend the ingredients object with additional information - estimate_ingredients_percent : compute percent_min, percent_max, percent_estimate for each ingredient in the ingredients object - analyze_ingredients : analyze the ingredients object and return a summary object - estimate_environmental_cost_ingredients : estimate the environmental cost of a given product (see Ecobalyse)

product

A product object

fields

An array list of fields to return. If empty, only fields that can be created or updated by the service are returned. e.g. a service to parse the ingredients text list will return the "ingredients" object.

Response

The response is in the JSON API v3 response format, with a resulting product object.

add_product_data_from_external_service ($request_ref, $product_ref, $url, $services_ref)

Make a request to execute services on product on an external server using the product services API.

The resulting fields are added to the product object.

e.g. this function is used to run the percent estimation service on the recipe-estimator server.

Parameters

$request_ref

The request object, used to log the request and to add errors if the external service call fails.

$product_ref

The product object to send to the external service, and to which the resulting fields will be added.

$url

The URL of the external service to call.

$services_ref

An array reference of services to perform on the product.

Return value

1: the external service was called successfully and the product object was updated with the resulting fields. 0: the external service call failed, an error was added to the request object, and the product object was not updated.

echo_service ($product_ref, $updated_product_fields_ref, $errors_ref)

Echo service that returns the input product unchanged.

Service function handlers

They will be called with the input product object, a reference to the updated fields hash, and a reference to the errors array.

product_services_api($request_ref)

Process API v3 product services requests.

external_sources

Get external sources but translated in target language

<<