From 62e361052783e55424cfc55bc62211c5a4dd81bb Mon Sep 17 00:00:00 2001 From: Sambhav Pokharel Date: Tue, 15 Jul 2025 13:06:51 -0400 Subject: [PATCH 1/2] Guzzle Connector --- SerialsSolutions/Summon/Guzzle.php | 148 +++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 SerialsSolutions/Summon/Guzzle.php diff --git a/SerialsSolutions/Summon/Guzzle.php b/SerialsSolutions/Summon/Guzzle.php new file mode 100644 index 0000000..9c10959 --- /dev/null +++ b/SerialsSolutions/Summon/Guzzle.php @@ -0,0 +1,148 @@ + + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org Main Site + */ + +namespace SerialsSolutions\Summon; + +use GuzzleHttp\Client as HttpClient; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerInterface; +use SerialsSolutions_Summon_Exception; + +/** + * Summon Search API Interface (Guzzle and PSR-compliant implementation) + * + * @category VuFind + * @package Search + * @author Sambhav Pokharel + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org + */ +class Guzzle extends \SerialsSolutions_Summon_Base implements LoggerAwareInterface +{ + /** + * HTTP client instance + * + * @var HttpClient + */ + protected $client; + + /** + * Logger instance. + * + * @var ?LoggerInterface + */ + protected $logger = null; + + /** + * Constructor. + * + * @param string $apiId Summon API ID + * @param string $apiKey Summon API Key + * @param array $options Options for the parent constructor + * @param HttpClient $client Optional HTTP client to use + */ + public function __construct(string $apiId, string $apiKey, array $options = [], ?HttpClient $client = null) + { + parent::__construct($apiId, $apiKey, $options); + $this->client = $client ?? new HttpClient(); + } + + /** + * Sets the logger instance. + * + * @param LoggerInterface $logger The logger instance to set. + * + * @return void + */ + public function setLogger(LoggerInterface $logger): void + { + $this->logger = $logger; + } + + /** + * Prints a debug message if debug is enabled. + * + * @param string $msg The message to debug. + * + * @return void + */ + protected function debugPrint($msg) + { + if ($this->logger) { + $this->logger->debug($msg); + } else { + parent::debugPrint($msg); + } + } + + /** + * Handle a fatal error. + * + * @param SerialsSolutions_Summon_Exception $e Exception to process. + * + * @return void + */ + public function handleFatalError($e) + { + throw $e; + } + + /** + * Perform an HTTP request. + * + * @param string $baseUrl Base URL for request + * @param string $method HTTP method for request + * @param string $queryString Query string to append to URL + * @param array $headers HTTP headers to send + * + * @throws SerialsSolutions_Summon_Exception + * @return string HTTP response body + */ + protected function httpRequest($baseUrl, $method, $queryString, $headers) + { + $this->debugPrint("{$method}: {$baseUrl}?{$queryString}"); + + $options = ['headers' => $headers]; + + if ($method == 'GET') { + $baseUrl .= '?' . $queryString; + } elseif ($method == 'POST') { + $options['body'] = $queryString; + $options['headers']['Content-Type'] = 'application/x-www-form-urlencoded'; + } + + $result = $this->client->request($method, $baseUrl, $options); + + if ($result->getStatusCode() < 200 || $result->getStatusCode() >= 300) { + throw new SerialsSolutions_Summon_Exception($result->getBody()->getContents()); + } + + return $result->getBody()->getContents(); + } +} From a75cc496a7aea36e353779d430b60ffce776d649 Mon Sep 17 00:00:00 2001 From: Sambhav Pokharel Date: Tue, 15 Jul 2025 13:30:32 -0400 Subject: [PATCH 2/2] Package name fix --- SerialsSolutions/Summon/Guzzle.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/SerialsSolutions/Summon/Guzzle.php b/SerialsSolutions/Summon/Guzzle.php index 9c10959..78dd4e3 100644 --- a/SerialsSolutions/Summon/Guzzle.php +++ b/SerialsSolutions/Summon/Guzzle.php @@ -20,8 +20,8 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * - * @category VuFind - * @package Search + * @category SerialsSolutions + * @package Summon * @author Sambhav Pokharel * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link https://vufind.org Main Site @@ -37,8 +37,8 @@ /** * Summon Search API Interface (Guzzle and PSR-compliant implementation) * - * @category VuFind - * @package Search + * @category SerialsSolutions + * @package Summon * @author Sambhav Pokharel * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link https://vufind.org