Bug 29953 - Possible inefficiency in REST API for biblios endpoint
Summary: Possible inefficiency in REST API for biblios endpoint
Status: NEW
Alias: None
Product: Koha
Classification: Unclassified
Component: REST API (show other bugs)
Version: Main
Hardware: All All
: P5 - low enhancement (vote)
Assignee: Bugs List
QA Contact:
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-01-27 02:29 UTC by David Cook
Modified: 2022-01-27 02:30 UTC (History)
1 user (show)

See Also:
Change sponsored?: ---
Patch complexity: ---
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Cook 2022-01-27 02:29:54 UTC
I was looking at Koha/REST/V1/Biblios.pm because of bug 28201 when I noticed it looks like we're running $record->$method calls more than we need to in the "get" function.

Consider the following block of code:

        if ( $c->req->headers->accept =~ m/application\/json/ ) {
            return $c->render(
                status => 200,
                json   => $biblio->to_api
            );
        }
        else {
            my $record = $biblio->metadata->record;

            $c->respond_to(
                marcxml => {
                    status => 200,
                    format => 'marcxml',
                    text   => $record->as_xml_record
                },
                mij => {
                    status => 200,
                    format => 'mij',
                    text   => $record->to_mij
                },
                marc => {
                    status => 200,
                    format => 'marc',
                    text   => $record->as_usmarc
                },
                txt => {
                    status => 200,
                    format => 'text/plain',
                    text   => $record->as_formatted
                },
                any => {
                    status  => 406,
                    openapi => [
                        "application/json",
                        "application/marcxml+xml",
                        "application/marc-in-json",
                        "application/marc",
                        "text/plain"
                    ]
                }
            );
        }

If there is an Accept header for application/json, then we run $biblio->to_api, and that's it.

However, if there isn't an Accept header for application/json, it looks like we create a hash of hashrefs that output the $record in 4 different formats. 

I think it would make more sense (based on what I see in https://docs.mojolicious.org/Mojolicious/Plugin/DefaultHelpers#respond_to) to provide anonymous functions instead of hashrefs so that $record->$method calls are only run when a specific format is actually chosen by Mojolicious. 

In terms of performance, it might be only a marginal gain, but if you're doing a lot of API calls, small gains can translate into large scale time savings.