Bugzilla – Attachment 150746 Details for
Bug 32735
Add GET endpoint for listing Authorities
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 32735: Add endpoint to list authorities
Bug-32735-Add-endpoint-to-list-authorities.patch (text/plain), 8.61 KB, created by
Nick Clemens (kidclamp)
on 2023-05-05 12:32:40 UTC
(
hide
)
Description:
Bug 32735: Add endpoint to list authorities
Filename:
MIME Type:
Creator:
Nick Clemens (kidclamp)
Created:
2023-05-05 12:32:40 UTC
Size:
8.61 KB
patch
obsolete
>From 52037b338529fe9c8593a7c88bd2146e8de9f129 Mon Sep 17 00:00:00 2001 >From: Agustin Moyano <agustinmoyano@theke.io> >Date: Tue, 28 Feb 2023 11:05:34 -0300 >Subject: [PATCH] Bug 32735: Add endpoint to list authorities > >This patch adds an endpoint to list authorities > >To test: >1. apply patch >2. enable basic auth >3. call to GET /api/v1/authorities 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/api/v1/authorities.t >6. sign off > >Signed-off-by: David Nind <david@davidnind.com> >Signed-off-by: Nick Clemens <nick@bywatersolutions.com> >--- > 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(-) > >diff --git a/Koha/REST/V1/Authorities.pm b/Koha/REST/V1/Authorities.pm >index 2bbab6a6a3..52244d025d 100644 >--- a/Koha/REST/V1/Authorities.pm >+++ b/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; >diff --git a/api/v1/swagger/paths/authorities.yaml b/api/v1/swagger/paths/authorities.yaml >index b2a3b7f334..2de38a5a36 100644 >--- a/api/v1/swagger/paths/authorities.yaml >+++ b/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 >diff --git a/t/db_dependent/api/v1/authorities.t b/t/db_dependent/api/v1/authorities.t >index 9524d53614..ba6eaaf186 100644 >--- a/t/db_dependent/api/v1/authorities.t >+++ b/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|<?xml version="1.0" encoding="UTF-8"?> >+<record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.loc.gov/MARC21/slim" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"> >+ <controlfield tag="001">1001</controlfield> >+ <datafield tag="110" ind1=" " ind2=" "> >+ <subfield code="9">102</subfield> >+ <subfield code="a">My Corporation</subfield> >+ </datafield> >+</record>| >+ } })->authid; >+ >+ my $authid2 = $builder->build_object({ 'class' => 'Koha::Authorities', value => { >+ marcxml => q|<?xml version="1.0" encoding="UTF-8"?> >+<record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.loc.gov/MARC21/slim" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"> >+ <controlfield tag="001">1001</controlfield> >+ <datafield tag="110" ind1=" " ind2=" "> >+ <subfield code="9">102</subfield> >+ <subfield code="a">My Corporation</subfield> >+ </datafield> >+</record>| >+ } })->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; >+}; >-- >2.30.2
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 32735
:
147492
|
147493
|
149703
|
149705
|
150615
|
150616
| 150746 |
150747