From 781e0a4fa61787f5d15167dd812c7aba24180063 Mon Sep 17 00:00:00 2001 From: Andreas Roussos 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 . + +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 + +L + +=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, C, etc.). + +Tokens are valid for a specific time period, and to ensure re-usability within this timeframe they will be cached (in C) using a unique key per scope and with an appropriate expiry set. + +This request follows the Client Credentials Grant OAuth pattern, documented here: L + +Essentially, an HTTP POST request to C is performed, with the Base64-encoded Client ID and Client Secret components included in an C header. + +WSKeys (or Web Service Keys) are documented here: L + +Instructions on how to request such a key can be found here: L + +=head4 Parameters + +=over 4 + +=item C (required) - a hashref containing the following keys: + +=over 8 + +=item C (string, required) - the service (i.e. scope) for which the client is requesting access (e.g. C, C, etc.) + +=item C (string, optional) - the Client ID component of your WSKey (falls back to the contents of the C System Preference) + +=item C (string, optional) - the Client Secret component of your WSKey (falls back to the contents of the C System Preference) + +=back + +=back + +=head4 Returns + +=over 4 + +=item C (string) - the API access token, or C if no token could be obtained + +=item C (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 + +=item C + +=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 + +=head4 Parameters + +=over 4 + +=item C (required) - a hashref containing the following keys: + +=over 8 + +=item C (string, required) - the access_token to use when making the API request + +=item C (string, required) - the DDC number to look up + +=back + +=back + +=head4 Returns + +=over 4 + +=item C (string) - the Linked Data ID (URI) associated with the DDC number requested, or C if no URI could be obtained + +=item C (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 + +=head4 Parameters + +=over 4 + +=item C (required) - a hashref containing the following keys: + +=over 8 + +=item C (string, required) - the access_token to use when making the API request + +=item C (string, required) - the DDC number to look up + +=back + +=back + +=head4 Returns + +=over 4 + +=item C (string) - the Linked Data URL associated with the DDC number requested, or C if no URL could be obtained + +=item C (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 + +=head4 Parameters + +=over 4 + +=item C (required) - a hashref containing the following keys: + +=over 8 + +=item C (string, required) - the access_token to use when making the API request + +=item C (string, required) - the DDC URI to look up + +=item C (string, optional) - the language of the description to obtain (falls back to the setting of the C System Preference) + +=back + +=back + +=head4 Returns + +=over 4 + +=item C (string) - the Linked Data Description associated with the DDC URI requested, or C if no Description could be obtained + +=item C (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 + +=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