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. 201
      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-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>'); 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-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>'); 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.'));
}); });
} }
} }

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

@ -1,5 +1,7 @@
<?php <?php
namespace Drupal\roblib_search_eds\Lib; namespace Drupal\roblib_search_eds\Lib;
use GuzzleHttp\Exception\GuzzleException;
/** /**
* @file * @file
* The EBSCO Connector and Exception classes. * The EBSCO Connector and Exception classes.
@ -429,136 +431,113 @@ class EBSCOConnector {
* @access protected * @access protected
*/ */
protected function request($url, $params, $headers = array(), $method = 'GET') { protected function request($url, $params, $headers = array(), $method = 'GET') {
$xml = FALSE;
$return = FALSE; $return = FALSE;
$data = NULL; $response = NULL;
if(!is_array($headers)){ if (!is_array($headers)) {
$headers = []; $headers = [];
} }
$headers[] = 'Content-Type: text/xml';
// Add compression in case its not there. // Convert "Name: value" header strings into the array Guzzle expects.
array_push($headers, 'Content-Type: text/xml'); $request_headers = [];
$data=$params; foreach ($headers as $header) {
// Send the request. [$name, $value] = array_map('trim', explode(':', $header, 2)) + [1 => ''];
$response=null; $request_headers[$name] = $value;
}
try { $options = [
$curl = curl_init(); 'headers' => $request_headers,
curl_setopt($curl, CURLOPT_URL, $url); 'timeout' => 10,
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 'allow_redirects' => ['max' => 10],
curl_setopt($curl, CURLOPT_MAXREDIRS, 10 ); // Let us inspect 4xx/5xx bodies; EDS returns API errors as HTTP 400.
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); 'http_errors' => FALSE,
curl_setopt($curl, CURLOPT_TIMEOUT, 10); ];
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); switch ($method) {
case 'GET':
switch ($method) if ($params) {
{ $url = sprintf('%s?%s', $url, http_build_query($params));
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);
}
break;
case 'DELETE':
if ($data) {
if (count($headers)>0) {
curl_setopt($curl,CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
}
break;
} }
break;
case 'POST':
case 'DELETE':
if ($params) {
$options['body'] = $params;
}
break;
}
$response = curl_exec($curl); try {
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE); $http_response = \Drupal::httpClient()->request($method, $url, $options);
curl_close($curl); $code = $http_response->getStatusCode();
$response = (string) $http_response->getBody();
switch ($code) { }
case self::HTTP_OK: catch (GuzzleException $e) {
// Network level failure (timeout, DNS, TLS...).
$xml_str = $response; \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.');
}
try { switch ($code) {
// Clean EMP namespace. case self::HTTP_OK:
$xml_str = str_replace(array("<a:", "</a:"), array("<", "</"), $xml_str); // Clean EMP namespace.
$xml = simplexml_load_string($xml_str); $xml_str = str_replace(array("<a:", "</a:"), array("<", "</"), $response);
$return = $xml; $xml = simplexml_load_string($xml_str);
$return = ($xml !== FALSE) ? $xml : new EBSCOException('The EDS API returned an invalid response.');
break;
case self::HTTP_BAD_REQUEST:
$xml = simplexml_load_string($response);
// If the response is an API error.
if ($xml !== FALSE && (isset($xml->ErrorNumber) || isset($xml->ErrorCode))) {
$error = '';
$error_code = 0;
if (isset($xml->DetailedErrorDescription) && !empty($xml->DetailedErrorDescription)) {
$error = (string) $xml->DetailedErrorDescription;
}
elseif (isset($xml->ErrorDescription)) {
$error = (string) $xml->ErrorDescription;
} }
catch (Exception $e) { elseif (isset($xml->Reason)) {
$return = new EBSCOException($xml); $error = (string) $xml->Reason;
} }
break; if (isset($xml->ErrorNumber)) {
$error_code = (int) $xml->ErrorNumber;
case self::HTTP_BAD_REQUEST:
$xml_str = $response;
try {
$xml = simplexml_load_string($xml_str);
// If the response is an API error.
$isError = isset($xml->ErrorNumber) || isset($xml->ErrorCode);
if ($isError) {
$error = ''; $code = 0;
if (isset($xml->DetailedErrorDescription) && !empty($xml->DetailedErrorDescription)) {
$error = (string) $xml->DetailedErrorDescription;
}
elseif (isset($xml->ErrorDescription)) {
$error = (string) $xml->ErrorDescription;
}
elseif (isset($xml->Reason)) {
$error = (string) $xml->Reason;
}
if (isset($xml->ErrorNumber)) {
$code = (integer) $xml->ErrorNumber;
}
elseif (isset($xml->ErrorCode)) {
$code = (integer) $xml->ErrorCode;
}
$return = new EBSCOException($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) { elseif (isset($xml->ErrorCode)) {
$return = new EBSCOException($xml); $error_code = (int) $xml->ErrorCode;
} }
break; $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.");
}
break;
case self::HTTP_NOT_FOUND: case self::HTTP_NOT_FOUND:
$return = new EBSCOException("HTTP {$code} : The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable."); $return = new EBSCOException("HTTP {$code} : The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.");
break; break;
case self::HTTP_INTERNAL_SERVER_ERROR: case self::HTTP_INTERNAL_SERVER_ERROR:
$return = new EBSCOException("HTTP {$code} : The server encountered an unexpected condition which prevented it from fulfilling the request."); $return = new EBSCOException("HTTP {$code} : The server encountered an unexpected condition which prevented it from fulfilling the request.");
break; break;
default: default:
$return = new EBSCOException("HTTP {$code} : Unexpected HTTP error."); $return = new EBSCOException("HTTP {$code} : Unexpected HTTP error.");
break; 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) { if ($this->logAPIRequests) {
// $client = both the HTTP request and response \Drupal::logger('ebsco')->debug('EDS @method @url returned HTTP @code: @response', [
// $response = only the HTTP response '@method' => $method,
$message = $this->toString($client); // or $this->toString($response) '@url' => $url,
\Drupal::logger('ebsco')->error($client); '@code' => $code,
'@response' => $response,
]);
} }
return $return; 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('')); 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-more-results-results').empty().append(results_url);
jQuery('#' + 'roblib-solr-search-toc-results').empty().append(move_me_div_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