Skip to content

Commit

Permalink
ExternalVuFind: Cache results
Browse files Browse the repository at this point in the history
  • Loading branch information
maccabeelevine committed Aug 22, 2024
1 parent a6f665d commit ecd7687
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
27 changes: 15 additions & 12 deletions module/VuFind/src/VuFind/Connection/ExternalVuFind.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@
*/
class ExternalVuFind implements
\VuFindHttp\HttpServiceAwareInterface,
\VuFind\Http\CachingDownloaderAwareInterface,
LoggerAwareInterface
{
use \VuFindHttp\HttpServiceAwareTrait;
use \VuFind\Http\CachingDownloaderAwareTrait;
use \VuFind\Log\LoggerAwareTrait;

/**
Expand All @@ -55,6 +57,14 @@ class ExternalVuFind implements
*/
protected $baseUrl = null;

/**
* Constructor
*/
public function __construct()
{
$this->cacheOptionsSection = 'ExternalVuFind';
}

/**
* Set the API base URL.
*
Expand Down Expand Up @@ -87,6 +97,9 @@ public function search(
$this->logError('Must call setBaseUrl() before searching.');
return [];
}
if (!isset($this->cachingDownloader)) {
throw new \Exception('CachingDownloader initialization failed.');
}

$params = [];
$params[] = $requestParam . '=' . urlencode($queryString);
Expand All @@ -97,7 +110,7 @@ public function search(
}

try {
$response = $this->httpService->get($this->baseUrl . '/search', $params);
$arr = $this->cachingDownloader->downloadJson($this->baseUrl . '/search', $params, true);
} catch (Exception $ex) {
$this->logError(
'Exception during request: ' .
Expand All @@ -106,16 +119,6 @@ public function search(
return [];
}

if ($response->isServerError()) {
$this->logError(
'ExternalVuFind API HTTP Error: ' .
$response->getStatusCode()
);
return [];
}

$responseData = trim($response->getBody());
$arr = json_decode($responseData, true);
return $arr ?? [];
}
}
}
8 changes: 4 additions & 4 deletions module/VuFind/src/VuFind/Http/CachingDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ public function download(
*
* @return stdClass
*/
public function downloadJson($url, $params = [])
public function downloadJson($url, $params = [], $associative = null)
{
$decodeJson = function (\Laminas\Http\Response $response, $url) {
$decodedJson = json_decode($response->getBody());
$decodeJson = function (\Laminas\Http\Response $response, $url) use ($associative) {
$decodedJson = json_decode($response->getBody(), $associative);
if ($decodedJson === null) {
throw new HttpDownloadException(
'Invalid response body',
Expand All @@ -211,4 +211,4 @@ public function downloadJson($url, $params = [])

return $this->download($url, $params, $decodeJson);
}
}
}
15 changes: 13 additions & 2 deletions module/VuFind/src/VuFind/Recommend/ConsortialVuFindFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,20 @@ public function __invoke(
throw new \Exception('Unexpected options passed to factory.');
}

$externalVuFind = $container->get(\VuFind\Connection\ExternalVuFind::class);

// Doing the construction of CachingDownloader explicitly until it is not a singleton
// $cachingDownloader = $container->get(\VuFind\Http\CachingDownloader::class);
$cachingDownloader = new \VuFind\Http\CachingDownloader(
$container->get(\VuFind\Cache\Manager::class),
$container->get(\VuFind\Config\PluginManager::class),
);
$cachingDownloader->setHttpService($container->get(\VuFindHttp\HttpService::class));

$externalVuFind->setCachingDownloader($cachingDownloader);
return new $requestedName(
$container->get(\VuFind\Config\PluginManager::class)->get('ExternalVuFind'),
$container->get(\VuFind\Connection\ExternalVuFind::class)
$externalVuFind
);
}
}
}

0 comments on commit ecd7687

Please sign in to comment.