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

125
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,84 +431,71 @@ 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;
}
$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 { switch ($method) {
$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)
{
case 'GET': case 'GET':
if ($data) { $url = sprintf('%s?%s', $url, http_build_query($data)); } if ($params) {
curl_setopt($curl, CURLOPT_URL, $url); $url = sprintf('%s?%s', $url, http_build_query($params));
break;
case 'POST':
if ($data) {
curl_setopt($curl,CURLOPT_POST, 1);
curl_setopt($curl,CURLOPT_POSTFIELDS, $data);
} }
break; break;
case 'POST':
case 'DELETE': case 'DELETE':
if ($data) { if ($params) {
if (count($headers)>0) { $options['body'] = $params;
curl_setopt($curl,CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
} }
break; break;
} }
try {
$response = curl_exec($curl); $http_response = \Drupal::httpClient()->request($method, $url, $options);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE); $code = $http_response->getStatusCode();
curl_close($curl); $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) { switch ($code) {
case self::HTTP_OK: case self::HTTP_OK:
$xml_str = $response;
try {
// Clean EMP namespace. // 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); $xml = simplexml_load_string($xml_str);
$return = $xml; $return = ($xml !== FALSE) ? $xml : new EBSCOException('The EDS API returned an invalid response.');
}
catch (Exception $e) {
$return = new EBSCOException($xml);
}
break; break;
case self::HTTP_BAD_REQUEST: case self::HTTP_BAD_REQUEST:
$xml_str = $response; $xml = simplexml_load_string($response);
try {
$xml = simplexml_load_string($xml_str);
// If the response is an API error. // If the response is an API error.
$isError = isset($xml->ErrorNumber) || isset($xml->ErrorCode); if ($xml !== FALSE && (isset($xml->ErrorNumber) || isset($xml->ErrorCode))) {
if ($isError) { $error = '';
$error = ''; $code = 0; $error_code = 0;
if (isset($xml->DetailedErrorDescription) && !empty($xml->DetailedErrorDescription)) { if (isset($xml->DetailedErrorDescription) && !empty($xml->DetailedErrorDescription)) {
$error = (string) $xml->DetailedErrorDescription; $error = (string) $xml->DetailedErrorDescription;
} }
@ -517,20 +506,16 @@ class EBSCOConnector {
$error = (string) $xml->Reason; $error = (string) $xml->Reason;
} }
if (isset($xml->ErrorNumber)) { if (isset($xml->ErrorNumber)) {
$code = (integer) $xml->ErrorNumber; $error_code = (int) $xml->ErrorNumber;
} }
elseif (isset($xml->ErrorCode)) { 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 { else {
$return = new EBSCOException("HTTP {$code} : The request could not be understood by the server due to malformed syntax. Modify your search before retrying."); $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; break;
case self::HTTP_NOT_FOUND: case self::HTTP_NOT_FOUND:
@ -545,20 +530,14 @@ class EBSCOConnector {
$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