Bug 30394: Add x-koha-request-id handling in controllers
[srvgit] / Koha / REST / Plugin / Objects.pm
index 635f6d0..4a5d291 100644 (file)
@@ -29,21 +29,23 @@ Koha::REST::Plugin::Objects
 
 =head2 Helper methods
 
-=head3 objects.search
+=cut
+
+sub register {
+    my ( $self, $app ) = @_;
+
+=head3 objects.find
 
     my $patrons_rs = Koha::Patrons->new;
-    my $patrons = $c->objects->search( $patrons_rs );
+    my $patrons = $c->objects->find( $patrons_rs, $id );
 
-Performs a database search using given Koha::Objects object and query parameters.
+Performs a database search using given Koha::Objects object and the $id.
 
-Returns an arrayref of the hashrefs representing the resulting objects
-for API rendering.
+Returns I<undef> if no object is found Returns the I<API representation> of
+the requested object. It passes through any embeds if specified.
 
 =cut
 
-sub register {
-    my ( $self, $app ) = @_;
-
     $app->helper(
         'objects.find' => sub {
             my ( $c, $result_set, $id ) = @_;
@@ -62,10 +64,27 @@ sub register {
 
             my $object = $result_set->find( $id, $attributes );
 
+            return unless $object;
+
             return $object->to_api({ embed => $embed });
         }
     );
 
+=head3 objects.search
+
+    my $patrons_rs = Koha::Patrons->new;
+    my $patrons = $c->objects->search( $patrons_rs );
+
+Performs a database search using given Koha::Objects object and query parameters.
+
+Returns an arrayref of the hashrefs representing the resulting objects
+for API rendering.
+
+Warning: this helper adds pagination headers to the calling controller, and thus
+shouldn't be called twice in it.
+
+=cut
+
     $app->helper(
         'objects.search' => sub {
             my ( $c, $result_set ) = @_;
@@ -75,6 +94,8 @@ sub register {
 
             # Extract reserved params
             my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args);
+            # Privileged reques?
+            my $is_public = $c->stash('is_public');
             # Look for embeds
             my $embed = $c->stash('koha.embed');
 
@@ -117,23 +138,58 @@ sub register {
                 $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
             }
 
-            if( defined $reserved_params->{q} || defined $reserved_params->{query} || defined $reserved_params->{'x-koha-query'}) {
-                $filtered_params //={};
+            if (   defined $reserved_params->{q}
+                || defined $reserved_params->{query}
+                || defined $reserved_params->{'x-koha-query'} )
+            {
+                $filtered_params //= {};
+
                 my @query_params_array;
-                my $query_params;
-                push @query_params_array, $reserved_params->{query} if defined $reserved_params->{query};
+
+                # query in request body, JSON::Validator already decoded it
+                push @query_params_array, $reserved_params->{query}
+                  if defined $reserved_params->{query};
+
                 my $json = JSON->new;
-                push @query_params_array, $json->decode($reserved_params->{q}) if defined $reserved_params->{q};
-                push @query_params_array, $json->decode($reserved_params->{'x-koha-query'}) if defined $reserved_params->{'x-koha-query'};
 
-                if(scalar(@query_params_array) > 1) {
-                    $query_params = {'-and' => \@query_params_array};
-                } else {
+                if ( ref($reserved_params->{q}) eq 'ARRAY' ) {
+                    # q is defined as multi => JSON::Validator generates an array
+                    foreach my $q ( @{ $reserved_params->{q} } ) {
+                        push @query_params_array, $json->decode($q)
+                        if $q; # skip if exists but is empty
+                    }
+                }
+                else {
+                    # objects.search called outside OpenAPI context
+                    # might be a hashref
+                    push @query_params_array, $json->decode($reserved_params->{q})
+                        if $reserved_params->{q};
+                }
+
+                push @query_params_array,
+                  $json->decode( $reserved_params->{'x-koha-query'} )
+                  if defined $reserved_params->{'x-koha-query'};
+
+                my $query_params;
+
+                if ( scalar(@query_params_array) > 1 ) {
+                    $query_params = { '-and' => \@query_params_array };
+                }
+                else {
                     $query_params = $query_params_array[0];
                 }
 
                 $filtered_params = $c->merge_q_params( $filtered_params, $query_params, $result_set );
             }
+
+            # request sequence id (i.e. 'draw' Datatables parameter)
+            $c->res->headers->add( 'x-koha-request-id' => $reserved_params->{'x-koha-request-id'} )
+              if $reserved_params->{'x-koha-request-id'};
+
+            # If search_limited exists, use it
+            $result_set = $result_set->search_limited,
+                if $result_set->can('search_limited');
+
             # Perform search
             my $objects = $result_set->search( $filtered_params, $attributes );
             my $total   = $result_set->search->count;
@@ -146,7 +202,7 @@ sub register {
                 }
             );
 
-            return $objects->to_api({ embed => $embed });
+            return $objects->to_api({ embed => $embed, public => $is_public });
         }
     );
 }