Skip to content

Commit

Permalink
Merge pull request #1110 from openfoodfacts/issue/623-compact-json-api
Browse files Browse the repository at this point in the history
add optional fields parameter to JSON API
  • Loading branch information
stephanegigandet authored Feb 24, 2018
2 parents d602fff + 16d18d1 commit e5dbf9d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cgi/search.pl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
$action = 'process';
}

foreach my $parameter ('json', 'jsonp', 'jqm', 'jqm_loadmore', 'xml', 'rss') {
foreach my $parameter ('fields', 'json', 'jsonp', 'jqm', 'jqm_loadmore', 'xml', 'rss') {

if (defined param($parameter)) {
$request_ref->{$parameter} = param($parameter);
Expand Down
55 changes: 46 additions & 9 deletions lib/ProductOpener/Display.pm
Original file line number Diff line number Diff line change
Expand Up @@ -395,28 +395,34 @@ sub analyze_request($)

print STDERR "analyze_request : query_string 1 : $request_ref->{query_string} \n";

# Process API parameters: fields, formats, revision

# API calls may request JSON, JSONP or XML by appending .json, .jsonp or .xml at the end of the query string
# .jqm returns results in HTML specifically formated for the OFF mobile app (which uses jquerymobile)
# for calls to /cgi/ actions (e.g. search.pl), the format can also be indicated with a parameter &json=1 &jsonp=1 &xml=1 &jqm=1
# (or ?json=1 if it's the first parameter)

# first check for the rev parameter (revision of a product)
foreach my $parameter ('rev', 'json', 'jsonp', 'jqm','xml') {
# first check parameters in the query string

foreach my $parameter ('fields', 'rev', 'json', 'jsonp', 'jqm','xml') {

if ($request_ref->{query_string} =~ /(\&|\?)$parameter=(\d+)/) {
$request_ref->{query_string} =~ s/(\&|\?)$parameter=(\d+)//;
if ($request_ref->{query_string} =~ /(\&|\?)$parameter=([^\&]+)/) {
$request_ref->{query_string} =~ s/(\&|\?)$parameter=([^\&]+)//;
$request_ref->{$parameter} = $2;
print STDERR "analyze_request : $parameter = $request_ref->{$parameter} \n";
}

}

# then check suffixes .json etc.

foreach my $parameter ('json', 'jsonp', 'jqm','xml') {

if ($request_ref->{query_string} =~ /\.$parameter$/) {
$request_ref->{query_string} =~ s/\.$parameter$//;
$request_ref->{$parameter} = 1;
print STDERR "analyze_request : $parameter = 1 (.$parameter) \n";
}

}
}

print STDERR "analyze_request : query_string 2 : $request_ref->{query_string} \n";

Expand Down Expand Up @@ -2747,6 +2753,24 @@ HTML
}


# If the request specified a value for the fields parameter, return only the fields listed
if (defined $request_ref->{fields}) {

my $compact_products = [];

for my $product_ref (@{$request_ref->{structured_response}{products}}) {

my $compact_product_ref = {};
foreach my $field (split(/,/, $request_ref->{fields})) {
if (defined $product_ref->{$field}) {
$compact_product_ref->{$field} = $product_ref->{$field};
}
}
push @$compact_products, $compact_product_ref;
}

$request_ref->{structured_response}{products} = $compact_products;
}


# Pagination
Expand Down Expand Up @@ -7698,10 +7722,23 @@ HTML
else {
$response{status} = 1;
$response{status_verbose} = 'product found';
$response{product} = $product_ref;
add_images_urls_to_product($product_ref);
$response{product} = $product_ref;
# If the request specified a value for the fields parameter, return only the fields listed
if (defined $request_ref->{fields}) {
my $compact_product_ref = {};
foreach my $field (split(/,/, $request_ref->{fields})) {
if (defined $product_ref->{$field}) {
$compact_product_ref->{$field} = $product_ref->{$field};
}
}
$response{product} = $compact_product_ref;
}
if ($request_ref->{jqm}) {
# return a jquerymobile page for the product
Expand Down

0 comments on commit e5dbf9d

Please sign in to comment.