Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/Github/Api/GitData/Blobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class Blobs extends AbstractApi
{
use AcceptHeaderTrait;

/**
* Configure the Accept header depending on the blob type.
*
Expand All @@ -23,7 +23,7 @@ class Blobs extends AbstractApi
public function configure($bodyType = null)
{
if ('raw' === $bodyType) {
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.raw', $this->client->getOption('api_version'));
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.raw', $this->client->getApiVersion());
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Github/Api/Issue/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function configure($bodyType = null)
$bodyType = 'full';
}

$this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $this->client->getOption('api_version'), $bodyType);
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $this->client->getApiVersion(), $bodyType);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions lib/Github/Api/Repository/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@
class Comments extends AbstractApi
{
use AcceptHeaderTrait;

public function configure($bodyType = null)
{
switch ($bodyType) {
case 'raw':
$header = sprintf('Accept: application/vnd.github.%s.raw+json', $this->client->getOption('api_version'));
$header = sprintf('Accept: application/vnd.github.%s.raw+json', $this->client->getApiVersion());
break;

case 'text':
$header = sprintf('Accept: application/vnd.github.%s.text+json', $this->client->getOption('api_version'));
$header = sprintf('Accept: application/vnd.github.%s.text+json', $this->client->getApiVersion());
break;

case 'html':
$header = sprintf('Accept: application/vnd.github.%s.html+json', $this->client->getOption('api_version'));
$header = sprintf('Accept: application/vnd.github.%s.html+json', $this->client->getApiVersion());
break;

default:
$header = sprintf('Accept: application/vnd.github.%s.full+json', $this->client->getOption('api_version'));
$header = sprintf('Accept: application/vnd.github.%s.full+json', $this->client->getApiVersion());
}

$this->acceptHeaderValue = $header;
Expand Down
2 changes: 1 addition & 1 deletion lib/Github/Api/Repository/Stargazers.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Stargazers extends AbstractApi
public function configure($bodyType = null)
{
if ('star' === $bodyType) {
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.star+json', $this->client->getOption('api_version'));
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.star+json', $this->client->getApiVersion());
}
}

Expand Down
67 changes: 29 additions & 38 deletions lib/Github/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,9 @@ class Client
const AUTH_HTTP_TOKEN = 'http_token';

/**
* @var array
* @var string
*/
private $options = array(
'api_version' => 'v3',
);
private $apiVersion = 'v3';

/**
* The object that sends HTTP messages
Expand Down Expand Up @@ -158,6 +156,8 @@ public function __construct(HttpClient $httpClient = null)
$this->addPlugin(new Plugin\HeaderDefaultsPlugin(array(
'User-Agent' => 'php-github-api (http://github.com/KnpLabs/php-github-api)',
)));
// Add standard headers.
$this->clearHeaders();
}

/**
Expand Down Expand Up @@ -305,7 +305,7 @@ public function setEnterpriseUrl($enterpriseUrl)
$this->removePlugin(PathPrepend::class);

$this->addPlugin(new Plugin\AddHostPlugin(UriFactoryDiscovery::find()->createUri($enterpriseUrl)));
$this->addPlugin(new PathPrepend(sprintf('/api/%s/', $this->getOption('api_version'))));
$this->addPlugin(new PathPrepend(sprintf('/api/%s/', $this->getApiVersion())));
}

/**
Expand Down Expand Up @@ -359,13 +359,36 @@ public function setHttpClient(HttpClient $httpClient)
$this->httpClient = $httpClient;
}

/**
* @return string
*/
public function getApiVersion()
{
return $this->apiVersion;
}

/**
* @param string $apiVersion
*
* @return Client
*/
public function setApiVersion($apiVersion)
{
$this->apiVersion = $apiVersion;
$this->addHeaders([
'Accept' => sprintf('application/vnd.github.%s+json', $apiVersion),
]);

return $this;
}

/**
* Clears used headers.
*/
public function clearHeaders()
{
$this->headers = array(
'Accept' => sprintf('application/vnd.github.%s+json', $this->options['api_version']),
'Accept' => sprintf('application/vnd.github.%s+json', $this->getApiVersion()),
);

$this->removePlugin(Plugin\HeaderAppendPlugin::class);
Expand Down Expand Up @@ -401,38 +424,6 @@ public function removeCache()
$this->removePlugin(Plugin\CachePlugin::class);
}

/**
* @param string $name
*
* @throws InvalidArgumentException
*
* @return mixed
*/
public function getOption($name)
{
if (!array_key_exists($name, $this->options)) {
throw new InvalidArgumentException(sprintf('Undefined option called: "%s"', $name));
}

return $this->options[$name];
}

/**
* @param string $name
* @param mixed $value
*
* @throws InvalidArgumentException
* @throws InvalidArgumentException
*/
public function setOption($name, $value)
{
if (!array_key_exists($name, $this->options)) {
throw new InvalidArgumentException(sprintf('Undefined option called: "%s"', $name));
}

$this->options[$name] = $value;
}

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