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

fix: access knowledge panels via API #7711

Merged
merged 3 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/ProductOpener/API.pm
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ use ProductOpener::Export qw/:all/;
use ProductOpener::Tags qw/:all/;
use ProductOpener::Text qw/:all/;
use ProductOpener::Attributes qw/:all/;
use ProductOpener::KnowledgePanels qw/:all/;
use ProductOpener::Ecoscore qw/localize_ecoscore/;

use ProductOpener::APIProductRead qw/:all/;
Expand Down
33 changes: 0 additions & 33 deletions lib/ProductOpener/Display.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4626,39 +4626,6 @@ sub add_params_to_query ($request_ref, $query_ref) {
return;
}

=head2 initialize_knowledge_panels_options( $knowledge_panels_options_ref, $request_ref )

Initialize the options for knowledge panels from parameters.

=cut

sub initialize_knowledge_panels_options ($knowledge_panels_options_ref, $request_ref) {

# Activate physical activity knowledge panel only when specified
if (single_param("activate_knowledge_panel_physical_activities")) {
$knowledge_panels_options_ref->{activate_knowledge_panel_physical_activities} = 1;
}

# Specify if we knowledge panels are requested from the app or the website
# in order to allow different behaviours (e.g. showing ingredients before nutrition on the web)
# possible values: "web", "app"
my $knowledge_panels_client = single_param("knowledge_panels_client");
# set a default value if client is not defined to app or web
if ( (not defined $knowledge_panels_client)
or (($knowledge_panels_client ne "web") and ($knowledge_panels_client ne "app")))
{
# Default to app mode
$knowledge_panels_client = 'app';
# but if it's not an api request, we consider it should be web
if (not defined $request_ref->{api}) {
$knowledge_panels_client = "web";
}
}
$knowledge_panels_options_ref->{knowledge_panels_client} = $knowledge_panels_client;

return;
}

sub search_and_display_products ($request_ref, $query_ref, $sort_by, $limit, $page) {

$request_ref->{page_type} = "products";
Expand Down
33 changes: 33 additions & 0 deletions lib/ProductOpener/KnowledgePanels.pm
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ BEGIN {
use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS);
@EXPORT_OK = qw(

&initialize_knowledge_panels_options
&create_knowledge_panels

); # symbols to export on request
Expand All @@ -69,6 +70,38 @@ use Data::DeepAccess qw(deep_get);

=head1 FUNCTIONS

=head2 initialize_knowledge_panels_options( $knowledge_panels_options_ref, $request_ref )

Initialize the options for knowledge panels from parameters.

=cut

sub initialize_knowledge_panels_options ($knowledge_panels_options_ref, $request_ref) {

# Activate physical activity knowledge panel only when specified
if (single_param("activate_knowledge_panel_physical_activities")) {
$knowledge_panels_options_ref->{activate_knowledge_panel_physical_activities} = 1;
}

# Specify if we knowledge panels are requested from the app or the website
# in order to allow different behaviours (e.g. showing ingredients before nutrition on the web)
# possible values: "web", "app"
my $knowledge_panels_client = single_param("knowledge_panels_client");
# set a default value if client is not defined to app or web
if ( (not defined $knowledge_panels_client)
or (($knowledge_panels_client ne "web") and ($knowledge_panels_client ne "app")))
{
# Default to app mode
$knowledge_panels_client = 'app';
# but if it's not an api request, we consider it should be web
if (not defined $request_ref->{api}) {
$knowledge_panels_client = "web";
}
}
$knowledge_panels_options_ref->{knowledge_panels_client} = $knowledge_panels_client;

return;
}

=head2 create_knowledge_panels( $product_ref, $target_lc, $target_cc, $options_ref )

Expand Down
28 changes: 28 additions & 0 deletions tests/integration/api_v2_product_read.t
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,34 @@ my $tests_ref = [
query_string => '?fields=product_name,categories_tags,categories_tags_en',
expected_status_code => 200,
},
{
test_case => 'get-attribute-groups',
method => 'GET',
path => '/api/v2/product/200000000034',
query_string => '?fields=attribute_groups',
expected_status_code => 200,
},
{
test_case => 'get-attribute-groups-fr',
method => 'GET',
path => '/api/v2/product/200000000034',
query_string => '?fields=attribute_groups&lc=fr',
expected_status_code => 200,
},
{
test_case => 'get-knowledge-panels',
method => 'GET',
path => '/api/v2/product/200000000034',
query_string => '?fields=knowledge_panels',
expected_status_code => 200,
},
{
test_case => 'get-knowledge-panels-fr',
method => 'GET',
path => '/api/v2/product/200000000034',
query_string => '?fields=knowledge_panels&lc=fr',
expected_status_code => 200,
},
];

execute_api_tests(__FILE__, $tests_ref);
Expand Down
28 changes: 28 additions & 0 deletions tests/integration/api_v3_product_read.t
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,34 @@ my $tests_ref = [
query_string => '?fields=images_to_update_en',
expected_status_code => 200,
},
{
test_case => 'get-attribute-groups',
method => 'GET',
path => '/api/v3/product/200000000034',
query_string => '?fields=attribute_groups',
expected_status_code => 200,
},
{
test_case => 'get-attribute-groups-fr',
method => 'GET',
path => '/api/v3/product/200000000034',
query_string => '?fields=attribute_groups&lc=fr',
expected_status_code => 200,
},
{
test_case => 'get-knowledge-panels',
method => 'GET',
path => '/api/v3/product/200000000034',
query_string => '?fields=knowledge_panels',
expected_status_code => 200,
},
{
test_case => 'get-knowledge-panels-fr',
method => 'GET',
path => '/api/v3/product/200000000034',
query_string => '?fields=knowledge_panels&lc=fr',
expected_status_code => 200,
},
];

execute_api_tests(__FILE__, $tests_ref);
Expand Down
Loading