Skip to content

Commit 2ff0101

Browse files
committed
Merge pull request KnpLabs#86 from kasperg/asset-create
Add support for asset creation
2 parents 92203a6 + bad663d commit 2ff0101

File tree

11 files changed

+199
-67
lines changed

11 files changed

+199
-67
lines changed

doc/repo/assets.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ $asset = $client->api('repo')->releases()->assets()->show('twbs', 'bootstrap', $
1515

1616
### Create an asset
1717

18-
This feature is not implemented because require usage of `uploads.github.com` subdomain.
18+
```php
19+
$asset = $client->api('repo')->releases()->assets()->show('twbs', 'bootstrap', $releaseId, $name, $contentType, $content);
20+
```
1921

2022
### Edit an asset
2123

lib/Github/Api/AbstractApi.php

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ public function setPerPage($perPage)
5757
}
5858

5959
/**
60-
* {@inheritDoc}
60+
* Send a GET request with query parameters.
61+
*
62+
* @param string $path Request path.
63+
* @param array $parameters GET parameters.
64+
* @param array $requestHeaders Request Headers.
65+
* @return \Guzzle\Http\EntityBodyInterface|mixed|string
6166
*/
6267
protected function get($path, array $parameters = array(), $requestHeaders = array())
6368
{
@@ -70,42 +75,105 @@ protected function get($path, array $parameters = array(), $requestHeaders = arr
7075
}
7176

7277
/**
73-
* {@inheritDoc}
78+
* Send a POST request with JSON-encoded parameters.
79+
*
80+
* @param string $path Request path.
81+
* @param array $parameters POST parameters to be JSON encoded.
82+
* @param array $requestHeaders Request headers.
7483
*/
7584
protected function post($path, array $parameters = array(), $requestHeaders = array())
7685
{
77-
$response = $this->client->getHttpClient()->post($path, $parameters, $requestHeaders);
86+
return $this->postRaw(
87+
$path,
88+
$this->createJsonBody($parameters),
89+
$requestHeaders
90+
);
91+
}
92+
93+
/**
94+
* Send a POST request with raw data.
95+
*
96+
* @param string $path Request path.
97+
* @param $body Request body.
98+
* @param array $requestHeaders Request headers.
99+
* @return \Guzzle\Http\EntityBodyInterface|mixed|string
100+
*/
101+
protected function postRaw($path, $body, $requestHeaders = array())
102+
{
103+
$response = $this->client->getHttpClient()->post(
104+
$path,
105+
$body,
106+
$requestHeaders
107+
);
78108

79109
return ResponseMediator::getContent($response);
80110
}
81111

112+
82113
/**
83-
* {@inheritDoc}
114+
* Send a PATCH request with JSON-encoded parameters.
115+
*
116+
* @param string $path Request path.
117+
* @param array $parameters POST parameters to be JSON encoded.
118+
* @param array $requestHeaders Request headers.
84119
*/
85120
protected function patch($path, array $parameters = array(), $requestHeaders = array())
86121
{
87-
$response = $this->client->getHttpClient()->patch($path, $parameters, $requestHeaders);
122+
$response = $this->client->getHttpClient()->patch(
123+
$path,
124+
$this->createJsonBody($parameters),
125+
$requestHeaders
126+
);
88127

89128
return ResponseMediator::getContent($response);
90129
}
91130

131+
92132
/**
93-
* {@inheritDoc}
133+
* Send a PUT request with JSON-encoded parameters.
134+
*
135+
* @param string $path Request path.
136+
* @param array $parameters POST parameters to be JSON encoded.
137+
* @param array $requestHeaders Request headers.
94138
*/
95139
protected function put($path, array $parameters = array(), $requestHeaders = array())
96140
{
97-
$response = $this->client->getHttpClient()->put($path, $parameters, $requestHeaders);
141+
$response = $this->client->getHttpClient()->put(
142+
$path,
143+
$this->createJsonBody($parameters),
144+
$requestHeaders
145+
);
98146

99147
return ResponseMediator::getContent($response);
100148
}
101149

150+
102151
/**
103-
* {@inheritDoc}
152+
* Send a DELETE request with JSON-encoded parameters.
153+
*
154+
* @param string $path Request path.
155+
* @param array $parameters POST parameters to be JSON encoded.
156+
* @param array $requestHeaders Request headers.
104157
*/
105158
protected function delete($path, array $parameters = array(), $requestHeaders = array())
106159
{
107-
$response = $this->client->getHttpClient()->delete($path, $parameters, $requestHeaders);
160+
$response = $this->client->getHttpClient()->delete(
161+
$path,
162+
$this->createJsonBody($parameters),
163+
$requestHeaders
164+
);
108165

109166
return ResponseMediator::getContent($response);
110167
}
168+
169+
/**
170+
* Create a JSON encoded version of an array of parameters.
171+
*
172+
* @param array $parameters Request parameters
173+
* @return null|string
174+
*/
175+
protected function createJsonBody(array $parameters)
176+
{
177+
return (count($parameters) === 0) ? null : json_encode($parameters, empty($parameters) ? JSON_FORCE_OBJECT : 0);
178+
}
111179
}

lib/Github/Api/Repository/Assets.php

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

55
use Github\Api\AbstractApi;
6+
use Github\Exception\ErrorException;
67
use Github\Exception\MissingArgumentException;
8+
use Github\HttpClient\HttpClient;
79

810
/**
911
* @link http://developer.github.com/v3/repos/releases/
@@ -41,6 +43,47 @@ public function show($username, $repository, $id)
4143
return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/releases/assets/'.rawurlencode($id));
4244
}
4345

46+
/**
47+
* Create an asset for selected repository's release
48+
* POST /repos/:owner/:repo/releases/:id/assets?name=:filename
49+
*
50+
* Creating an asset requires support for server name indentification (SNI)
51+
* so this must be supported by your PHP version.
52+
* @see http://developer.github.com/v3/repos/releases/#upload-a-release-asset
53+
* @see http://php.net/manual/en/openssl.constsni.php
54+
*
55+
* @param string $username the user who owns the repo
56+
* @param string $repository the name of the repo
57+
* @param integer $id the id of the release
58+
* @param string $name the filename for the asset
59+
* @param string $contentType the content type for the asset
60+
* @param string $content the content of the asset
61+
*
62+
* @throws MissingArgumentException
63+
* @throws ErrorException
64+
*
65+
* @return array
66+
*/
67+
public function create($username, $repository, $id, $name, $contentType, $content)
68+
{
69+
if (!defined('OPENSSL_TLSEXT_SERVER_NAME') || !OPENSSL_TLSEXT_SERVER_NAME) {
70+
throw new ErrorException('Asset upload support requires Server Name Indication. This is not supported se your PHP version. See http://php.net/manual/en/openssl.constsni.php.');
71+
}
72+
73+
// Asset creation requires a separate endpoint, uploads.github.com.
74+
// Change the base url for the HTTP client temporarily while we execute
75+
// this request.
76+
$baseUrl = $this->client->getHttpClient()->client->getBaseUrl();
77+
$this->client->getHttpClient()->client->setBaseUrl('https://uploads.github.com/');
78+
79+
$response = $this->postRaw('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/releases/'.rawurlencode($id).'/assets?name='.$name, $content, array('Content-Type' => $contentType));
80+
81+
// Reset the base url.
82+
$this->client->getHttpClient()->client->setBaseUrl($baseUrl);
83+
84+
return $response;
85+
}
86+
4487
/**
4588
* Edit an asset in selected repository's release
4689
* PATCH /repos/:owner/:repo/releases/assets/:id

lib/Github/HttpClient/CachedHttpClient.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public function setCache(CacheInterface $cache)
4242
/**
4343
* {@inheritdoc}
4444
*/
45-
public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array())
45+
public function request($path, $body = null, $httpMethod = 'GET', array $headers = array(), array $options = array())
4646
{
47-
$response = parent::request($path, $parameters, $httpMethod, $headers);
47+
$response = parent::request($path, $body, $httpMethod, $headers, $options);
4848

4949
$key = trim($this->options['base_url'].$path, '/');
5050
if (304 == $response->getStatusCode()) {
@@ -61,9 +61,9 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
6161
*
6262
* {@inheritdoc}
6363
*/
64-
protected function createRequest($httpMethod, $path, array $parameters = array(), array $headers = array())
64+
protected function createRequest($httpMethod, $path, $body = null, array $headers = array(), array $options = array())
6565
{
66-
$request = parent::createRequest($httpMethod, $path, $parameters, $headers = array());
66+
$request = parent::createRequest($httpMethod, $path, $body, $headers = array(), $options);
6767

6868
if ($modifiedAt = $this->getCache()->getModifiedSince($path)) {
6969
$modifiedAt = new \DateTime('@'.$modifiedAt);

lib/Github/HttpClient/HttpClient.php

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,47 +87,47 @@ public function addListener($eventName, $listener)
8787
*/
8888
public function get($path, array $parameters = array(), array $headers = array())
8989
{
90-
return $this->request($path, $parameters, 'GET', $headers);
90+
return $this->request($path, null, 'GET', $headers, array('query' => $parameters));
9191
}
9292

9393
/**
9494
* {@inheritDoc}
9595
*/
96-
public function post($path, array $parameters = array(), array $headers = array())
96+
public function post($path, $body = null, array $headers = array())
9797
{
98-
return $this->request($path, $parameters, 'POST', $headers);
98+
return $this->request($path, $body, 'POST', $headers);
9999
}
100100

101101
/**
102102
* {@inheritDoc}
103103
*/
104-
public function patch($path, array $parameters = array(), array $headers = array())
104+
public function patch($path, $body = null, array $headers = array())
105105
{
106-
return $this->request($path, $parameters, 'PATCH', $headers);
106+
return $this->request($path, $body, 'PATCH', $headers);
107107
}
108108

109109
/**
110110
* {@inheritDoc}
111111
*/
112-
public function delete($path, array $parameters = array(), array $headers = array())
112+
public function delete($path, $body = null, array $headers = array())
113113
{
114-
return $this->request($path, $parameters, 'DELETE', $headers);
114+
return $this->request($path, $body, 'DELETE', $headers);
115115
}
116116

117117
/**
118118
* {@inheritDoc}
119119
*/
120-
public function put($path, array $parameters = array(), array $headers = array())
120+
public function put($path, $body, array $headers = array())
121121
{
122-
return $this->request($path, $parameters, 'PUT', $headers);
122+
return $this->request($path, $body, 'PUT', $headers);
123123
}
124124

125125
/**
126126
* {@inheritDoc}
127127
*/
128-
public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array())
128+
public function request($path, $body = null, $httpMethod = 'GET', array $headers = array(), array $options = array())
129129
{
130-
$request = $this->createRequest($httpMethod, $path, $parameters, $headers);
130+
$request = $this->createRequest($httpMethod, $path, $body, $headers, $options);
131131
$request->addHeaders($headers);
132132

133133
try {
@@ -170,23 +170,13 @@ public function getLastResponse()
170170
return $this->lastResponse;
171171
}
172172

173-
protected function createRequest($httpMethod, $path, array $parameters = array(), array $headers = array())
173+
protected function createRequest($httpMethod, $path, $body = null, array $headers = array(), array $options = array())
174174
{
175-
if ('GET' !== $httpMethod) {
176-
$requestBody = count($parameters) === 0
177-
? null : json_encode($parameters, empty($parameters) ? JSON_FORCE_OBJECT : 0)
178-
;
179-
$options = array();
180-
} else {
181-
$requestBody = null;
182-
$options = array('query' => $parameters);
183-
}
184-
185175
return $this->client->createRequest(
186176
$httpMethod,
187177
$path,
188178
array_merge($this->headers, $headers),
189-
$requestBody,
179+
$body,
190180
$options
191181
);
192182
}

lib/Github/HttpClient/HttpClientInterface.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,58 +26,59 @@ public function get($path, array $parameters = array(), array $headers = array()
2626
* Send a POST request
2727
*
2828
* @param string $path Request path
29-
* @param array $parameters POST Parameters
29+
* @param mixed $body Request body
3030
* @param array $headers Reconfigure the request headers for this call only
3131
*
3232
* @return array Data
3333
*/
34-
public function post($path, array $parameters = array(), array $headers = array());
34+
public function post($path, $body = null, array $headers = array());
3535

3636
/**
3737
* Send a PATCH request
3838
*
3939
* @param string $path Request path
40-
* @param array $parameters PATCH Parameters
40+
* @param mixed $body Reuqest body
4141
* @param array $headers Reconfigure the request headers for this call only
4242
*
43+
* @internal param array $parameters Request body
4344
* @return array Data
4445
*/
45-
public function patch($path, array $parameters = array(), array $headers = array());
46+
public function patch($path, $body = null, array $headers = array());
4647

4748
/**
4849
* Send a PUT request
4950
*
5051
* @param string $path Request path
51-
* @param array $parameters PUT Parameters
52+
* @param mixed $body Request body
5253
* @param array $headers Reconfigure the request headers for this call only
5354
*
5455
* @return array Data
5556
*/
56-
public function put($path, array $parameters = array(), array $headers = array());
57+
public function put($path, $body, array $headers = array());
5758

5859
/**
5960
* Send a DELETE request
6061
*
6162
* @param string $path Request path
62-
* @param array $parameters DELETE Parameters
63+
* @param mixed $body Request body
6364
* @param array $headers Reconfigure the request headers for this call only
6465
*
6566
* @return array Data
6667
*/
67-
public function delete($path, array $parameters = array(), array $headers = array());
68+
public function delete($path, $body = null, array $headers = array());
6869

6970
/**
7071
* Send a request to the server, receive a response,
7172
* decode the response and returns an associative array
7273
*
73-
* @param string $path Request API path
74-
* @param array $parameters Parameters
74+
* @param string $path Request path
75+
* @param mixed $body Request body
7576
* @param string $httpMethod HTTP method to use
7677
* @param array $headers Request headers
7778
*
7879
* @return array Data
7980
*/
80-
public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array());
81+
public function request($path, $body, $httpMethod = 'GET', array $headers = array());
8182

8283
/**
8384
* Change an option value.

test/Github/Tests/Api/AbstractApiTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ public function post($path, array $parameters = array(), $requestHeaders = array
161161
return $this->client->getHttpClient()->post($path, $parameters, $requestHeaders);
162162
}
163163

164+
/**
165+
* {@inheritDoc}
166+
*/
167+
public function postRaw($path, $body, $requestHeaders = array())
168+
{
169+
return $this->client->getHttpClient()->post($path, $body, $requestHeaders);
170+
}
171+
164172
/**
165173
* {@inheritDoc}
166174
*/

0 commit comments

Comments
 (0)