From f87f1ffed1ffa7fd533f74b38c3bd292b6f3a977 Mon Sep 17 00:00:00 2001 From: darthmaim Date: Sun, 17 May 2015 22:33:13 +0200 Subject: [PATCH] implement PaginatedEndpoint::all() --- src/V2/BulkEndpoint.php | 3 +- src/V2/Endpoint.php | 2 +- src/V2/EndpointTrait.php | 10 ++++++ src/V2/Interfaces/IPaginatedEndpoint.php | 2 +- src/V2/PaginatedEndpoint.php | 42 ++++++++++++++++++++---- 5 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/V2/BulkEndpoint.php b/src/V2/BulkEndpoint.php index 05c373d..0712d47 100644 --- a/src/V2/BulkEndpoint.php +++ b/src/V2/BulkEndpoint.php @@ -2,7 +2,6 @@ namespace GW2Treasures\GW2Api\V2; -use GuzzleHttp\Client; use GuzzleHttp\Pool; trait BulkEndpoint { @@ -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 )); } diff --git a/src/V2/Endpoint.php b/src/V2/Endpoint.php index a9af7f2..33fa399 100644 --- a/src/V2/Endpoint.php +++ b/src/V2/Endpoint.php @@ -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 { diff --git a/src/V2/EndpointTrait.php b/src/V2/EndpointTrait.php index 9aa0134..e7d0c75 100644 --- a/src/V2/EndpointTrait.php +++ b/src/V2/EndpointTrait.php @@ -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 ); } diff --git a/src/V2/Interfaces/IPaginatedEndpoint.php b/src/V2/Interfaces/IPaginatedEndpoint.php index 44ee12d..586cbd5 100644 --- a/src/V2/Interfaces/IPaginatedEndpoint.php +++ b/src/V2/Interfaces/IPaginatedEndpoint.php @@ -25,5 +25,5 @@ function all(); * @param int $size * @return mixed */ - function page( $index, $size = null ); + function page( $page, $size = null ); } diff --git a/src/V2/PaginatedEndpoint.php b/src/V2/PaginatedEndpoint.php index 70e3c63..3c429cb 100644 --- a/src/V2/PaginatedEndpoint.php +++ b/src/V2/PaginatedEndpoint.php @@ -2,6 +2,7 @@ namespace GW2Treasures\GW2Api\V2; +use GuzzleHttp\Pool; use OutOfRangeException; trait PaginatedEndpoint { @@ -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(); } @@ -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 ]); + } }