Skip to content

Commit dc61c89

Browse files
committed
Allow binary content downloads of assets
1 parent 63f7d10 commit dc61c89

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)