Bugzilla – Attachment 183811 Details for
Bug 40308
New Perl Class (Koha::ExternalContent::OCLC) for integrating with OCLC's APIs
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 40308: add new Perl Class Koha::ExternalContent::OCLC
Bug-40308-add-new-Perl-Class-KohaExternalContentOC.patch (text/plain), 13.51 KB, created by
Andreas Roussos
on 2025-07-07 00:17:28 UTC
(
hide
)
Description:
Bug 40308: add new Perl Class Koha::ExternalContent::OCLC
Filename:
MIME Type:
Creator:
Andreas Roussos
Created:
2025-07-07 00:17:28 UTC
Size:
13.51 KB
patch
obsolete
>From 781e0a4fa61787f5d15167dd812c7aba24180063 Mon Sep 17 00:00:00 2001 >From: Andreas Roussos <a.roussos@dataly.gr> >Date: Mon, 7 Jul 2025 02:39:57 +0300 >Subject: [PATCH] Bug 40308: add new Perl Class Koha::ExternalContent::OCLC > >OCLC are offering a number of web APIs, this is an attempt >to collect utility functions in a new Perl Class. > >At the moment, the Class contains the following functions, >that allow interaction with the OCLC Dewey Linked Data API: > >- get_access_token >- get_ddc_uri >- get_ddc_url >- get_ddc_description > >Test plan: > >1) Inside a KTD instance (MARC flavour does not play a role), > run `perldoc -o html Koha::ExternalContent::OCLC > OCLC.html` > and view the documentation for this class (OCLC.html) in your > web browser. Ensure everything makes sense ;-) >--- > Koha/ExternalContent/OCLC.pm | 422 +++++++++++++++++++++++++++++++++++ > 1 file changed, 422 insertions(+) > create mode 100644 Koha/ExternalContent/OCLC.pm > >diff --git a/Koha/ExternalContent/OCLC.pm b/Koha/ExternalContent/OCLC.pm >new file mode 100644 >index 0000000000..f18d1fa9c9 >--- /dev/null >+++ b/Koha/ExternalContent/OCLC.pm >@@ -0,0 +1,422 @@ >+package Koha::ExternalContent::OCLC; >+ >+# This file is part of Koha. >+# >+# Copyright (C) 2025 Dataly Tech >+# >+# 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 <http://www.gnu.org/licenses>. >+ >+use Modern::Perl; >+ >+use C4::Context; >+ >+use Koha::Caches; >+ >+use Encode qw( decode ); >+use JSON qw( from_json ); >+use LWP::UserAgent; >+use MIME::Base64 qw( encode_base64 ); >+ >+=head1 NAME >+ >+Koha::ExternalContent::OCLC >+ >+=head1 SYNOPSIS >+ >+A class that provides helper functions related to OCLC's APIs. >+ >+For more information on these APIs, please visit the following pages: >+ >+L<https://www.oclc.org/developer/api/oclc-apis.en.html> >+ >+L<https://developer.api.oclc.org/> >+ >+=head1 DESCRIPTION >+ >+This class contains common functions used when accessing OCLC's APIs. >+ >+Examples of such APIs include, but are not limited to: >+ >+=over >+ >+=item * Citations API >+ >+=item * Dewey Linked Data API >+ >+=item * Dewey Lookup API >+ >+=item * Discovery Library Profiles API >+ >+=item * Entity Search API >+ >+=item * FAST API >+ >+=item * New Titles API >+ >+=item * Suggestions API >+ >+=item * VIAF API >+ >+=item * WorldCat Discovery API >+ >+=item * WorldCat Entity Data >+ >+=item * WorldCat Metadata API >+ >+=back >+ >+Currently, this class has functions that allow use of the Dewey Linked Data and Dewey Lookup APIs. >+ >+=head1 FUNCTIONS >+ >+=head3 get_access_token >+ >+ my $access_token = get_access_token( >+ { >+ scope => $scope, >+ client_id => $client_id, >+ client_secret => $client_secret, >+ } >+ ); >+ >+Requests an API access token from OCLC's OAuth 2.0 server, for a particular scope (such as C<deweyLinkedData>, C<WorldCatMetadataAPI>, etc.). >+ >+Tokens are valid for a specific time period, and to ensure re-usability within this timeframe they will be cached (in C<memcached>) using a unique key per scope and with an appropriate expiry set. >+ >+This request follows the Client Credentials Grant OAuth pattern, documented here: L<https://www.oclc.org/developer/api/keys/oauth/client-credentials-grant.en.html> >+ >+Essentially, an HTTP POST request to C<https://oauth.oclc.org/token> is performed, with the Base64-encoded Client ID and Client Secret components included in an C<Authorization: Basic> header. >+ >+WSKeys (or Web Service Keys) are documented here: L<https://www.oclc.org/developer/develop/authentication/what-is-a-wskey.en.html> >+ >+Instructions on how to request such a key can be found here: L<https://help.oclc.org/Librarian_Toolbox/OCLC_APIs/Get_started/Request_an_API_WSKey> >+ >+=head4 Parameters >+ >+=over 4 >+ >+=item C<options> (required) - a hashref containing the following keys: >+ >+=over 8 >+ >+=item C<scope> (string, required) - the service (i.e. scope) for which the client is requesting access (e.g. C<deweyLinkedData>, C<WorldCatMetadataAPI>, etc.) >+ >+=item C<client_id> (string, optional) - the Client ID component of your WSKey (falls back to the contents of the C<OCLCAPIWSKeyClientID> System Preference) >+ >+=item C<client_secret> (string, optional) - the Client Secret component of your WSKey (falls back to the contents of the C<OCLCAPIWSKeyClientSecret> System Preference) >+ >+=back >+ >+=back >+ >+=head4 Returns >+ >+=over 4 >+ >+=item C<access_token> (string) - the API access token, or C<undef> if no token could be obtained >+ >+=item C<msg> (string) - the source of the token (API, cache), or the error message returned if no token could be obtained >+ >+=back >+ >+=head4 Related System Preferences >+ >+=over 4 >+ >+=item C<OCLCAPIWSKeyClientID> >+ >+=item C<OCLCAPIWSKeyClientSecret> >+ >+=back >+ >+=cut >+ >+sub get_access_token { >+ >+ my ( $self, $args ) = @_; >+ >+ my $scope = $args->{scope}; >+ my $client_id = $args->{client_id} ||= C4::Context->preference('OCLCAPIWSKeyClientID'); >+ my $client_secret = $args->{client_secret} ||= C4::Context->preference('OCLCAPIWSKeyClientSecret'); >+ my $cache = Koha::Caches->get_instance; >+ >+ my $cached_access_token = $cache->get_from_cache( 'OCLC_access_token-' . $scope ); >+ if ($cached_access_token) { >+ return ( $cached_access_token->{token}, 'cache' ); >+ } else { >+ my $basic_auth = encode_base64( "$client_id:$client_secret", '' ); >+ my $ua = LWP::UserAgent->new; >+ my $method = 'POST'; >+ my $uri = "https://oauth.oclc.org/token?grant_type=client_credentials&scope=$scope"; >+ my $header = [ 'Authorization' => "Basic $basic_auth" ]; >+ my $request = HTTP::Request->new( $method, $uri, $header ); >+ my $response = $ua->request($request); >+ my $content = Encode::decode( "utf8", $response->content ); >+ my $json = from_json($content); >+ >+ if ( $response->is_success ) { >+ $cache->set_in_cache( >+ "OCLC_access_token-$scope", >+ { >+ token => $json->{access_token}, >+ expires_at => $json->{expires_at}, >+ }, >+ { expiry => $json->{expires_in} } >+ ); >+ return ( $json->{access_token}, 'API' ); >+ } else { >+ return ( undef, $json->{code} . ' ' . $json->{message} ); >+ } >+ } >+ >+} >+ >+=head3 get_ddc_uri >+ >+ my $ddc_uri = get_ddc_uri( >+ { >+ access_token => $access_token, >+ ddc_number => $ddc_number, >+ } >+ ); >+ >+Requests a Linked Data ID (URI) for a particular DDC number from OCLC's Dewey Linked Data API. >+ >+The API is documented here: L<https://developer.api.oclc.org/dewey-ld> >+ >+=head4 Parameters >+ >+=over 4 >+ >+=item C<options> (required) - a hashref containing the following keys: >+ >+=over 8 >+ >+=item C<access_token> (string, required) - the access_token to use when making the API request >+ >+=item C<ddc_number> (string, required) - the DDC number to look up >+ >+=back >+ >+=back >+ >+=head4 Returns >+ >+=over 4 >+ >+=item C<uri> (string) - the Linked Data ID (URI) associated with the DDC number requested, or C<undef> if no URI could be obtained >+ >+=item C<msg> (string) - the source of the URI (API, cache), or the error message returned if no token could be obtained >+ >+=back >+ >+=cut >+ >+sub get_ddc_uri { >+ >+ my ( $self, $args ) = @_; >+ >+ my $access_token = $args->{access_token}; >+ my $ddc_number = $args->{ddc_number}; >+ my $cache = Koha::Caches->get_instance; >+ >+ my $cached_uri = $cache->get_from_cache("$ddc_number-uri"); >+ if ($cached_uri) { >+ return ( $cached_uri, 'cache' ); >+ } else { >+ my $ua = LWP::UserAgent->new; >+ my $method = 'GET'; >+ my $url = "https://id.oclc.org/worldcat/ddc/api/id?ddc=$ddc_number"; >+ my $header = [ 'Authorization' => "Bearer $access_token" ]; >+ my $request = HTTP::Request->new( $method, $url, $header ); >+ my $response = $ua->request($request); >+ my $content = Encode::decode( "utf8", $response->content ); >+ my $json = from_json($content); >+ >+ if ( $response->is_success ) { >+ if ( $json->{$ddc_number} ) { >+ $cache->set_in_cache( "$ddc_number-uri", $json->{$ddc_number} ); >+ return ( $json->{$ddc_number}, 'API' ); >+ } else { >+ return ( undef, 'not found' ); >+ } >+ } else { >+ return ( undef, $json->{message} ); >+ } >+ } >+ >+} >+ >+=head3 get_ddc_url >+ >+ my $ddc_url = get_ddc_url( >+ { >+ access_token => $access_token, >+ ddc_number => $ddc_number, >+ } >+ ); >+ >+Requests a Linked Data URL for a particular DDC number from OCLC's Dewey Linked Data API. >+ >+The API is documented here: L<https://developer.api.oclc.org/dewey-ld> >+ >+=head4 Parameters >+ >+=over 4 >+ >+=item C<options> (required) - a hashref containing the following keys: >+ >+=over 8 >+ >+=item C<access_token> (string, required) - the access_token to use when making the API request >+ >+=item C<ddc_number> (string, required) - the DDC number to look up >+ >+=back >+ >+=back >+ >+=head4 Returns >+ >+=over 4 >+ >+=item C<url> (string) - the Linked Data URL associated with the DDC number requested, or C<undef> if no URL could be obtained >+ >+=item C<msg> (string) - the source of the URL (API, cache), or the error message returned if no token could be obtained >+ >+=back >+ >+=cut >+ >+sub get_ddc_url { >+ >+ my ( $self, $args ) = @_; >+ >+ my $access_token = $args->{access_token}; >+ my $ddc_number = $args->{ddc_number}; >+ my $cache = Koha::Caches->get_instance; >+ >+ my $cached_url = $cache->get_from_cache("$ddc_number-url"); >+ if ($cached_url) { >+ return ( $cached_url, 'cache' ); >+ } else { >+ my $ua = LWP::UserAgent->new; >+ my $method = 'GET'; >+ my $url = "https://id.oclc.org/worldcat/ddc/api/url?ddc=$ddc_number"; >+ my $header = [ 'Authorization' => "Bearer $access_token" ]; >+ my $request = HTTP::Request->new( $method, $url, $header ); >+ my $response = $ua->request($request); >+ my $content = Encode::decode( "utf8", $response->content ); >+ my $json = from_json($content); >+ >+ if ( $response->is_success ) { >+ $cache->set_in_cache( "$ddc_number-url", $json->{$ddc_number} ); >+ return ( $json->{$ddc_number}, 'API' ); >+ } else { >+ return ( undef, $json->{message} ); >+ } >+ } >+ >+} >+ >+=head3 get_ddc_description >+ >+ my $ddc_description = get_ddc_description( >+ { >+ access_token => $access_token, >+ ddc_uri => $ddc_uri, >+ [ language => $language, ] >+ } >+ ); >+ >+Requests a Linked Data Description for a particular DDC number from OCLC's Dewey Linked Data API. >+ >+The API is documented here: L<https://developer.api.oclc.org/dewey-ld> >+ >+=head4 Parameters >+ >+=over 4 >+ >+=item C<options> (required) - a hashref containing the following keys: >+ >+=over 8 >+ >+=item C<access_token> (string, required) - the access_token to use when making the API request >+ >+=item C<ddc_uri> (string, required) - the DDC URI to look up >+ >+=item C<language> (string, optional) - the language of the description to obtain (falls back to the setting of the C<OCLCDeweyLinkedDataLanguage> System Preference) >+ >+=back >+ >+=back >+ >+=head4 Returns >+ >+=over 4 >+ >+=item C<description> (string) - the Linked Data Description associated with the DDC URI requested, or C<undef> if no Description could be obtained >+ >+=item C<msg> (string) - the source of the LD Description (API, cache), or the error message returned if no token could be obtained >+ >+=back >+ >+=head4 Related System Preferences >+ >+=over 4 >+ >+=item C<OCLCDeweyLinkedDataLanguage> >+ >+=back >+ >+=cut >+ >+sub get_ddc_description { >+ >+ my ( $self, $args ) = @_; >+ >+ my $access_token = $args->{access_token}; >+ my $ddc_uri = $args->{ddc_uri}; >+ my $language = $args->{language} ||= C4::Context->preference('OCLCDeweyLinkedDataLanguage'); >+ my $cache = Koha::Caches->get_instance; >+ >+ my $cached_description = $cache->get_from_cache("$ddc_uri-description-$language"); >+ if ($cached_description) { >+ return ( $cached_description, 'cache' ); >+ } else { >+ my $ua = LWP::UserAgent->new; >+ my $method = 'GET'; >+ my $url = "https://id.oclc.org/worldcat/ddc/$ddc_uri"; >+ my $header = [ 'Authorization' => "Bearer $access_token" ]; >+ my $request = HTTP::Request->new( $method, $url, $header ); >+ my $response = $ua->request($request); >+ my $content = Encode::decode( "utf8", $response->content ); >+ my $json = from_json($content); >+ >+ if ( $response->is_success ) { >+ >+ # Some DDC numbers (e.g. 348.481) do not have descriptions in all >+ # possible languages. We take that into account here. >+ my $description = $json->{'prefLabel'}->{$language} || "No description found for language code '$language'"; >+ $cache->set_in_cache( "$ddc_uri-description-$language", $description ); >+ return ( $description, 'API' ); >+ >+ } else { >+ return ( undef, $json->{message} ); >+ } >+ } >+ >+} >+ >+1; >-- >2.39.5
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 40308
:
183809
|
183810
| 183811 |
183812
|
183813