Skip to content

Commit

Permalink
Add a User-Agent header for API calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
realityking committed Jan 19, 2016
1 parent 0daba5b commit 671b7a1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added
* Send a User-Agent header with API requests.

### Changed
* When GitHub is generating archieves, are few files with metadata are excluded.

Expand Down
14 changes: 14 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function __construct($token, $baseUri, $headers = [])
{
$stack = HandlerStack::create();
$stack->push(new BearerToken($token));
$headers['User-Agent'] = $this->getUserAgent();

$this->httpClient = new GuzzleClient([
'base_uri' => $baseUri,
Expand Down Expand Up @@ -61,6 +62,19 @@ protected function request($method, $path, array $options = [])
return $this->decodeJson($response->getBody());
}

abstract protected function getUserAgentAppName();

protected function getUserAgent()
{
$agent = $this->getUserAgentAppName() . ' GuzzleHttp/' . GuzzleClient::VERSION;
if (extension_loaded('curl') && function_exists('curl_version')) {
$agent .= ' curl/' . \curl_version()['version'];
}
$agent .= ' PHP/' . \PHP_VERSION;

return $agent;
}

/**
* @param string $json JSON encoded object or array
*
Expand Down
9 changes: 8 additions & 1 deletion src/Delivery/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
class Client extends BaseClient
{
const VERSION = '0.6.1-beta';

/**
* @var ResourceBuilder
*/
Expand Down Expand Up @@ -51,13 +53,18 @@ public function __construct($token, $spaceId, $preview = false)

$instanceCache = new InstanceCache;

parent::__construct($token, $baseUri . $spaceId . '/', $instanceCache);
parent::__construct($token, $baseUri . $spaceId . '/');

$this->preview = $preview;
$this->instanceCache = $instanceCache;
$this->builder = new ResourceBuilder($this, $instanceCache, $spaceId);
}

protected function getUserAgentAppName()
{
return 'ContentfulCDA/' . self::VERSION;
}

/**
* @param string $id
*
Expand Down

0 comments on commit 671b7a1

Please sign in to comment.