Skip to content

Commit 489868b

Browse files
authored
Merge pull request KnpLabs#415 from Nyholm/header
Make sure to add accept header
2 parents 92f6f2d + 8fb3bfd commit 489868b

File tree

5 files changed

+38
-47
lines changed

5 files changed

+38
-47
lines changed

lib/Github/Api/GitData/Blobs.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
class Blobs extends AbstractApi
1515
{
1616
use AcceptHeaderTrait;
17-
17+
1818
/**
1919
* Configure the Accept header depending on the blob type.
2020
*
@@ -23,7 +23,7 @@ class Blobs extends AbstractApi
2323
public function configure($bodyType = null)
2424
{
2525
if ('raw' === $bodyType) {
26-
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.raw', $this->client->getOption('api_version'));
26+
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.raw', $this->client->getApiVersion());
2727
}
2828
}
2929

lib/Github/Api/Issue/Comments.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function configure($bodyType = null)
2727
$bodyType = 'full';
2828
}
2929

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

3333
/**

lib/Github/Api/Repository/Comments.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@
1414
class Comments extends AbstractApi
1515
{
1616
use AcceptHeaderTrait;
17-
17+
1818
public function configure($bodyType = null)
1919
{
2020
switch ($bodyType) {
2121
case 'raw':
22-
$header = sprintf('Accept: application/vnd.github.%s.raw+json', $this->client->getOption('api_version'));
22+
$header = sprintf('Accept: application/vnd.github.%s.raw+json', $this->client->getApiVersion());
2323
break;
2424

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

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

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

3737
$this->acceptHeaderValue = $header;

lib/Github/Api/Repository/Stargazers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Stargazers extends AbstractApi
2424
public function configure($bodyType = null)
2525
{
2626
if ('star' === $bodyType) {
27-
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.star+json', $this->client->getOption('api_version'));
27+
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.star+json', $this->client->getApiVersion());
2828
}
2929
}
3030

lib/Github/Client.php

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,9 @@ class Client
8787
const AUTH_HTTP_TOKEN = 'http_token';
8888

8989
/**
90-
* @var array
90+
* @var string
9191
*/
92-
private $options = array(
93-
'api_version' => 'v3',
94-
);
92+
private $apiVersion = 'v3';
9593

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

163163
/**
@@ -305,7 +305,7 @@ public function setEnterpriseUrl($enterpriseUrl)
305305
$this->removePlugin(PathPrepend::class);
306306

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

311311
/**
@@ -361,13 +361,36 @@ public function setHttpClient(HttpClient $httpClient)
361361
$this->httpClient = $httpClient;
362362
}
363363

364+
/**
365+
* @return string
366+
*/
367+
public function getApiVersion()
368+
{
369+
return $this->apiVersion;
370+
}
371+
372+
/**
373+
* @param string $apiVersion
374+
*
375+
* @return Client
376+
*/
377+
public function setApiVersion($apiVersion)
378+
{
379+
$this->apiVersion = $apiVersion;
380+
$this->addHeaders([
381+
'Accept' => sprintf('application/vnd.github.%s+json', $apiVersion),
382+
]);
383+
384+
return $this;
385+
}
386+
364387
/**
365388
* Clears used headers.
366389
*/
367390
public function clearHeaders()
368391
{
369392
$this->headers = array(
370-
'Accept' => sprintf('application/vnd.github.%s+json', $this->options['api_version']),
393+
'Accept' => sprintf('application/vnd.github.%s+json', $this->getApiVersion()),
371394
);
372395

373396
$this->removePlugin(Plugin\HeaderAppendPlugin::class);
@@ -403,38 +426,6 @@ public function removeCache()
403426
$this->removePlugin(Plugin\CachePlugin::class);
404427
}
405428

406-
/**
407-
* @param string $name
408-
*
409-
* @throws InvalidArgumentException
410-
*
411-
* @return mixed
412-
*/
413-
public function getOption($name)
414-
{
415-
if (!array_key_exists($name, $this->options)) {
416-
throw new InvalidArgumentException(sprintf('Undefined option called: "%s"', $name));
417-
}
418-
419-
return $this->options[$name];
420-
}
421-
422-
/**
423-
* @param string $name
424-
* @param mixed $value
425-
*
426-
* @throws InvalidArgumentException
427-
* @throws InvalidArgumentException
428-
*/
429-
public function setOption($name, $value)
430-
{
431-
if (!array_key_exists($name, $this->options)) {
432-
throw new InvalidArgumentException(sprintf('Undefined option called: "%s"', $name));
433-
}
434-
435-
$this->options[$name] = $value;
436-
}
437-
438429
/**
439430
* @param string $name
440431
*

0 commit comments

Comments
 (0)