From 4dc54bef0ea35b218444dfda7c8f821bdff2ff6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Michael=20O=2E=20Hegg=C3=B8?= Date: Tue, 1 Aug 2017 10:28:12 +0200 Subject: [PATCH] Allow setting custom headers --- src/Client.php | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/src/Client.php b/src/Client.php index 217df5a..d853347 100644 --- a/src/Client.php +++ b/src/Client.php @@ -32,6 +32,9 @@ class Client /** @var string Some user agent string to identify our client */ protected $userAgent; + /** @var array Custom headers */ + protected $headers; + /** * @var string|string[] Proxy configuration details. * @@ -73,9 +76,14 @@ public function __construct( ? $options['version'] : '1.1'; - $this->userAgent = isset($options['user-agent']) - ? $options['user-agent'] - : null; + $this->headers = isset($options['headers']) + ? $options['headers'] + : ['Accept' => 'application/xml']; + + if (isset($options['user-agent'])) { + // legacy option + $this->headers['User-Agent'] = $options['user-agent']; + } if (isset($options['credentials'])) { $authentication = new BasicAuth($options['credentials'][0], $options['credentials'][1]); @@ -122,23 +130,6 @@ public function urlTo($cql, $start = 1, $count = 10, $extraParams = array()) return $this->url . '?' . http_build_query($qs); } - /** - * Get HTTP client configuration options (authentication, proxy, headers) - * - * @return array - */ - public function getHttpHeaders() - { - $headers = array( - 'Accept' => 'application/xml' - ); - if ($this->userAgent) { - $headers['User-Agent'] = $this->userAgent; - } - - return $headers; - } - /** * Perform a searchRetrieve request * @@ -220,7 +211,7 @@ public function explain() */ public function request($method, $url) { - $request = $this->messageFactory->createRequest($method, $url, $this->getHttpHeaders()); + $request = $this->messageFactory->createRequest($method, $url, $this->headers); $response = $this->httpClient->sendRequest($request); return (string) $response->getBody();