[Koha-bugs] [Bug 29953] New: Possible inefficiency in REST API for biblios endpoint

bugzilla-daemon at bugs.koha-community.org bugzilla-daemon at bugs.koha-community.org
Thu Jan 27 03:29:54 CET 2022


https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=29953

            Bug ID: 29953
           Summary: Possible inefficiency in REST API for biblios endpoint
 Change sponsored?: ---
           Product: Koha
           Version: master
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P5 - low
         Component: REST API
          Assignee: koha-bugs at lists.koha-community.org
          Reporter: dcook at prosentient.com.au
                CC: tomascohen at gmail.com

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.

-- 
You are receiving this mail because:
You are the assignee for the bug.
You are watching all bug changes.


More information about the Koha-bugs mailing list