Skip to content

Commit 6947590

Browse files
committed
Change the way GET request is build, fix pagination parameter
1 parent 1f644eb commit 6947590

File tree

4 files changed

+52
-10
lines changed

4 files changed

+52
-10
lines changed

lib/Github/HttpClient/CachedHttpClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
6161
*
6262
* {@inheritdoc}
6363
*/
64-
protected function createRequest($httpMethod, $path, $requestBody, array $headers = array())
64+
protected function createRequest($httpMethod, $path, array $parameters = array(), array $headers = array())
6565
{
66-
$request = parent::createRequest($httpMethod, $path, $requestBody, $headers = array());
66+
$request = parent::createRequest($httpMethod, $path, $parameters, $headers = array());
6767

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

lib/Github/HttpClient/HttpClient.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class HttpClient implements HttpClientInterface
4343
public function __construct(array $options = array(), ClientInterface $client = null)
4444
{
4545
$this->options = array_merge($this->options, $options);
46-
$client = $client ?: new GuzzleClient($this->options['base_url'], $this->options);
46+
$client = $client ?: new GuzzleClient($this->options['base_url'], $this->options);
4747
$this->client = $client;
4848

4949
$this->addListener('request.error', array(new ErrorListener($this->options), 'onRequestError'));
@@ -127,11 +127,7 @@ public function put($path, array $parameters = array(), array $headers = array()
127127
*/
128128
public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array())
129129
{
130-
$requestBody = count($parameters) === 0
131-
? null : json_encode($parameters, empty($parameters) ? JSON_FORCE_OBJECT : 0)
132-
;
133-
134-
$request = $this->createRequest($httpMethod, $path, $requestBody, $headers);
130+
$request = $this->createRequest($httpMethod, $path, $parameters, $headers);
135131
$request->addHeaders($headers);
136132

137133
try {
@@ -174,8 +170,24 @@ public function getLastResponse()
174170
return $this->lastResponse;
175171
}
176172

177-
protected function createRequest($httpMethod, $path, $requestBody, array $headers = array())
173+
protected function createRequest($httpMethod, $path, array $parameters = array(), array $headers = array())
178174
{
179-
return $this->client->createRequest($httpMethod, $path, array_merge($this->headers, $headers), $requestBody);
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+
185+
return $this->client->createRequest(
186+
$httpMethod,
187+
$path,
188+
array_merge($this->headers, $headers),
189+
$requestBody,
190+
$options
191+
);
180192
}
181193
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Github\Tests\Functional;
4+
5+
use Github\ResultPager;
6+
7+
/**
8+
* @group functional
9+
*/
10+
class ResultPagerTest extends TestCase
11+
{
12+
/**
13+
* @test
14+
*/
15+
public function shouldPaginateGetRequests()
16+
{
17+
$repositoriesApi = $this->client->api('user');
18+
$repositoriesApi->setPerPage(10);
19+
20+
$pager = new ResultPager($this->client);
21+
22+
$repositories = $pager->fetch($repositoriesApi, 'repositories', array('KnpLabs'));
23+
$this->assertCount(10, $repositories);
24+
25+
$repositoriesApi->setPerPage(20);
26+
$repositories = $pager->fetch($repositoriesApi, 'repositories', array('KnpLabs'));
27+
$this->assertCount(20, $repositories);
28+
}
29+
}

test/Github/Tests/Functional/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class TestCase extends \PHPUnit_Framework_TestCase
1515

1616
public function setUp()
1717
{
18+
// You have to specify authentication here to run full suite
1819
$client = new Client();
1920

2021
try {

0 commit comments

Comments
 (0)