Bug 32734: Add REST endpoint to list biblios

This patch adds an endpoint to list biblios

To test:
1. apply patch
2. enable basic auth
3. call to GET /api/v1/biblios with the following Accept headers:
 * application/json
 * application/marcxml+xml
 * application/marc-in-json
 * application/marc
 * text/plain
4. notice how data changes with each Accept header
5. prove t/db_dependent/Koha/Biblios.t t/db_dependent/api/v1/biblios.t
6. sign off

Signed-off-by: Matt Blenkinsop <matt.blenkinsop@ptfs-europe.com>
Signed-off-by: Hammat Wele <hammat.wele@inlibro.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
(cherry picked from commit a4fb1ca3d1)
Signed-off-by: Jacob O'Mara <jacob.omara@ptfs-europe.com>
This commit is contained in:
Agustin Moyano 2023-02-16 22:17:43 -03:00 committed by Jacob O'Mara
parent fed1e0ff76
commit 7b3fd8f475
4 changed files with 934 additions and 708 deletions

View file

@ -24,6 +24,10 @@ use Koha::Database;
use Koha::Biblio;
use Koha::Libraries;
use MARC::File::MiJ;
use MARC::File::USMARC;
use MARC::File::XML;
use MARC::Record;
use base qw(Koha::Objects);
@ -35,6 +39,45 @@ Koha::Biblios - Koha Biblio object set class
=head2 Class methods
=head3 print_collection
my $collection_text = $result_set->print_collection($format)
Return a text representation of a collection (group of records) in the specified format.
Allowed formats are marcxml, mij, marc and txt. Defaults to marcxml.
=cut
sub print_collection {
my ( $self, $format ) = @_;
my ($start, $glue, $end, @parts);
my %serializers = (
'mij' => \&MARC::File::MiJ::encode,
'marc' => \&MARC::File::USMARC::encode,
'txt' => \&MARC::Record::as_formatted,
'marcxml' => \&MARC::File::XML::record
);
if ($format eq 'mij') {
$start = '[';
$glue = ',';
$end = ']';
} elsif ($format eq 'marc') {
$glue = "\n";
} elsif ($format eq 'txt') {
$glue = "\n\n";
} else {
$glue = '';
$format = 'marcxml';
$start = MARC::File::XML::header();
$end = MARC::File::XML::footer();
}
while (my $biblio = $self->next) {
push @parts, $serializers{$format}->($biblio->metadata->record);
}
return (defined $start ? $start : '').join($glue, @parts).(defined $end ? $end : '');
}
=head3 pickup_locations
my $biblios = Koha::Biblios->search(...);

View file

@ -597,4 +597,76 @@ sub update {
$c->unhandled_exception($_);
};
}
=head3 list
Controller function that handles retrieving a single biblio object
=cut
sub list {
my $c = shift->openapi->valid_input or return;
my $attributes;
$attributes =
{ prefetch => ['metadata'] } # don't prefetch metadata if not needed
unless $c->req->headers->accept =~ m/application\/json/;
my $biblios = $c->objects->search_rs( Koha::Biblios->new );
return try {
if ( $c->req->headers->accept =~ m/application\/json(;.*)?$/ ) {
return $c->render(
status => 200,
json => $biblios->to_api
);
}
elsif (
$c->req->headers->accept =~ m/application\/marcxml\+xml(;.*)?$/ )
{
$c->res->headers->add( 'Content-Type', 'application/marcxml+xml' );
return $c->render(
status => 200,
text => $biblios->print_collection('marcxml')
);
}
elsif (
$c->req->headers->accept =~ m/application\/marc-in-json(;.*)?$/ )
{
$c->res->headers->add( 'Content-Type', 'application/marc-in-json' );
return $c->render(
status => 200,
data => $biblios->print_collection('mij')
);
}
elsif ( $c->req->headers->accept =~ m/application\/marc(;.*)?$/ ) {
$c->res->headers->add( 'Content-Type', 'application/marc' );
return $c->render(
status => 200,
text => $biblios->print_collection('marc')
);
}
elsif ( $c->req->headers->accept =~ m/text\/plain(;.*)?$/ ) {
return $c->render(
status => 200,
text => $biblios->print_collection('txt')
);
}
else {
return $c->render(
status => 406,
openapi => [
"application/json", "application/marcxml+xml",
"application/marc-in-json", "application/marc",
"text/plain"
]
);
}
}
catch {
$c->unhandled_exception($_);
};
}
1;

View file

@ -56,6 +56,63 @@
x-koha-authorization:
permissions:
editcatalogue: edit_catalogue
get:
x-mojo-to: Biblios#list
operationId: listBiblio
tags:
- biblios
summary: List biblios
parameters:
- $ref: "../swagger.yaml#/parameters/page"
- $ref: "../swagger.yaml#/parameters/per_page"
- $ref: "../swagger.yaml#/parameters/match"
- $ref: "../swagger.yaml#/parameters/order_by"
- $ref: "../swagger.yaml#/parameters/q_param"
- $ref: "../swagger.yaml#/parameters/q_body"
- $ref: "../swagger.yaml#/parameters/q_header"
- $ref: "../swagger.yaml#/parameters/request_id_header"
produces:
- application/json
- application/marcxml+xml
- application/marc-in-json
- application/marc
- text/plain
responses:
"200":
description: A list of biblios
"401":
description: Authentication required
schema:
$ref: "../swagger.yaml#/definitions/error"
"403":
description: Access forbidden
schema:
$ref: "../swagger.yaml#/definitions/error"
"404":
description: Biblio not found
schema:
$ref: "../swagger.yaml#/definitions/error"
"406":
description: Not acceptable
schema:
type: array
description: Accepted content-types
items:
type: string
"500":
description: |
Internal server error. Possible `error_code` attribute values:
* `internal_server_error`
schema:
$ref: "../swagger.yaml#/definitions/error"
"503":
description: Under maintenance
schema:
$ref: "../swagger.yaml#/definitions/error"
x-koha-authorization:
permissions:
catalogue: "1"
"/biblios/{biblio_id}":
get:
x-mojo-to: Biblios#get

File diff suppressed because it is too large Load diff