Skip to content

Commit

Permalink
Merge pull request #8 from bookboon/feature/responseObj
Browse files Browse the repository at this point in the history
added response entities on all entity requests
  • Loading branch information
AthanasiosKoumpouras committed May 18, 2017
2 parents dd86ec7 + bfa246c commit 68cf848
Show file tree
Hide file tree
Showing 19 changed files with 326 additions and 67 deletions.
4 changes: 3 additions & 1 deletion src/Bookboon.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
*/

use Bookboon\Api\Cache\Cache;
use Bookboon\Api\Client\BookboonResponse;
use Bookboon\Api\Client\Client;
use Bookboon\Api\Client\Headers;
use Bookboon\Api\Client\OauthClient;
use Psr\Http\Message\ResponseInterface;

class Bookboon
{
Expand Down Expand Up @@ -62,7 +64,7 @@ public static function create($appId, $appSecret, array $scopes, array $headers
* @param array $variables
* @param $httpMethod
* @param bool $shouldCache
* @return array
* @return BookboonResponse
*/
public function rawRequest($url, array $variables = [], $httpMethod = Client::HTTP_GET, $shouldCache = true)
{
Expand Down
11 changes: 9 additions & 2 deletions src/Client/BasicAuthClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct($apiId, $apiSecret, Headers $headers, Cache $cache =
* @oaram string $contentType
*
* @param string $contentType
* @return array
* @return BookboonResponse
* @throws ApiGeneralException
* @throws ApiTimeoutException
*/
Expand Down Expand Up @@ -93,7 +93,14 @@ protected function executeQuery($url, $type = self::HTTP_GET, $variables = array

curl_close($http);

return $this->handleResponse(substr($response, $headersSize), substr($response, 0, $headersSize), $httpStatus, $url);
$responseArray = $this->handleResponse(
substr($response, $headersSize),
substr($response, 0, $headersSize),
$httpStatus,
$url
);

return new BookboonResponse($responseArray, $headers);
}

/**
Expand Down
66 changes: 66 additions & 0 deletions src/Client/BookboonResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Bookboon\Api\Client;

use Bookboon\Api\Entity\EntityStore;

class BookboonResponse
{
/**
* @var array
*/
protected $returnArray;

/**
* @var array
*/
protected $headers;

/**
* @var array
*/
protected $entityStore;

/**
* BookboonResponse constructor.
* @param $returnArray
* @param $headers
*/
public function __construct($returnArray, $headers)
{
$this->returnArray = $returnArray;
$this->headers = $headers;
}

/**
* @return array
*/
public function getHeaders()
{
return $this->headers;
}

/**
* @return array
*/
public function getReturnArray()
{
return $this->returnArray;
}

/**
* @return array
*/
public function getEntityStore()
{
return $this->entityStore;
}

/**
* @param $entityStore
*/
public function setEntityStore(EntityStore $entityStore)
{
$this->entityStore = $entityStore;
}
}
9 changes: 8 additions & 1 deletion src/Client/OauthClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,14 @@ protected function executeQuery($url, $type = Client::HTTP_GET, $variables = arr
$response = $e->getResponse();
}

return $this->handleResponse($response->getBody()->getContents(), $response->getHeaders(), $response->getStatusCode(), $url);
$responseArray = $this->handleResponse(
$response->getBody()->getContents(),
$response->getHeaders(),
$response->getStatusCode(),
$url
);

return new BookboonResponse($responseArray, $response->getHeaders());
}

/**
Expand Down
33 changes: 25 additions & 8 deletions src/Entity/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
namespace Bookboon\Api\Entity;

use Bookboon\Api\Bookboon;
use Bookboon\Api\Client\BookboonResponse;
use Bookboon\Api\Exception\BadUUIDException;

class Author extends Entity
{
/**
* Get Author.
*
* @param Bookboon $bookboon
* @param string $authorId
* @return Author
* @param $authorId
* @return BookboonResponse
* @throws BadUUIDException
*/
public static function get(Bookboon $bookboon, $authorId)
Expand All @@ -21,15 +20,25 @@ public static function get(Bookboon $bookboon, $authorId)
throw new BadUUIDException("UUID Not Formatted Correctly");
}

return new static($bookboon->rawRequest("/authors/$authorId"));
$bResponse = $bookboon->rawRequest("/authors/$authorId");

$bResponse->setEntityStore(
new EntityStore(
[
new static($bResponse->getReturnArray())
]
)
);

return $bResponse;
}

/**
* Get Author by book.
*
* @param Bookboon $bookboon
* @param string $bookId
* @return Author[]
* @return BookboonResponse
* @throws BadUUIDException
*/
public function getByBookId(Bookboon $bookboon, $bookId)
Expand All @@ -38,9 +47,17 @@ public function getByBookId(Bookboon $bookboon, $bookId)
throw new BadUUIDException("UUID Not Formatted Correctly");
}

$authors = $bookboon->rawRequest("/books/$bookId/authors");
$bResponse = $bookboon->rawRequest("/books/$bookId/authors");

$bResponse->setEntityStore(
new EntityStore(
[
static::getEntitiesFromArray($bResponse->getReturnArray())
]
)
);

return static::getEntitiesFromArray($authors);
return $bResponse;
}

protected function isValid(array $array)
Expand Down
85 changes: 64 additions & 21 deletions src/Entity/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Bookboon\Api\Entity;

use Bookboon\Api\Bookboon;
use Bookboon\Api\Client\BookboonResponse;
use Bookboon\Api\Client\Client;

class Book extends Entity
Expand All @@ -21,16 +22,24 @@ class Book extends Entity
const FORMAT_M3U = 'm3u';

/**
* Get Book object.
*
* @param Bookboon $bookboon
* @param string|array $bookId uuid for book
* @param bool $extendedMetadata bool include reviews and similar books
* @return Book
* @param $bookId
* @param bool $extendedMetadata
* @return BookboonResponse
*/
public static function get(Bookboon $bookboon, $bookId, $extendedMetadata = false)
{
return static::objectTransformer($bookboon->rawRequest("/books/$bookId", array('extendedMetadata' => $extendedMetadata ? 'true' : 'false')));
$bResponse = $bookboon->rawRequest("/books/$bookId", array('extendedMetadata' => $extendedMetadata ? 'true' : 'false'));

$bResponse->setEntityStore(
new EntityStore(
[
static::objectTransformer($bResponse->getReturnArray())
]
)
);

return $bResponse;
}

/**
Expand All @@ -39,7 +48,7 @@ public static function get(Bookboon $bookboon, $bookId, $extendedMetadata = fals
* @param Bookboon $bookboon
* @param string[] $bookIds
* @param bool $extendedMetadata
* @return Book[]
* @return BookboonResponse
*/
public static function getMultiple(Bookboon $bookboon, array $bookIds, $extendedMetadata = false)
{
Expand All @@ -48,15 +57,25 @@ public static function getMultiple(Bookboon $bookboon, array $bookIds, $extended
'extendedMetadata' => $extendedMetadata ? 'true' : 'false'
);

return static::getEntitiesFromArray($bookboon->rawRequest("/books", $variables));
$bResponse = $bookboon->rawRequest("/books", $variables);

$bResponse->setEntityStore(
new EntityStore(
[
static::getEntitiesFromArray($bResponse->getReturnArray())
]
)
);

return $bResponse;
}

/**
* Get many books
*
* @param Bookboon $bookboon
* @param bool $extendedMetadata
* @return Book[]
* @return BookboonResponse
*/
public static function getAll(Bookboon $bookboon, $extendedMetadata = false)
{
Expand All @@ -65,7 +84,17 @@ public static function getAll(Bookboon $bookboon, $extendedMetadata = false)
'extendedMetadata' => $extendedMetadata ? 'true' : 'false'
);

return static::getEntitiesFromArray($bookboon->rawRequest("/books", $variables));
$bResponse = $bookboon->rawRequest("/books", $variables);

$bResponse->setEntityStore(
new EntityStore(
[
static::getEntitiesFromArray($bResponse->getReturnArray())
]
)
);

return $bResponse;
}

/**
Expand Down Expand Up @@ -105,9 +134,10 @@ public static function getEntitiesFromArray(array $array)
public static function getDownloadUrl(Bookboon $bookboon, $bookId, array $variables, $format = self::FORMAT_PDF)
{
$variables['format'] = $format;
$download = $bookboon->rawRequest("/books/$bookId/download", $variables, Client::HTTP_POST);

return $download['url'];
$bResponse = $bookboon->rawRequest("/books/$bookId/download", $variables, Client::HTTP_POST);

return $bResponse->getReturnArray()['url'];
}

/**
Expand All @@ -117,16 +147,21 @@ public static function getDownloadUrl(Bookboon $bookboon, $bookId, array $variab
* @param $query string to search for
* @param int $limit results to return per page
* @param int $offset offset of results
* @return Book[]
* @return BookboonResponse
*/
public static function search(Bookboon $bookboon, $query, $limit = 10, $offset = 0)
{
$search = $bookboon->rawRequest('/search', array('q' => $query, 'limit' => $limit, 'offset' => $offset));
if (count($search) === 0) {
return array();
}
$bResponse = $bookboon->rawRequest('/search', array('q' => $query, 'limit' => $limit, 'offset' => $offset));

return Book::getEntitiesFromArray($search);
$bResponse->setEntityStore(
new EntityStore(
[
Book::getEntitiesFromArray($bResponse->getReturnArray())
]
)
);

return $bResponse;
}

/**
Expand All @@ -136,13 +171,21 @@ public static function search(Bookboon $bookboon, $query, $limit = 10, $offset =
* @param string $bookType
* @param array $bookIds array of book ids to base recommendations on, can be empty
* @param int $limit
* @return Book[]
* @return BookboonResponse
*/
public static function recommendations(Bookboon $bookboon, array $bookIds = array(), $limit = 5, $bookType = 'pdf')
{
$recommendations = $bookboon->rawRequest('/recommendations', array('limit' => $limit, 'book' => $bookIds, 'bookType' => $bookType));
$bResponse = $bookboon->rawRequest('/recommendations', array('limit' => $limit, 'book' => $bookIds, 'bookType' => $bookType));

$bResponse->setEntityStore(
new EntityStore(
[
Book::getEntitiesFromArray($bResponse->getReturnArray())
]
)
);

return Book::getEntitiesFromArray($recommendations);
return $bResponse;
}

protected function isValid(array $array)
Expand Down
Loading

0 comments on commit 68cf848

Please sign in to comment.