Skip to content

Commit

Permalink
Allow setting custom headers
Browse files Browse the repository at this point in the history
  • Loading branch information
danmichaelo committed Aug 1, 2017
1 parent 3550b3f commit 4dc54be
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 4dc54be

Please sign in to comment.