Skip to content

Commit 5c540ae

Browse files
authored
feature KnpLabs#990 Allow binary content downloads of assets (acrobat)
This PR was merged into the 3.2.x-dev branch. Discussion ---------- For this to work I also had to change the way the default accept header, previously the header was appended which produced actually an incorrect accept header. Previously is produced this (this was the same with preview headers being set): ``` Host: api.github.com Accept: application/octet-stream Accept: application/vnd.github.v3+json User-Agent: php-github-api (http://github.com/KnpLabs/php-github-api) ``` With the default headers plugin being used ``` Host: api.github.com Accept: application/octet-stream User-Agent: php-github-api (http://github.com/KnpLabs/php-github-api) ``` Fixes KnpLabs#320 Commits ------- dc61c89 Allow binary content downloads of assets
2 parents 32d46fe + dc61c89 commit 5c540ae

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

doc/repo/assets.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ $assets = $client->api('repo')->releases()->assets()->all('twbs', 'bootstrap', $
1313
$asset = $client->api('repo')->releases()->assets()->show('twbs', 'bootstrap', $assetId);
1414
```
1515

16+
### Download binary content of asset
17+
18+
```php
19+
$asset = $client->api('repo')->releases()->assets()->show('twbs', 'bootstrap', $assetId, true);
20+
```
21+
1622
### Create an asset
1723

1824
```php

lib/Github/Api/Repository/Assets.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Github\Api\Repository;
44

55
use Github\Api\AbstractApi;
6+
use Github\Api\AcceptHeaderTrait;
67
use Github\Exception\ErrorException;
78
use Github\Exception\MissingArgumentException;
89

@@ -13,6 +14,8 @@
1314
*/
1415
class Assets extends AbstractApi
1516
{
17+
use AcceptHeaderTrait;
18+
1619
/**
1720
* Get all release's assets in selected repository
1821
* GET /repos/:owner/:repo/releases/:id/assets.
@@ -36,10 +39,14 @@ public function all($username, $repository, $id)
3639
* @param string $repository the name of the repo
3740
* @param int $id the id of the asset
3841
*
39-
* @return array
42+
* @return array|string
4043
*/
41-
public function show($username, $repository, $id)
44+
public function show($username, $repository, $id, bool $returnBinaryContent = false)
4245
{
46+
if ($returnBinaryContent) {
47+
$this->acceptHeaderValue = 'application/octet-stream';
48+
}
49+
4350
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/releases/assets/'.$id);
4451
}
4552

lib/Github/Client.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,17 @@ public function __construct(Builder $httpClientBuilder = null, $apiVersion = nul
119119
{
120120
$this->responseHistory = new History();
121121
$this->httpClientBuilder = $builder = $httpClientBuilder ?? new Builder();
122+
$this->apiVersion = $apiVersion ?: 'v3';
122123

123124
$builder->addPlugin(new GithubExceptionThrower());
124125
$builder->addPlugin(new Plugin\HistoryPlugin($this->responseHistory));
125126
$builder->addPlugin(new Plugin\RedirectPlugin());
126127
$builder->addPlugin(new Plugin\AddHostPlugin(Psr17FactoryDiscovery::findUriFactory()->createUri('https://api.github.com')));
127128
$builder->addPlugin(new Plugin\HeaderDefaultsPlugin([
128129
'User-Agent' => 'php-github-api (http://github.com/KnpLabs/php-github-api)',
130+
'Accept' => sprintf('application/vnd.github.%s+json', $this->apiVersion),
129131
]));
130132

131-
$this->apiVersion = $apiVersion ?: 'v3';
132-
$builder->addHeaderValue('Accept', sprintf('application/vnd.github.%s+json', $this->apiVersion));
133-
134133
if ($enterpriseUrl) {
135134
$this->setEnterpriseUrl($enterpriseUrl);
136135
}

0 commit comments

Comments
 (0)