Skip to content

Commit efd69fa

Browse files
committed
Updated Object and some Repo api calls.
1 parent eea5b43 commit efd69fa

File tree

8 files changed

+193
-34
lines changed

8 files changed

+193
-34
lines changed

lib/Github/Api/Api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ abstract class Api implements ApiInterface
1616
* The client
1717
* @var Client
1818
*/
19-
private $client;
19+
protected $client;
2020

2121
/**
2222
* @param Client $client

lib/Github/Api/Object.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,21 @@ class Object extends Api
1818
* @param string $username the username
1919
* @param string $repo the repo
2020
* @param string $treeSHA the tree sha
21+
* @param boolean $resursive the recursive flag
2122
* @return array root tree of the project
2223
*/
23-
public function showTree($username, $repo, $treeSHA)
24+
public function showTree($username, $repo, $treeSHA, $recursive = false)
2425
{
25-
$response = $this->get('tree/show/'.urlencode($username).'/'.urlencode($repo).'/'.urlencode($treeSHA));
26+
$url = 'repos/'.urlencode($username).'/'.urlencode($repo).'/git/trees/'.urlencode($treeSHA);
27+
if ($recursive) {
28+
$url .= '?recursive=1';
29+
}
2630

27-
return $response['tree'];
31+
return $this->get($url);
2832
}
2933

3034
/**
3135
* Lists the data blobs of a tree by tree SHA
32-
* http://develop.github.com/p/object.html#blobs
3336
*
3437
* @param string $username the username
3538
* @param string $repo the repo
@@ -39,14 +42,19 @@ public function showTree($username, $repo, $treeSHA)
3942
*/
4043
public function listBlobs($username, $repo, $treeSHA)
4144
{
42-
$response = $this->get('blob/all/'.urlencode($username).'/'.urlencode($repo).'/'.urlencode($treeSHA));
45+
$tree = $this->showTree($username, $repo, $treeSHA, true);
4346

44-
return $response['blobs'];
47+
if (isset($tree['tree'])) {
48+
$blobs = array_filter($tree['tree'], function ($element) {
49+
return 'blob' === $element['type'];
50+
});
51+
52+
return $blobs;
53+
}
4554
}
4655

4756
/**
4857
* Get the data about a blob by tree SHA and file path.
49-
* http://develop.github.com/p/object.html#blobs
5058
*
5159
* @param string $username the username
5260
* @param string $repo the repo
@@ -56,14 +64,19 @@ public function listBlobs($username, $repo, $treeSHA)
5664
*/
5765
public function showBlob($username, $repo, $treeSHA, $path)
5866
{
59-
$response = $this->get('blob/show/'.urlencode($username).'/'.urlencode($repo).'/'.urlencode($treeSHA).'/'.urlencode($path));
67+
$tree = $this->showTree($username, $repo, $treeSHA, true);
68+
69+
if (isset($tree['tree'])) {
70+
$blobs = array_filter($tree['tree'], function ($element) use ($path) {
71+
return $path === $element['path'];
72+
});
6073

61-
return $response['blob'];
74+
return reset($blobs);
75+
}
6276
}
6377

6478
/**
6579
* Returns the raw text content of the object.
66-
* http://develop.github.com/p/object.html#raw_git_data
6780
*
6881
* @param string $username the username
6982
* @param string $repo the repo
@@ -72,7 +85,9 @@ public function showBlob($username, $repo, $treeSHA, $path)
7285
*/
7386
public function getRawData($username, $repo, $objectSHA)
7487
{
88+
$this->client->setHeaders(array('Accept: application/vnd.github.v3.raw'));
7589
$response = $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/git/blobs/'.urlencode($objectSHA));
90+
$this->client->clearHeaders();
7691

7792
return $response;
7893
}

lib/Github/Api/Repo.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ public function search($query, $language = '', $startPage = 1)
4040
*/
4141
public function getUserRepos($username)
4242
{
43-
$response = $this->get('repos/show/'.urlencode($username));
44-
45-
return $response['repositories'];
43+
return $this->get('users/'.urlencode($username).'/repos');
4644
}
4745

4846
/**
@@ -59,17 +57,14 @@ public function getPushableRepos()
5957

6058
/**
6159
* Get extended information about a repository by its username and repo name
62-
* http://develop.github.com/p/repo.html
6360
*
6461
* @param string $username the user who owns the repo
6562
* @param string $repo the name of the repo
6663
* @return array informations about the repo
6764
*/
6865
public function show($username, $repo)
6966
{
70-
$response = $this->get('repos/show/'.urlencode($username).'/'.urlencode($repo));
71-
72-
return $response['repository'];
67+
return $this->get('repos/'.urlencode($username).'/'.urlencode($repo));
7368
}
7469

7570
/**

lib/Github/Client.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ class Client
4444
*/
4545
protected $httpClient = null;
4646

47+
/**
48+
* HTTP Headers
49+
*
50+
* @var array
51+
*/
52+
private $headers = array();
53+
4754
/**
4855
* The list of loaded API instances
4956
*
@@ -70,13 +77,13 @@ public function __construct(HttpClientInterface $httpClient = null)
7077
*/
7178
public function authenticate($login, $secret, $method = null)
7279
{
73-
$this->httpClient->setOption('auth_method', $method ?: self::AUTH_URL_TOKEN);
80+
$this->getHttpClient()->setOption('auth_method', $method ?: self::AUTH_URL_TOKEN);
7481

7582
if (self::AUTH_HTTP_PASSWORD) {
76-
$this->httpClient->setOption('login', $login)
83+
$this->getHttpClient()->setOption('login', $login)
7784
->setOption('password', $secret);
7885
} else {
79-
$this->httpClient->setOption('token', $secret);
86+
$this->getHttpClient()->setOption('token', $secret);
8087
}
8188
}
8289

@@ -99,7 +106,7 @@ public function deAuthenticate()
99106
*/
100107
public function get($path, array $parameters = array(), $requestOptions = array())
101108
{
102-
return $this->httpClient->get($path, $parameters, $requestOptions);
109+
return $this->getHttpClient()->get($path, $parameters, $requestOptions);
103110
}
104111

105112
/**
@@ -113,7 +120,7 @@ public function get($path, array $parameters = array(), $requestOptions = array(
113120
*/
114121
public function post($path, array $parameters = array(), $requestOptions = array())
115122
{
116-
return $this->httpClient->post($path, $parameters, $requestOptions);
123+
return $this->getHttpClient()->post($path, $parameters, $requestOptions);
117124
}
118125

119126
/**
@@ -123,6 +130,8 @@ public function post($path, array $parameters = array(), $requestOptions = array
123130
*/
124131
public function getHttpClient()
125132
{
133+
$this->httpClient->setHeaders($this->headers);
134+
126135
return $this->httpClient;
127136
}
128137

@@ -259,4 +268,14 @@ public function getApi($name)
259268
{
260269
return $this->apis[$name];
261270
}
271+
272+
public function clearHeaders()
273+
{
274+
$this->setHeaders(array());
275+
}
276+
277+
public function setHeaders($headers)
278+
{
279+
$this->headers = $headers;
280+
}
262281
}

lib/Github/HttpClient/HttpClient.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ class HttpClient implements HttpClientInterface
3535
*/
3636
protected static $history = array();
3737

38+
/**
39+
* @var array
40+
*/
41+
protected $headers = array();
42+
3843
/**
3944
* @var Buzz\Browser
4045
*/
@@ -64,6 +69,11 @@ public function __construct(array $options = array(), Browser $browser = null)
6469
}
6570
}
6671

72+
public function setHeaders($headers)
73+
{
74+
$this->headers = $headers;
75+
}
76+
6777
/**
6878
* Change an option value.
6979
*
@@ -118,10 +128,7 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
118128
// get encoded response
119129
$response = $this->doRequest($url, $parameters, $httpMethod, $options);
120130

121-
// decode response
122-
$response = $this->decodeResponse($response['response']);
123-
124-
return $response;
131+
return $this->decodeResponse($response['response']);
125132
}
126133

127134
/**
@@ -136,7 +143,7 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
136143
*/
137144
protected function doRequest($url, array $parameters = array(), $httpMethod = 'GET', array $options = array())
138145
{
139-
$response = $this->browser->call($url, $httpMethod);
146+
$response = $this->browser->call($url, $httpMethod, $this->headers);
140147

141148
$this->checkApiLimit($response);
142149

@@ -162,7 +169,7 @@ protected function decodeResponse($response)
162169
$content = json_decode($response, true);
163170

164171
if (JSON_ERROR_NONE !== json_last_error()) {
165-
throw new \RuntimeException('Response was not valid json, error: '.json_last_error());
172+
return $response;
166173
}
167174

168175
return $content;

lib/Github/HttpClient/HttpClientInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,11 @@ function post($path, array $parameters = array(), array $options = array());
4141
* @return HttpClientInterface The current object instance
4242
*/
4343
function setOption($name, $value);
44+
45+
/**
46+
* Set HTTP headers
47+
*
48+
* @param array
49+
*/
50+
function setHeaders($headers);
4451
}

test/Github/Tests/Functional/ObjectTest.php

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,77 @@
66

77
class ObjectTest extends \PHPUnit_Framework_TestCase
88
{
9-
public function testGetRawData()
9+
/**
10+
* @test
11+
*/
12+
public function shouldGetRawData()
1013
{
11-
$username = 'ornicar';
14+
$username = 'KnpLabs';
1215
$repo = 'php-github-api';
13-
$sha1 = 'ab9f17c8ed85c0dd9f33ba8d2d61ebb570768158';
16+
$sha = 'dd2630b84b948e7ed9a58f9828abb8d6dea2b2a9';
1417

1518
$github = new Client();
16-
$data = $github->getObjectApi()->getRawData($username, $repo, $sha1);
19+
$data = $github->getObjectApi()->getRawData($username, $repo, $sha);
1720

18-
$this->assertRegexp('/Release version 3\.1/', $data);
21+
$this->assertRegexp('/Copyright/', $data);
22+
}
23+
24+
/**
25+
* @test
26+
*/
27+
public function shouldShowHeadTree()
28+
{
29+
$username = 'KnpLabs';
30+
$repo = 'php-github-api';
31+
$sha = 'HEAD';
32+
33+
$github = new Client();
34+
$tree = $github->getObjectApi()->showTree($username, $repo, $sha);
35+
36+
$this->assertArrayHasKey('url', $tree);
37+
$this->assertArrayHasKey('sha', $tree);
38+
$this->assertArrayHasKey('tree', $tree);
39+
}
40+
41+
/**
42+
* @test
43+
*/
44+
public function shouldListBlobsFromTree()
45+
{
46+
$username = 'KnpLabs';
47+
$repo = 'php-github-api';
48+
$sha = 'HEAD';
49+
50+
$github = new Client();
51+
$blobs = $github->getObjectApi()->listBlobs($username, $repo, $sha);
52+
53+
$blob = reset($blobs);
54+
55+
$this->assertArrayHasKey('url', $blob);
56+
$this->assertEquals('blob', $blob['type']);
57+
$this->assertArrayHasKey('size', $blob);
58+
$this->assertArrayHasKey('path', $blob);
59+
$this->assertArrayHasKey('sha', $blob);
60+
$this->assertArrayHasKey('mode', $blob);
61+
}
62+
63+
/**
64+
* @test
65+
*/
66+
public function shouldShowBlob()
67+
{
68+
$username = 'KnpLabs';
69+
$repo = 'php-github-api';
70+
$sha = 'a9300ff6c7da0b70913b9a39d59a416b0f6c0c5a';
71+
72+
$github = new Client();
73+
$blob = $github->getObjectApi()->showBlob($username, $repo, $sha, 'CHANGELOG');
74+
75+
$this->assertArrayHasKey('url', $blob);
76+
$this->assertEquals('blob', $blob['type']);
77+
$this->assertEquals('CHANGELOG', $blob['path']);
78+
$this->assertArrayHasKey('size', $blob);
79+
$this->assertArrayHasKey('sha', $blob);
80+
$this->assertArrayHasKey('mode', $blob);
1981
}
2082
}

0 commit comments

Comments
 (0)