@@ -, +, @@
* application/json
* application/marcxml+xml
* application/marc-in-json
* application/marc
* text/plain
---
Koha/REST/V1/Authorities.pm | 66 +++++++++++++++++++++++
api/v1/swagger/paths/authorities.yaml | 57 ++++++++++++++++++++
t/db_dependent/api/v1/authorities.t | 77 ++++++++++++++++++++++++++-
3 files changed, 199 insertions(+), 1 deletion(-)
--- a/Koha/REST/V1/Authorities.pm
+++ a/Koha/REST/V1/Authorities.pm
@@ -253,4 +253,70 @@ sub update {
};
}
+=head3 list
+
+Controller function that handles retrieving a list of authorities
+
+=cut
+
+sub list {
+ my $c = shift->openapi->valid_input or return;
+
+ my $authorities = $c->objects->search_rs( Koha::Authorities->new );
+
+ return try {
+
+ if ( $c->req->headers->accept =~ m/application\/json(;.*)?$/ ) {
+ return $c->render(
+ status => 200,
+ json => $authorities->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 => $authorities->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 => $authorities->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 => $authorities->print_collection('marc')
+ );
+ }
+ elsif ( $c->req->headers->accept =~ m/text\/plain(;.*)?$/ ) {
+ return $c->render(
+ status => 200,
+ text => $authorities->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;
--- a/api/v1/swagger/paths/authorities.yaml
+++ a/api/v1/swagger/paths/authorities.yaml
@@ -1,5 +1,62 @@
---
"/authorities":
+ get:
+ x-mojo-to: Authorities#list
+ operationId: listAuthorities
+ tags:
+ - authorities
+ summary: List authorities
+ 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 authorities
+ "401":
+ description: Authentication required
+ schema:
+ $ref: "../swagger.yaml#/definitions/error"
+ "403":
+ description: Access forbidden
+ schema:
+ $ref: "../swagger.yaml#/definitions/error"
+ "404":
+ description: Authority 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"
post:
x-mojo-to: Authorities#add
operationId: addAuthority
--- a/t/db_dependent/api/v1/authorities.t
+++ a/t/db_dependent/api/v1/authorities.t
@@ -20,7 +20,7 @@ use Modern::Perl;
use utf8;
use Encode;
-use Test::More tests => 4;
+use Test::More tests => 5;
use Test::MockModule;
use Test::Mojo;
use Test::Warn;
@@ -314,3 +314,78 @@ subtest 'put() tests' => sub {
$schema->storage->txn_rollback;
};
+
+
+
+subtest 'list() tests' => sub {
+ plan tests => 14;
+
+ $schema->storage->txn_begin;
+
+ my $patron = $builder->build_object(
+ {
+ class => 'Koha::Patrons',
+ value => { flags => 0 }
+ }
+ );
+ my $password = 'thePassword123';
+ $patron->set_password( { password => $password, skip_validation => 1 } );
+ $patron->discard_changes;
+ my $userid = $patron->userid;
+
+ my $authid1 = $builder->build_object({ 'class' => 'Koha::Authorities', value => {
+ marcxml => q|
+
+ 1001
+
+ 102
+ My Corporation
+
+|
+ } })->authid;
+
+ my $authid2 = $builder->build_object({ 'class' => 'Koha::Authorities', value => {
+ marcxml => q|
+
+ 1001
+
+ 102
+ My Corporation
+
+|
+ } })->authid;
+
+ my $search =
+"[{\"authid\": \"$authid1\"}, {\"authid\": \"$authid2\"}]";
+ $t->get_ok(
+ "//$userid:$password@/api/v1/authorities/" => { 'x-koha-query' => $search }
+ )->status_is(403);
+
+ $patron->flags(4)->store;
+
+ $t->get_ok( "//$userid:$password@/api/v1/authorities/" =>
+ { Accept => 'application/weird+format', 'x-koha-query' => $search } )
+ ->status_is(400);
+
+ $t->get_ok( "//$userid:$password@/api/v1/authorities/" =>
+ { Accept => 'application/json', 'x-koha-query' => $search } )
+ ->status_is(200);
+
+ $t->get_ok( "//$userid:$password@/api/v1/authorities/" =>
+ { Accept => 'application/marcxml+xml', 'x-koha-query' => $search } )
+ ->status_is(200);
+
+ $t->get_ok( "//$userid:$password@/api/v1/authorities/" =>
+ { Accept => 'application/marc-in-json', 'x-koha-query' => $search } )
+ ->status_is(200);
+
+ $t->get_ok( "//$userid:$password@/api/v1/authorities/" =>
+ { Accept => 'application/marc', 'x-koha-query' => $search } )
+ ->status_is(200);
+
+ $t->get_ok( "//$userid:$password@/api/v1/authorities/" =>
+ { Accept => 'text/plain', 'x-koha-query' => $search } )
+ ->status_is(200);
+
+ $schema->storage->txn_rollback;
+};
--