Bug 29953

Summary: Possible inefficiency in REST API for biblios endpoint
Product: Koha Reporter: David Cook <dcook>
Component: REST APIAssignee: Bugs List <koha-bugs>
Status: NEW --- QA Contact:
Severity: enhancement    
Priority: P5 - low CC: tomascohen
Version: Main   
Hardware: All   
OS: All   
See Also: https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=28201
Change sponsored?: --- Patch complexity: ---
Documentation contact: Documentation submission:
Text to go in the release notes:
Version(s) released in:
Circulation function:

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.