Skip to content

Commit

Permalink
implement PaginatedEndpoint::all()
Browse files Browse the repository at this point in the history
  • Loading branch information
darthmaim committed May 17, 2015
1 parent 3240520 commit f87f1ff
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src/V2/BulkEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace GW2Treasures\GW2Api\V2;

use GuzzleHttp\Client;
use GuzzleHttp\Pool;

trait BulkEndpoint {
Expand Down Expand Up @@ -63,8 +62,8 @@ public function many( array $ids = [] ) {
$responses = Pool::batch( $this->getClient(), $requests, ['pool_size' => 128] );

$result = [];
/** @var \GuzzleHttp\Message\Response $response */
foreach( $responses as $response ) {
/** @var \GuzzleHttp\Message\Response $response */
// TODO: handle errors
$result = array_merge( $result, $this->getResponseAsJson( $response ));
}
Expand Down
2 changes: 1 addition & 1 deletion src/V2/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function createRequest( array $query = [], $url = null, $method = 'GET

/**
* @param RequestInterface $request
* @return mixed
* @return ResponseInterface
*/
protected function request( RequestInterface $request ) {
try {
Expand Down
10 changes: 10 additions & 0 deletions src/V2/EndpointTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@ protected abstract function getClient();
*/
protected abstract function createRequest( array $query = [], $url = null, $method = 'GET', $options = [] );

/**
* @param RequestInterface $request
* @return ResponseInterface
*/
protected abstract function request( RequestInterface $request );

/**
* Returns the json object if the response contains valid json, otherwise null.
*
* @param ResponseInterface $response
* @return mixed|null
*/
protected abstract function getResponseAsJson( ResponseInterface $response );
}
2 changes: 1 addition & 1 deletion src/V2/Interfaces/IPaginatedEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ function all();
* @param int $size
* @return mixed
*/
function page( $index, $size = null );
function page( $page, $size = null );
}
42 changes: 35 additions & 7 deletions src/V2/PaginatedEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace GW2Treasures\GW2Api\V2;

use GuzzleHttp\Pool;
use OutOfRangeException;

trait PaginatedEndpoint {
Expand All @@ -24,17 +25,40 @@ protected function maxPageSize() {
* @return array
*/
public function all() {
// TODO: implement
$size = $this->maxPageSize();
$size = 1;

$firstPageResponse = $this->request( $this->createPaginatedRequest( 0, $size ) );
$total = $firstPageResponse->getHeader('X-Result-Total');

$result = $this->getResponseAsJson( $firstPageResponse );

if( $total <= $size ) {
return $result;
}

$requests = [];
for( $page = 1; $page < ceil( $total / $size ); $page++ ) {
$requests[] = $this->createPaginatedRequest( $page, $size );
}

$responses = Pool::batch( $this->getClient(), $requests, ['pool_size' => 128]);

foreach( $responses as $response ) {
$result = array_merge( $result, $this->getResponseAsJson( $response ));
}

return $result;
}

/**
* Get a single page.
*
* @param int $index
* @param int $page
* @param int $size
* @return mixed
*/
public function page( $index, $size = null ) {
public function page( $page, $size = null ) {
if( is_null( $size )) {
$size = $this->maxPageSize();
}
Expand All @@ -43,12 +67,16 @@ public function page( $index, $size = null ) {
throw new OutOfRangeException('$size has to be between 0 and ' . $this->maxPageSize() . ', was ' . $size );
}

if( $index < 0 ) {
throw new OutOfRangeException('$index has to be 0 or greater');
if( $page < 0 ) {
throw new OutOfRangeException('$page has to be 0 or greater');
}

$request = $this->createRequest(['page' => $index, 'page_size' => $size ]);
$request = $this->createPaginatedRequest( $page, $size );
$response = $this->request( $request );
return $this->getResponseAsJson( $response );
}

protected function createPaginatedRequest( $page, $size ) {
return $this->createRequest(['page' => $page, 'page_size' => $size ]);
}
}

0 comments on commit f87f1ff

Please sign in to comment.