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: 3 additions & 1 deletion doc/repo/assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ $asset = $client->api('repo')->releases()->assets()->show('twbs', 'bootstrap', $

### Create an asset

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

### Edit an asset

Expand Down
86 changes: 77 additions & 9 deletions lib/Github/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ public function setPerPage($perPage)
}

/**
* {@inheritDoc}
* Send a GET request with query parameters.
*
* @param string $path Request path.
* @param array $parameters GET parameters.
* @param array $requestHeaders Request Headers.
* @return \Guzzle\Http\EntityBodyInterface|mixed|string
*/
protected function get($path, array $parameters = array(), $requestHeaders = array())
{
Expand All @@ -70,42 +75,105 @@ protected function get($path, array $parameters = array(), $requestHeaders = arr
}

/**
* {@inheritDoc}
* Send a POST request with JSON-encoded parameters.
*
* @param string $path Request path.
* @param array $parameters POST parameters to be JSON encoded.
* @param array $requestHeaders Request headers.
*/
protected function post($path, array $parameters = array(), $requestHeaders = array())
{
$response = $this->client->getHttpClient()->post($path, $parameters, $requestHeaders);
return $this->postRaw(
$path,
$this->createJsonBody($parameters),
$requestHeaders
);
}

/**
* Send a POST request with raw data.
*
* @param string $path Request path.
* @param $body Request body.
* @param array $requestHeaders Request headers.
* @return \Guzzle\Http\EntityBodyInterface|mixed|string
*/
protected function postRaw($path, $body, $requestHeaders = array())
{
$response = $this->client->getHttpClient()->post(
$path,
$body,
$requestHeaders
);

return ResponseMediator::getContent($response);
}


/**
* {@inheritDoc}
* Send a PATCH request with JSON-encoded parameters.
*
* @param string $path Request path.
* @param array $parameters POST parameters to be JSON encoded.
* @param array $requestHeaders Request headers.
*/
protected function patch($path, array $parameters = array(), $requestHeaders = array())
{
$response = $this->client->getHttpClient()->patch($path, $parameters, $requestHeaders);
$response = $this->client->getHttpClient()->patch(
$path,
$this->createJsonBody($parameters),
$requestHeaders
);

return ResponseMediator::getContent($response);
}


/**
* {@inheritDoc}
* Send a PUT request with JSON-encoded parameters.
*
* @param string $path Request path.
* @param array $parameters POST parameters to be JSON encoded.
* @param array $requestHeaders Request headers.
*/
protected function put($path, array $parameters = array(), $requestHeaders = array())
{
$response = $this->client->getHttpClient()->put($path, $parameters, $requestHeaders);
$response = $this->client->getHttpClient()->put(
$path,
$this->createJsonBody($parameters),
$requestHeaders
);

return ResponseMediator::getContent($response);
}


/**
* {@inheritDoc}
* Send a DELETE request with JSON-encoded parameters.
*
* @param string $path Request path.
* @param array $parameters POST parameters to be JSON encoded.
* @param array $requestHeaders Request headers.
*/
protected function delete($path, array $parameters = array(), $requestHeaders = array())
{
$response = $this->client->getHttpClient()->delete($path, $parameters, $requestHeaders);
$response = $this->client->getHttpClient()->delete(
$path,
$this->createJsonBody($parameters),
$requestHeaders
);

return ResponseMediator::getContent($response);
}

/**
* Create a JSON encoded version of an array of parameters.
*
* @param array $parameters Request parameters
* @return null|string
*/
protected function createJsonBody(array $parameters)
{
return (count($parameters) === 0) ? null : json_encode($parameters, empty($parameters) ? JSON_FORCE_OBJECT : 0);
}
}
43 changes: 43 additions & 0 deletions lib/Github/Api/Repository/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Github\Api\Repository;

use Github\Api\AbstractApi;
use Github\Exception\ErrorException;
use Github\Exception\MissingArgumentException;
use Github\HttpClient\HttpClient;

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

/**
* Create an asset for selected repository's release
* POST /repos/:owner/:repo/releases/:id/assets?name=:filename
*
* Creating an asset requires support for server name indentification (SNI)
* so this must be supported by your PHP version.
* @see http://developer.github.com/v3/repos/releases/#upload-a-release-asset
* @see http://php.net/manual/en/openssl.constsni.php
*
* @param string $username the user who owns the repo
* @param string $repository the name of the repo
* @param integer $id the id of the release
* @param string $name the filename for the asset
* @param string $contentType the content type for the asset
* @param string $content the content of the asset
*
* @throws MissingArgumentException
* @throws ErrorException
*
* @return array
*/
public function create($username, $repository, $id, $name, $contentType, $content)
{
if (!defined('OPENSSL_TLSEXT_SERVER_NAME') || !OPENSSL_TLSEXT_SERVER_NAME) {
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.');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a skipTest for phpunit test on earlier versions of php

}

// Asset creation requires a separate endpoint, uploads.github.com.
// Change the base url for the HTTP client temporarily while we execute
// this request.
$baseUrl = $this->client->getHttpClient()->client->getBaseUrl();
$this->client->getHttpClient()->client->setBaseUrl('https://uploads.github.com/');

$response = $this->postRaw('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/releases/'.rawurlencode($id).'/assets?name='.$name, $content, array('Content-Type' => $contentType));

// Reset the base url.
$this->client->getHttpClient()->client->setBaseUrl($baseUrl);

return $response;
}

/**
* Edit an asset in selected repository's release
* PATCH /repos/:owner/:repo/releases/assets/:id
Expand Down
8 changes: 4 additions & 4 deletions lib/Github/HttpClient/CachedHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public function setCache(CacheInterface $cache)
/**
* {@inheritdoc}
*/
public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array())
public function request($path, $body = null, $httpMethod = 'GET', array $headers = array(), array $options = array())
{
$response = parent::request($path, $parameters, $httpMethod, $headers);
$response = parent::request($path, $body, $httpMethod, $headers, $options);

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

if ($modifiedAt = $this->getCache()->getModifiedSince($path)) {
$modifiedAt = new \DateTime('@'.$modifiedAt);
Expand Down
36 changes: 13 additions & 23 deletions lib/Github/HttpClient/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,47 +87,47 @@ public function addListener($eventName, $listener)
*/
public function get($path, array $parameters = array(), array $headers = array())
{
return $this->request($path, $parameters, 'GET', $headers);
return $this->request($path, null, 'GET', $headers, array('query' => $parameters));
}

/**
* {@inheritDoc}
*/
public function post($path, array $parameters = array(), array $headers = array())
public function post($path, $body = null, array $headers = array())
{
return $this->request($path, $parameters, 'POST', $headers);
return $this->request($path, $body, 'POST', $headers);
}

/**
* {@inheritDoc}
*/
public function patch($path, array $parameters = array(), array $headers = array())
public function patch($path, $body = null, array $headers = array())
{
return $this->request($path, $parameters, 'PATCH', $headers);
return $this->request($path, $body, 'PATCH', $headers);
}

/**
* {@inheritDoc}
*/
public function delete($path, array $parameters = array(), array $headers = array())
public function delete($path, $body = null, array $headers = array())
{
return $this->request($path, $parameters, 'DELETE', $headers);
return $this->request($path, $body, 'DELETE', $headers);
}

/**
* {@inheritDoc}
*/
public function put($path, array $parameters = array(), array $headers = array())
public function put($path, $body, array $headers = array())
{
return $this->request($path, $parameters, 'PUT', $headers);
return $this->request($path, $body, 'PUT', $headers);
}

/**
* {@inheritDoc}
*/
public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array())
public function request($path, $body = null, $httpMethod = 'GET', array $headers = array(), array $options = array())
{
$request = $this->createRequest($httpMethod, $path, $parameters, $headers);
$request = $this->createRequest($httpMethod, $path, $body, $headers, $options);
$request->addHeaders($headers);

try {
Expand Down Expand Up @@ -170,23 +170,13 @@ public function getLastResponse()
return $this->lastResponse;
}

protected function createRequest($httpMethod, $path, array $parameters = array(), array $headers = array())
protected function createRequest($httpMethod, $path, $body = null, array $headers = array(), array $options = array())
{
if ('GET' !== $httpMethod) {
$requestBody = count($parameters) === 0
? null : json_encode($parameters, empty($parameters) ? JSON_FORCE_OBJECT : 0)
;
$options = array();
} else {
$requestBody = null;
$options = array('query' => $parameters);
}

return $this->client->createRequest(
$httpMethod,
$path,
array_merge($this->headers, $headers),
$requestBody,
$body,
$options
);
}
Expand Down
23 changes: 12 additions & 11 deletions lib/Github/HttpClient/HttpClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,58 +26,59 @@ public function get($path, array $parameters = array(), array $headers = array()
* Send a POST request
*
* @param string $path Request path
* @param array $parameters POST Parameters
* @param mixed $body Request body
* @param array $headers Reconfigure the request headers for this call only
*
* @return array Data
*/
public function post($path, array $parameters = array(), array $headers = array());
public function post($path, $body = null, array $headers = array());

/**
* Send a PATCH request
*
* @param string $path Request path
* @param array $parameters PATCH Parameters
* @param mixed $body Reuqest body
* @param array $headers Reconfigure the request headers for this call only
*
* @internal param array $parameters Request body
* @return array Data
*/
public function patch($path, array $parameters = array(), array $headers = array());
public function patch($path, $body = null, array $headers = array());

/**
* Send a PUT request
*
* @param string $path Request path
* @param array $parameters PUT Parameters
* @param mixed $body Request body
* @param array $headers Reconfigure the request headers for this call only
*
* @return array Data
*/
public function put($path, array $parameters = array(), array $headers = array());
public function put($path, $body, array $headers = array());

/**
* Send a DELETE request
*
* @param string $path Request path
* @param array $parameters DELETE Parameters
* @param mixed $body Request body
* @param array $headers Reconfigure the request headers for this call only
*
* @return array Data
*/
public function delete($path, array $parameters = array(), array $headers = array());
public function delete($path, $body = null, array $headers = array());

/**
* Send a request to the server, receive a response,
* decode the response and returns an associative array
*
* @param string $path Request API path
* @param array $parameters Parameters
* @param string $path Request path
* @param mixed $body Request body
* @param string $httpMethod HTTP method to use
* @param array $headers Request headers
*
* @return array Data
*/
public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array());
public function request($path, $body, $httpMethod = 'GET', array $headers = array());

/**
* Change an option value.
Expand Down
8 changes: 8 additions & 0 deletions test/Github/Tests/Api/AbstractApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ public function post($path, array $parameters = array(), $requestHeaders = array
return $this->client->getHttpClient()->post($path, $parameters, $requestHeaders);
}

/**
* {@inheritDoc}
*/
public function postRaw($path, $body, $requestHeaders = array())
{
return $this->client->getHttpClient()->post($path, $body, $requestHeaders);
}

/**
* {@inheritDoc}
*/
Expand Down
Loading