LP#1772680: Resolved caching, holds, and checkout issues
authorJaswinder Singh <Jaswinder.Singh0011@gmail.com>
Wed, 24 Oct 2018 19:12:34 +0000 (15:12 -0400)
committerChris Sharp <csharp@georgialibraries.org>
Wed, 6 Nov 2019 17:29:41 +0000 (12:29 -0500)
Signed-off-by: Jaswinder Singh <jaswinder.singh0011@gmail.com>
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI.pm
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/RBDigitalRecord.pm
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/RBDigitalSearch.pm
Open-ILS/src/templates/opac/ebook_api/rbdigital/parts/result/table.tt2

index 3876a31..2c9be3a 100644 (file)
@@ -278,10 +278,9 @@ sub _start_session {
     $logger->info("EbookAPI: _start_session: New Session request received  for $vendor.");
 
     # determine EbookAPI handler from vendor name
-    # TODO handle API versions?
     if ($vendor_handlers{$vendor}) {
         $module = $vendor_handlers{$vendor};
-        $logger->debug("EbookAPI: _start_session: Module '$module' ");
+        $logger->info("EbookAPI: _start_session: Module '$module' ");
     } else {
         $logger->error("EbookAPI: No handler module found for $vendor!");
         return;
@@ -306,7 +305,7 @@ sub _start_session {
     # our "session" is actually just our handler object, serialized and cached
     $cache->put_cache($handler->{session_id}, $handler, $cache_timeout);
 
-    $logger->debug("EbookAPI: _start_session: New session created for '$vendor' : " .$handler->{session_id});
+    $logger->info("EbookAPI: _start_session: New session created for '$vendor' : " .$handler->{session_id});
 
     return $handler->{session_id};
 }
@@ -833,7 +832,7 @@ __PACKAGE__->register_method(
 sub _get_patron_xacts {
     my ($xact_type, $auth, $session_id, $barcode) = @_;
 
-    $logger->info("EbookAPI: _get_patron_xacts: $xact_type for session_id: $session_id");
+    $logger->debug("EbookAPI: _get_patron_xacts: Requested $xact_type for session_id: $session_id ");
 
     # verify that user is authenticated in EG
     my $e = new_editor(authtoken => $auth);
@@ -843,14 +842,16 @@ sub _get_patron_xacts {
     }
     
     # Check if there is cached object before making calls to an API
-    $logger->info("EbookAPI: _get_patron_xacts: Calling cached session for ".$xact_type);
+    $logger->debug("EbookAPI: _get_patron_xacts: Calling cached session for $xact_type: ($session_id)");
     my $xact_value = _get_cache_value($session_id, $xact_type);
 
     if (defined($xact_value)) {
-        $logger->info("EbookAPI: _get_patron_xacts: Returning cached session for $xact_type :". $xact_value);
+        $logger->debug("EbookAPI: _get_patron_xacts: Returning cached session for $xact_type: $xact_value ($session_id)");
         return $xact_value;
     }
 
+    $logger->debug("EbookAPI: _get_patron_xacts: No cached session found for $xact_type: (($handler)");
+
     my $handler = new_handler($session_id, retrieve_session($session_id));
 
     my $user_token = _get_user_token($handler, $e, $barcode);
@@ -864,6 +865,8 @@ sub _get_patron_xacts {
         $logger->error("EbookAPI: invalid transaction type '$xact_type'");
         return;
     }
+    
+    $logger->debug("EbookAPI: _get_patron_xacts: Response received for $xact_type ($handler): ".Dumper($xacts));
 
     if (defined($xacts)) {
         # cache and return transaction details
@@ -915,7 +918,7 @@ sub get_patron_xacts {
     } elsif ($self->api_name =~ /holds/) {
         $xact_type = 'holds';
     }
-
+    $logger->info("EbookAPI: get_patron_xacts request received to to get $xact_type for $session_id");
     return _get_patron_xacts($xact_type, $auth, $session_id, $barcode);
 }
 
index 62aca2e..4d9f0ce 100644 (file)
@@ -469,18 +469,22 @@ sub load_rbdigital_common {
     # Load vendor tabs for authenticated patrons only
     if (defined($self->editor->requestor)) {
         my $session_id = "";
+
+        $session_id = _get_ebook_session_id($self->ctx->{authtoken}, $self->editor->requestor->home_ou);
+        $logger->debug("EbookAPI: load_rbdigital_common: Retrieved new session: $session_id");
+
         # Get cached session based on authtoken that contains the ebook session id
-        my $cached_session = $cache->get_cache($self->ctx->{authtoken}) || undef;
-        $logger->debug("EbookAPI: load_rbdigital_common: Cached session retrieved: ".Dumper($cached_session));
-        if ($cached_session) {
-            $session_id = $cached_session;
-            $logger->debug("EbookAPI: load_rbdigital_common: Retrieved cached session_id: $session_id");
-        } else {
-            $session_id = _get_ebook_session_id($session_id, $self->editor->requestor->home_ou);
-            $logger->debug("EbookAPI: load_rbdigital_common: Retrieved new session: $session_id");
-            # Save session id into cache
-            $cache->put_cache($self->ctx->{authtoken}, $session_id, $cache_timeout);
-        }
+        my $cached_session = $cache->get_cache($self->ctx->{authtoken}) || undef;
+        $logger->debug("EbookAPI: load_rbdigital_common: Cached session retrieved: ".Dumper($cached_session));
+        if ($cached_session) {
+            $session_id = $cached_session;
+            $logger->debug("EbookAPI: load_rbdigital_common: Retrieved cached session_id: $session_id");
+        } else {
+            $session_id = _get_ebook_session_id($session_id, $self->editor->requestor->home_ou);
+            $logger->debug("EbookAPI: load_rbdigital_common: Retrieved new session: $session_id");
+            # Save session id into cache
+            $cache->put_cache($self->ctx->{authtoken}, $session_id, $cache_timeout);
+        }
 
         # Assign session id to global variable so it can be saved to cookie as well.
         $ebook_rbdigital_session_id = $session_id;
index 085c2c0..cfd5a0a 100644 (file)
@@ -49,7 +49,7 @@ sub load_rbdigital_record {
         unless $isbn and $isbn =~ /^\d+$/;
 
     # Call sub to get the session id
-    my $session_id = _get_ebook_session_id($self->cgi->cookie(COOKIE_EBOOK_RBDIGITAL), $self->editor->requestor->home_ou);
+    my $session_id = _get_ebook_session_id($self->ctx->{authtoken},$self->editor->requestor->home_ou);
 
     $ctx->{title_info} = _get_ebook_title_info($session_id, $isbn);
 
index b028f19..2f25429 100644 (file)
@@ -22,6 +22,7 @@ use OpenSRF::Utils::Logger qw/$logger/;
 use OpenILS::Utils::CStoreEditor qw/:funcs/;
 use OpenILS::Utils::Fieldmapper;
 use OpenILS::Application::AppUtils;
+use OpenSRF::Utils::Cache;
 use OpenSRF::Utils::JSON;
 use Data::Dumper;
 $Data::Dumper::Indent = 0;
@@ -32,14 +33,16 @@ use constant COOKIE_EBOOK_RBDIGITAL => 'rbdigital';
 use constant EBOOK_API_VENDOR => 'rbdigital';
 use constant EBOOK_API_OU => 1;
 
+my $cache;
+my $cache_timeout;
+
 # This method will load the admin area of the Advanced Search Tab Content
 sub load_rbdigital_advanced {
     my $self = shift;
     $self->ctx->{search_filters} = {};
     $logger->info('EbookAPI: RBDigitalSearch calling an API to get search filters');
-
-    # Call sub to get the session id
-    my $session_id = _get_ebook_session_id($self->cgi->cookie(COOKIE_EBOOK_RBDIGITAL),$self->editor->requestor->home_ou);
+    
+    my $session_id = _get_ebook_session_id($self->ctx->{authtoken},$self->editor->requestor->home_ou);
 
     my $ebook_api = OpenSRF::AppSession->create('open-ils.ebook_api');
     my $api_request = $ebook_api->request('open-ils.ebook_api.get_search_filters', $self->editor->authtoken, $session_id);
@@ -78,7 +81,7 @@ sub load_rbdigital_manage_search_fields {
     $logger->info('EbookAPI: RBDigitalSearch calling an API to search fields for mapping with evergreen');
 
     # Call sub to get the session id
-    my $session_id = _get_ebook_session_id($self->cgi->cookie(COOKIE_EBOOK_RBDIGITAL),$self->editor->requestor->home_ou);
+    my $session_id = _get_ebook_session_id($self->ctx->{authtoken},$self->editor->requestor->home_ou);
 
     my $ebook_api = OpenSRF::AppSession->create('open-ils.ebook_api');
     my $api_request = $ebook_api->request('open-ils.ebook_api.get_search_filters', $self->editor->authtoken, $session_id);
@@ -121,7 +124,6 @@ sub load_rbdigital_results {
     my $e = $self->editor;
     my $record_ids;
     my $results;
-    my $session_id;
     my $is_eg_advanced_search = $cgi->param('_adv') || 0;
 
     $logger->info('EbookAPI: RBDigitalSearch started');
@@ -130,7 +132,7 @@ sub load_rbdigital_results {
     $logger->info('EbookAPI: RBDigitalSearch User is authenticated.');
 
     # Call sub to get the session id
-    $session_id = _get_ebook_session_id($self->cgi->cookie(COOKIE_EBOOK_RBDIGITAL),$self->editor->requestor->home_ou);
+    my $session_id = _get_ebook_session_id($self->ctx->{authtoken},$self->editor->requestor->home_ou);
 
     # Read all the parameters passed
     my $auth_session = $cgi->cookie('ses') || $cgi->param('ses');
@@ -335,7 +337,8 @@ sub _get_patron_holds {
     my $holds_req = $ebookapi_session->request('open-ils.ebook_api.patron.get_holds',$authtoken, $session_id, $barcode)->recv();
 
     my %holds_ref;
-    
+    $logger->info("EbookAPI: RBDigitalSearch Holds response 1: " . Dumper($holds_req));
+
     if (defined($holds_req) && $holds_req->content ne '') {
         $logger->info('EbookAPI: RBDigitalSearch Patron Holds API call was successful');
         # Loop over the holds contents and build hash array for easier to use in the UI: { isbn => '1', ...}
@@ -360,7 +363,7 @@ sub _get_patron_checkouts {
     my %checkouts_ref;
     
     if (defined($checkouts_req) && $checkouts_req->content ne '') {
-        $logger->info('EbookAPI: EbookAPI: RBDigitalSearch Patron Checkouts API call was successful');
+        $logger->info('EbookAPI: RBDigitalSearch Patron Checkouts API call was successful');
         # Loop over the checkout contents and build hash array for easier to use in the UI: { isbn => '1', ...}
         $logger->debug("EbookAPI: Checkout response: " . Dumper($checkouts_req->content));
         foreach my $value (@{$checkouts_req->content}) {
@@ -421,14 +424,32 @@ sub _get_patron_id {
 }
 
 sub _get_ebook_session_id {
-    my ($session_id, $home_ou) = @_;
-    $logger->info("EbookAPI: _get_ebook_session_id home library: $home_ou ");
-
-    # Create an instance of EbookAPI
-    my $ebook_api = OpenSRF::AppSession->create('open-ils.ebook_api');
-    my $session_id_req = $ebook_api->request(
-        'open-ils.ebook_api.check_session', $session_id, EBOOK_API_VENDOR, $home_ou);
-    $session_id = $session_id_req->recv->content;
+    my ($authtoken, $home_ou) = @_;
+    $logger->info("EbookAPI: _get_ebook_session_id home library: $home_ou ($authtoken)");
+    my $session_id;
+    # Call sub to get the session id
+    # Get cached session based on authtoken that contains the ebook session id
+    if (defined($authtoken) && $authtoken ne '') {
+        $cache = OpenSRF::Utils::Cache->new;
+        $session_id = $cache->get_cache($authtoken) || undef;
+    }
+    
+    if ($session_id) {
+        $logger->info("EbookAPI: _get_ebook_session_id: Retrieved cached session_id: $session_id");
+    } else {
+        # Create an instance of EbookAPI
+        my $ebook_api = OpenSRF::AppSession->create('open-ils.ebook_api');
+        my $session_id_req = $ebook_api->request(
+            'open-ils.ebook_api.check_session', $session_id, EBOOK_API_VENDOR, $home_ou);
+        $session_id = $session_id_req->recv->content;
+        $logger->info("EbookAPI: _get_ebook_session_id: Received new session_id: $session_id");
+        
+        
+        my $sclient = OpenSRF::Utils::SettingsClient->new();
+        $cache_timeout = $sclient->config_value("apps", "open-ils.ebook_api", "app_settings", "cache_timeout" ) || 300;
+        # Save session id into cache
+        $cache->put_cache($authtoken, $session_id, $cache_timeout);
+    }
 
     return $session_id;
 }
index c0e4c53..5f8e99d 100644 (file)
@@ -1,7 +1,7 @@
 [%  PROCESS "opac/parts/misc_util.tt2";
     PROCESS "opac/ebook_api/rbdigital/parts/file_formats.tt2";
     USE ResolverResolver;
-
+    USE Dumper;
     ctx.result_start = 1 + ctx.page_size * page;
     ctx.result_stop = ctx.page_size * (page + 1);
     IF ctx.result_stop > ctx.hit_count; ctx.result_stop = ctx.hit_count; END;
@@ -12,6 +12,7 @@
 [% ctx.results_count_header = PROCESS results_count_header; 
    ctx.results_count_header;
 %]
+[% Dumper.dump_html(ctx.holds) %]
 
 [% IF ctx.bookbag %]
 <div id="result-bookbag-heading">