Browse Source

removed dependancy on curl and better error checking in javascript

2.x
Paul Pound 2 weeks ago
parent
commit
59e7ec79b3
  1. 3
      modules/roblib_search_eds/js/eds_results_article.js
  2. 3
      modules/roblib_search_eds/js/eds_results_book.js
  3. 125
      modules/roblib_search_eds/src/Lib/EBSCOConnector.php
  4. 3
      modules/solr_site/js/solr_site_bestbet.js
  5. 3
      modules/solr_site/js/solr_site_results.js

3
modules/roblib_search_eds/js/eds_results_article.js

@ -84,6 +84,9 @@ Drupal.behaviors.roblib_search_eds_articles = {
jQuery('#roblib-eds-articles-more-results').empty().append(upei_link);
jQuery('#roblib-eds-articles-toc').empty().append('<a href="' + proxy_url + href_str + query_str + '" id="eds-article-see-all-results-toc">Articles (' + data.recordCount + ')</a>');
}).fail(function () {
// Don't leave the spinner running if the request fails.
jQuery('#roblib-search-content-eds-articles').empty().append(Drupal.t('Results are currently unavailable.'));
});
}
}

3
modules/roblib_search_eds/js/eds_results_book.js

@ -86,6 +86,9 @@ Drupal.behaviors.roblib_search_eds = {
jQuery('#roblib-eds-books-more-results').empty().append(upei_link);
jQuery('#roblib-eds-books-toc').empty().append('<a href="' + proxy_url + href_str + query_str + '" id="eds-see-all-results-toc">Books (' + data.recordCount + ')</a>');
}).fail(function () {
// Don't leave the spinner running if the request fails.
jQuery('#roblib-search-content-eds').empty().append(Drupal.t('Results are currently unavailable.'));
});
}
}

125
modules/roblib_search_eds/src/Lib/EBSCOConnector.php

@ -1,5 +1,7 @@
<?php
namespace Drupal\roblib_search_eds\Lib;
use GuzzleHttp\Exception\GuzzleException;
/**
* @file
* The EBSCO Connector and Exception classes.
@ -429,84 +431,71 @@ class EBSCOConnector {
* @access protected
*/
protected function request($url, $params, $headers = array(), $method = 'GET') {
$xml = FALSE;
$return = FALSE;
$data = NULL;
if(!is_array($headers)){
$response = NULL;
if (!is_array($headers)) {
$headers = [];
}
$headers[] = 'Content-Type: text/xml';
// Add compression in case its not there.
array_push($headers, 'Content-Type: text/xml');
$data=$params;
// Send the request.
$response=null;
// Convert "Name: value" header strings into the array Guzzle expects.
$request_headers = [];
foreach ($headers as $header) {
[$name, $value] = array_map('trim', explode(':', $header, 2)) + [1 => ''];
$request_headers[$name] = $value;
}
$options = [
'headers' => $request_headers,
'timeout' => 10,
'allow_redirects' => ['max' => 10],
// Let us inspect 4xx/5xx bodies; EDS returns API errors as HTTP 400.
'http_errors' => FALSE,
];
try {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_MAXREDIRS, 10 );
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
switch ($method)
{
switch ($method) {
case 'GET':
if ($data) { $url = sprintf('%s?%s', $url, http_build_query($data)); }
curl_setopt($curl, CURLOPT_URL, $url);
break;
case 'POST':
if ($data) {
curl_setopt($curl,CURLOPT_POST, 1);
curl_setopt($curl,CURLOPT_POSTFIELDS, $data);
if ($params) {
$url = sprintf('%s?%s', $url, http_build_query($params));
}
break;
case 'POST':
case 'DELETE':
if ($data) {
if (count($headers)>0) {
curl_setopt($curl,CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
if ($params) {
$options['body'] = $params;
}
break;
}
$response = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
try {
$http_response = \Drupal::httpClient()->request($method, $url, $options);
$code = $http_response->getStatusCode();
$response = (string) $http_response->getBody();
}
catch (GuzzleException $e) {
// Network level failure (timeout, DNS, TLS...).
\Drupal::logger('ebsco')->error('EDS request to @url failed: @message', [
'@url' => $url,
'@message' => $e->getMessage(),
]);
return new EBSCOException('The EDS API could not be reached.');
}
switch ($code) {
case self::HTTP_OK:
$xml_str = $response;
try {
// Clean EMP namespace.
$xml_str = str_replace(array("<a:", "</a:"), array("<", "</"), $xml_str);
$xml_str = str_replace(array("<a:", "</a:"), array("<", "</"), $response);
$xml = simplexml_load_string($xml_str);
$return = $xml;
}
catch (Exception $e) {
$return = new EBSCOException($xml);
}
$return = ($xml !== FALSE) ? $xml : new EBSCOException('The EDS API returned an invalid response.');
break;
case self::HTTP_BAD_REQUEST:
$xml_str = $response;
try {
$xml = simplexml_load_string($xml_str);
$xml = simplexml_load_string($response);
// If the response is an API error.
$isError = isset($xml->ErrorNumber) || isset($xml->ErrorCode);
if ($isError) {
$error = ''; $code = 0;
if ($xml !== FALSE && (isset($xml->ErrorNumber) || isset($xml->ErrorCode))) {
$error = '';
$error_code = 0;
if (isset($xml->DetailedErrorDescription) && !empty($xml->DetailedErrorDescription)) {
$error = (string) $xml->DetailedErrorDescription;
}
@ -517,20 +506,16 @@ class EBSCOConnector {
$error = (string) $xml->Reason;
}
if (isset($xml->ErrorNumber)) {
$code = (integer) $xml->ErrorNumber;
$error_code = (int) $xml->ErrorNumber;
}
elseif (isset($xml->ErrorCode)) {
$code = (integer) $xml->ErrorCode;
$error_code = (int) $xml->ErrorCode;
}
$return = new EBSCOException($error, $code);
$return = new EBSCOException($error, $error_code);
}
else {
$return = new EBSCOException("HTTP {$code} : The request could not be understood by the server due to malformed syntax. Modify your search before retrying.");
}
}
catch (Exception $e) {
$return = new EBSCOException($xml);
}
break;
case self::HTTP_NOT_FOUND:
@ -545,20 +530,14 @@ class EBSCOConnector {
$return = new EBSCOException("HTTP {$code} : Unexpected HTTP error.");
break;
}
}
catch (Exception $e) {
// Or $this->toString($response)
$message = $this->toString($e);
\Drupal::logger('ebsco')->error($message);
$return = new EBSCOException($response);
}
// Log any error
if ($this->logAPIRequests) {
// $client = both the HTTP request and response
// $response = only the HTTP response
$message = $this->toString($client); // or $this->toString($response)
\Drupal::logger('ebsco')->error($client);
\Drupal::logger('ebsco')->debug('EDS @method @url returned HTTP @code: @response', [
'@method' => $method,
'@url' => $url,
'@code' => $code,
'@response' => $response,
]);
}
return $return;

3
modules/solr_site/js/solr_site_bestbet.js

@ -30,6 +30,9 @@ Drupal.behaviors.roblib_search_solr_bestbet_results = {
});
jQuery('#' + 'roblib-search-content-solr-site-bestbet').empty().append(items.join(''));
}
}).fail(function () {
// Don't leave the spinner running if the request fails.
jQuery('#roblib-search-content-solr-site-bestbet').empty().append(Drupal.t('Results are currently unavailable.'));
});
}
}

3
modules/solr_site/js/solr_site_results.js

@ -39,6 +39,9 @@ Drupal.behaviors.roblib_search_solr_site_results = {
jQuery('#' + 'roblib-solr-more-results-results').empty().append(results_url);
jQuery('#' + 'roblib-solr-search-toc-results').empty().append(move_me_div_url);
}
}).fail(function () {
// Don't leave the spinner running if the request fails.
jQuery('#roblib-search-content-solr-site-results').empty().append(Drupal.t('Results are currently unavailable.'));
});
}
}

Loading…
Cancel
Save