Skip to content

Add support for asset creation - #86

Merged
cursedcoder merged 2 commits into
KnpLabs:masterfrom
kasperg:asset-create
Nov 19, 2013
Merged

cursedcoder merged 2 commits into
KnpLabs:masterfrom
kasperg:asset-create

Conversation

@kasperg

@kasperg kasperg commented Nov 13, 2013

Copy link
Copy Markdown
Contributor

Based on #85 I have taken a first stab at support for creating assets.

I do not have that much experience using this library or its code style so feedback would be much appreciated.

The related test case passes but I have tried to create an actual asset using the following code:

$client = new Github\Client();
$client->authenticate($username, $password, Github\Client::AUTH_HTTP_PASSWORD);
$return = $client->api('repo')->releases()->assets()->create('kasperg', 'release-asset-test', '92217', array('name' => 'test.txt', 'content-type' => 'text/plain', 'body' => '123'));

This fails with the following exception. I am unsure why.

Github\Exception\RuntimeException: You have reached GitHub hour limit! Actual limit is: 5000 in /Users/kasper/Code/php-github-api/lib/Github/HttpClient/HttpClient.php on line 138

The corresponding cURL request works fine:

curl --user "kasperg" -H "Content-Type: text/plain" --data-binary test.txt "https://uploads.github.com/repos/kasperg/release-asset-test/releases/92217/assets?name=test1.txt"

If we at some point decide to merge this, the documentation will have to be updated as well.

Comment thread lib/Github/Api/Repository/Assets.php Outdated

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.

blank line should be removed

@cursedcoder

Copy link
Copy Markdown
Contributor

Please use 4-spaces indentation for methods bodies instead of 2-spaces

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.

@kasperg

kasperg commented Nov 15, 2013

Copy link
Copy Markdown
Contributor Author

Thanks for the comments @cursedcoder. I have fixed the broken test and the code style issues.

I am still unable to actually upload an asset using the new method. Uploading with cURL works fine.

@cursedcoder

Copy link
Copy Markdown
Contributor

@kasperg that because body is posted as json hash, you will need to define new method like postBinary

@kasperg

kasperg commented Nov 15, 2013

Copy link
Copy Markdown
Contributor Author

Thank you for the suggestion. I will look into that.

kasperg added a commit to kasperg/php-github-api that referenced this pull request Nov 15, 2013
When receiving a validation error (as in KnpLabs#86) there is no `X-RateLimit-Remaining` header included with the response. That is quite confusing.

Here `$remaining` when cast to a string is set to "" (empty string). This does not strictly equal null and thus a `ApiLimitExceedException` is thrown.

This change fixes this behavior.

cURL requests/responses:

```
# Request:
POST /repos/kasperg/release-asset-test/releases/92217/assets?name=test2.txt HTTP/1.1
Host: uploads.github.com
0: Accept: application/vnd.github.v3+json
1: User-Agent: php-github-api (http://github.com/KnpLabs/php-github-api)
Content-Type: text/plain, text/plain
User-Agent: Guzzle/3.7.4 curl/7.30.0 PHP/5.3.27
Authorization: Basic a2FzcGVyZzpwaW5rbW9ucw==
Content-Length: 14

{"body":"122"}

# Response:
HTTP/1.1 422 status code 422
Cache-Control: no-cache
Content-Length: 214
Content-Type: application/json; charset=utf-8
X-Content-Type-Options: nosniff
X-Github-Media-Type: github.beta; format=json
X-Github-Request-Id: a6633df9-4dff-11e3-800a-0be28f962e12
Date: Fri, 15 Nov 2013 14:10:15 GMT

{"message":"Validation Failed","request_id":"a6633df9-4dff-11e3-800a-0be28f962e12","documentation_url":"http://developer.github.com/v3","errors":[{"resource":"ReleaseAsset","code":"already_exists","field":"name"}]}
```
@cursedcoder

Copy link
Copy Markdown
Contributor

@kasperg please try this patch https://gist.github.com/cursedcoder/7506314

kasperg added a commit to kasperg/php-github-api that referenced this pull request Nov 17, 2013
When receiving a validation error (as in KnpLabs#86) there is no `X-RateLimit-Remaining` header included with the response. That is quite confusing.

Here `$remaining` when cast to a string is set to "" (empty string). This does not strictly equal null and thus a `ApiLimitExceedException` is thrown.

This change fixes this behavior.

cURL requests/responses:

```
# Request:
POST /repos/kasperg/release-asset-test/releases/92217/assets?name=test2.txt HTTP/1.1
Host: uploads.github.com
0: Accept: application/vnd.github.v3+json
1: User-Agent: php-github-api (http://github.com/KnpLabs/php-github-api)
Content-Type: text/plain, text/plain
User-Agent: Guzzle/3.7.4 curl/7.30.0 PHP/5.3.27
Authorization: Basic a2FzcGVyZzpwaW5rbW9ucw==
Content-Length: 14

{"body":"122"}

# Response:
HTTP/1.1 422 status code 422
Cache-Control: no-cache
Content-Length: 214
Content-Type: application/json; charset=utf-8
X-Content-Type-Options: nosniff
X-Github-Media-Type: github.beta; format=json
X-Github-Request-Id: a6633df9-4dff-11e3-800a-0be28f962e12
Date: Fri, 15 Nov 2013 14:10:15 GMT

{"message":"Validation Failed","request_id":"a6633df9-4dff-11e3-800a-0be28f962e12","documentation_url":"http://developer.github.com/v3","errors":[{"resource":"ReleaseAsset","code":"already_exists","field":"name"}]}
```
@kasperg

kasperg commented Nov 17, 2013

Copy link
Copy Markdown
Contributor Author

@cursedcoder Thanks for the suggestion. It's a simple solution which should would work.

Before I saw this I started working on some a more serious refactoring which moves JSON encoding from Github\HttpClient\HttpClient to Github\Api\AbstractApi (a67737a).

Let me know what you think goes best with the structure of the project.

Comment thread lib/Github/Api/AbstractApi.php Outdated

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.

curly brace should be on next line

@cursedcoder

Copy link
Copy Markdown
Contributor

@kasperg PR looks good

@kasperg

kasperg commented Nov 17, 2013

Copy link
Copy Markdown
Contributor Author

Great! I'll clean up the commit history a bit and then it should be good to go.

… encoded parameters.

This is needed to support Asset creation where the request body must be the content of the asset.

This involves the following changes
- Updated HttpClient methods to accept an untyped body instead of an array of parameters
- Moved JSON encoding to AbstractApi and updated documentation accordingly
- Updated test cases
Added coresponding unit test for PHP 5.3.4 or newer. SSL extensions are not fully working for 5.3.3. See travis-ci/travis-ci#1385.

Updated documentation to show that Asset creation is now supported.
cursedcoder added a commit that referenced this pull request Nov 19, 2013
Add support for asset creation
@cursedcoder
cursedcoder merged commit 2ff0101 into KnpLabs:master Nov 19, 2013
@cursedcoder

Copy link
Copy Markdown
Contributor

merged, thanks @kasperg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants