X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=C4%2FService.pm;h=fa95a61cdbd7e41d335d77d048532c01ef231587;hb=82ec758f18ba5b2600c065245fb68a3f177dcba8;hp=bf7ef57f38775791dec955f78b4995286168a8eb;hpb=fdd82e8054d132c7da889dada5b538a7bf20951a;p=koha.git diff --git a/C4/Service.pm b/C4/Service.pm index bf7ef57f38..fa95a61cdb 100644 --- a/C4/Service.pm +++ b/C4/Service.pm @@ -4,18 +4,18 @@ package C4::Service; # # This file is part of Koha. # -# Koha is free software; you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation; either version 2 of the License, or (at your option) any later -# version. +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. # -# Koha is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along with -# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, -# Suite 330, Boston, MA 02111-1307 USA +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . =head1 NAME @@ -41,7 +41,7 @@ This module packages several useful functions for JSON webservices. use strict; use warnings; -use CGI; +use CGI qw ( -utf8 ); use C4::Auth qw( check_api_auth ); use C4::Output qw( :ajax ); use C4::Output::JSONStream; @@ -55,15 +55,22 @@ BEGIN { our ( $query, $cookie ); -=head1 METHODS +sub _output { + my ( $response, $status ) = @_; + binmode STDOUT, ':encoding(UTF-8)'; -=head2 init + if ( $query->param( 'callback' ) ) { + output_with_http_headers $query, $cookie, $query->param( 'callback' ) . '(' . $response->output . ');', 'js'; + } else { + output_with_http_headers $query, $cookie, $response->output, 'json', $status; + } +} -=over 4 +=head1 METHODS - our ( $query, $response ) = C4::Service->init( %needed_flags ); +=head2 init -=back + our ( $query, $response ) = C4::Service->init( %needed_flags ); Initialize the service and check for the permissions in C<%needed_flags>. @@ -92,12 +99,8 @@ sub init { =head2 return_error -=over 4 - C4::Service->return_error( $type, $error, %flags ); -=back - Exit the script with HTTP status 400, and return a JSON error object. C<$type> should be a short, lower case code for the generic type of error (such @@ -121,28 +124,20 @@ sub return_error { $response->param( message => $error ) if ( $error ); $response->param( type => $type, %flags ); - output_with_http_headers $query, $cookie, $response->output, 'json', '400 Bad Request'; + _output( $response, '400 Bad Request' ); exit; } =head2 return_multi -=over 4 - -C4::Service->return_multi( \@responses, %flags ); - -=back + C4::Service->return_multi( \@responses, %flags ); return_multi is similar to return_success or return_error, but allows you to return different statuses for several requests sent at once (using HTTP status "207 Multi-Status", much like WebDAV). The toplevel hashref (turned into the JSON response) looks something like this: -=over 4 - -{ multi => JSON::true, responses => \@responses, %flags } - -=back + { multi => JSON::true, responses => \@responses, %flags } Each element of @responses should be either a plain hashref or an arrayref. If it is a hashref, it is sent to the browser as-is. If it is an arrayref, it is @@ -175,7 +170,7 @@ sub return_multi { } $response->param( 'multi' => JSON::true, responses => \@responses_formatted, @flags ); - output_with_http_headers $query, $cookie, $response->output, 'json', '207 Multi-Status'; + _output( $response, '207 Multi-Status' ); } exit; @@ -183,12 +178,8 @@ sub return_multi { =head2 return_success -=over 4 - C4::Service->return_success( $response ); -=back - Print out the information in the C C<$response>, then exit with HTTP status 200. @@ -197,17 +188,13 @@ exit with HTTP status 200. sub return_success { my ( $class, $response ) = @_; - output_with_http_headers $query, $cookie, $response->output, 'json'; + _output( $response ); } =head2 require_params -=over 4 - my @values = C4::Service->require_params( @params ); -=back - Check that each of of the parameters specified in @params was sent in the request, then return their values in that order. @@ -222,7 +209,7 @@ sub require_params { for my $param ( @params ) { $class->return_error( 'params', "Missing '$param'" ) if ( !defined( $query->param( $param ) ) ); - push @values, $query->param( $param ); + push @values, scalar $query->param( $param ); # will we ever need multi_param here? } return @values; @@ -230,14 +217,10 @@ sub require_params { =head2 dispatch -=over 4 - -C4::Service->dispatch( - [ $path_regex, \@required_params, \&handler ], - ... -); - -=back + C4::Service->dispatch( + [ $path_regex, \@required_params, \&handler ], + ... + ); dispatch takes several array-refs, each one describing a 'route', to use the Rails terminology.