From db4575842fcb5ca15cb4b303dea6029915eafc0d Mon Sep 17 00:00:00 2001 From: Shi Yao Wang Date: Fri, 12 Jan 2024 12:20:26 -0500 Subject: [PATCH] Bug 35797: REST API Add GET route for patronimage Api changes adding GET route for patronimage Test plan: 1- Apply the patch 2- Go to administration -> sys.pref. -> set patronimages to allow 3- Log in koha using a user with edit_borrowers privileges 4- Have a patron with a profile image (add an image if necessary by searching for a patron and then clicking on the default profile image on the top left) 5- Open a new browser tab, write {intranet_url}/api/v1/patrons/{borrowernumber}/image where {intranet_url} is the base intranet url of koha and {borrowernumber} is the borrowernumber of a borrower that has an image 6- Notice the response is a json with 3 keys: imagefile, mimetype and patron_id which corresponds to the fields of patronimage table in the database. Note that imagefile in db and in json response are different because it has been base64 encoded in order to be sent as a string in json 7- Do step 5 with a borrowernumber of a patron without an image and a borrowernumber of a patron that doesn't exist (and other edge cases where there should be an error message displayed such as "Access forbidden", "Authentication required", etc.) 8- Notice an appropriate error message is displayed --- Koha/REST/V1/Patrons/Image.pm | 78 ++++++++++++++++++++ api/v1/swagger/definitions/patron_image.yaml | 14 ++++ api/v1/swagger/paths/patrons_image.yaml | 43 +++++++++++ api/v1/swagger/swagger.yaml | 4 + 4 files changed, 139 insertions(+) create mode 100644 Koha/REST/V1/Patrons/Image.pm create mode 100644 api/v1/swagger/definitions/patron_image.yaml create mode 100644 api/v1/swagger/paths/patrons_image.yaml diff --git a/Koha/REST/V1/Patrons/Image.pm b/Koha/REST/V1/Patrons/Image.pm new file mode 100644 index 0000000000..d9b53b6ae2 --- /dev/null +++ b/Koha/REST/V1/Patrons/Image.pm @@ -0,0 +1,78 @@ +package Koha::REST::V1::Patrons::Image; + +# 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 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. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Mojo::Base 'Mojolicious::Controller'; + +use Koha::Patrons; + +use Try::Tiny qw( catch try ); + +use MIME::Base64 qw( encode_base64 ); + +=head1 NAME + +Koha::REST::V1::Patrons::Image + +=head1 API + +=head2 Methods + +=head3 get + +Controller method that gets a patron's image + +=cut + +sub get { + + my $c = shift->openapi->valid_input or return; + + my $patron = Koha::Patrons->find( $c->param('patron_id') ); + + unless ($patron) { + return $c->render( + status => 404, + openapi => { error => "Patron not found." } + ); + } + + return try { + my $image = $patron->image; + + unless ($image) { + return $c->render( + status => 404, + openapi => { error => "Patron image not found." } + ); + } + + return $c->render( + status => 200, + openapi => { + patron_id => $image->borrowernumber, + mimetype => $image->mimetype, + imagefile => encode_base64($image->imagefile) + } + ); + } catch { + $c->unhandled_exception($_); + }; +} + +1; diff --git a/api/v1/swagger/definitions/patron_image.yaml b/api/v1/swagger/definitions/patron_image.yaml new file mode 100644 index 0000000000..34aef0361f --- /dev/null +++ b/api/v1/swagger/definitions/patron_image.yaml @@ -0,0 +1,14 @@ +--- +type: object +properties: + patron_id: + type: integer + description: Internal patron identifier + mimetype: + type: string + description: Patron image mimetype + imagefile: + type: string + description: Patron image raw data +required: + - patron_id diff --git a/api/v1/swagger/paths/patrons_image.yaml b/api/v1/swagger/paths/patrons_image.yaml new file mode 100644 index 0000000000..e6c69f0c02 --- /dev/null +++ b/api/v1/swagger/paths/patrons_image.yaml @@ -0,0 +1,43 @@ +--- +"/patrons/{patron_id}/image": + get: + x-mojo-to: Patrons::Image#get + operationId: getPatronImage + tags: + - patrons + summary: Get image of a patron + parameters: + - $ref: "../swagger.yaml#/parameters/patron_id_pp" + produces: + - application/json + responses: + "200": + description: Patron's image + schema: + $ref: "../swagger.yaml#/definitions/patron_image" + "401": + description: Authentication required + schema: + $ref: "../swagger.yaml#/definitions/error" + "403": + description: Access forbidden + schema: + $ref: "../swagger.yaml#/definitions/error" + "404": + description: Patron not found + schema: + $ref: "../swagger.yaml#/definitions/error" + "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: + borrowers: edit_borrowers diff --git a/api/v1/swagger/swagger.yaml b/api/v1/swagger/swagger.yaml index 46966e40b2..149c9f8f91 100644 --- a/api/v1/swagger/swagger.yaml +++ b/api/v1/swagger/swagger.yaml @@ -128,6 +128,8 @@ definitions: $ref: ./definitions/patron_balance.yaml patron_extended_attribute: $ref: ./definitions/patron_extended_attribute.yaml + patron_image: + $ref: ./definitions/patron_image.yaml preservation_config: $ref: ./definitions/preservation_config.yaml preservation_train: @@ -419,6 +421,8 @@ paths: $ref: "./paths/patrons_extended_attributes.yaml#/~1patrons~1{patron_id}~1extended_attributes~1{extended_attribute_id}" "/patrons/{patron_id}/holds": $ref: "./paths/patrons_holds.yaml#/~1patrons~1{patron_id}~1holds" + "/patrons/{patron_id}/image": + $ref: "./paths/patrons_image.yaml#/~1patrons~1{patron_id}~1image" "/patrons/{patron_id}/password": $ref: "./paths/patrons_password.yaml#/~1patrons~1{patron_id}~1password" "/patrons/{patron_id}/password/expiration_date": -- 2.34.1