Skip to content

Commit

Permalink
Merge branch '2.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-gribanov committed Jul 31, 2017
2 parents 62c9f81 + da71cc5 commit 42303c9
Show file tree
Hide file tree
Showing 11 changed files with 369 additions and 181 deletions.
16 changes: 16 additions & 0 deletions README.md
Expand Up @@ -98,6 +98,22 @@ Destroy a message ([docs](https://shikimori.org/api/doc/1.0/messages/destroy))
$browser->delete('messages/12');
```

Catch exceptions

```php
use AnimeDb\Bundle\ShikimoriBrowserBundle\Exception\NotFoundException;

try {
$content = $browser->get('animes/1');
} catch (NotFoundException $e) {
// anime not found
} catch (\Exception $e) {
// other exceptions
}
```

You can customize request options. See [Guzzle Documentation](http://docs.guzzlephp.org/en/stable/request-options.html).

License
-------

Expand Down
6 changes: 3 additions & 3 deletions src/DependencyInjection/AnimeDbShikimoriBrowserExtension.php
Expand Up @@ -30,9 +30,9 @@ public function load(array $configs, ContainerBuilder $container)

$container
->getDefinition('anime_db.shikimori.browser')
->replaceArgument(1, $config['host'])
->replaceArgument(2, $config['prefix'])
->replaceArgument(3, $config['client'])
->replaceArgument(2, $config['host'])
->replaceArgument(3, $config['prefix'])
->replaceArgument(4, $config['client'])
;
}
}
40 changes: 40 additions & 0 deletions src/Exception/ErrorException.php
@@ -0,0 +1,40 @@
<?php

/**
* AnimeDb package.
*
* @author Peter Gribanov <info@peter-gribanov.ru>
* @copyright Copyright (c) 2011, Peter Gribanov
* @license http://opensource.org/licenses/GPL-3.0 GPL v3
*/

namespace AnimeDb\Bundle\ShikimoriBrowserBundle\Exception;

class ErrorException extends \RuntimeException
{
/**
* @param string $massage
* @param int $code
*
* @return ErrorException
*/
public static function failed($massage, $code)
{
if ($massage) {
return new self(sprintf('Server returned error "%s".', $massage), $code);
} else {
return new self('Server returned an error.', $code);
}
}

/**
* @param string $massage
* @param int $code
*
* @return ErrorException
*/
public static function invalidResponse($massage, $code)
{
return new self(sprintf('Failed to parse response due to "%s" error.', $massage), $code);
}
}
22 changes: 22 additions & 0 deletions src/Exception/NotFoundException.php
@@ -0,0 +1,22 @@
<?php

/**
* AnimeDb package.
*
* @author Peter Gribanov <info@peter-gribanov.ru>
* @copyright Copyright (c) 2011, Peter Gribanov
* @license http://opensource.org/licenses/GPL-3.0 GPL v3
*/

namespace AnimeDb\Bundle\ShikimoriBrowserBundle\Exception;

class NotFoundException extends ErrorException
{
/**
* @return NotFoundException
*/
public static function page()
{
return new self('Page not found.');
}
}
6 changes: 5 additions & 1 deletion src/Resources/config/services.yml
@@ -1,8 +1,12 @@
services:
anime_db.shikimori.browser:
class: AnimeDb\Bundle\ShikimoriBrowserBundle\Service\Browser
arguments: [ '@anime_db.shikimori.browser.client', ~, ~, ~ ]
arguments: [ '@anime_db.shikimori.browser.client', '@anime_db.shikimori.browser.error_detector', ~, ~, ~ ]

anime_db.shikimori.browser.client:
class: GuzzleHttp\Client
public: false

anime_db.shikimori.browser.error_detector:
class: AnimeDb\Bundle\ShikimoriBrowserBundle\Service\ErrorDetector
public: false
48 changes: 23 additions & 25 deletions src/Service/Browser.php
Expand Up @@ -10,11 +10,20 @@

namespace AnimeDb\Bundle\ShikimoriBrowserBundle\Service;

use AnimeDb\Bundle\ShikimoriBrowserBundle\Service\Exception\ResponseException;
use GuzzleHttp\Client;
use GuzzleHttp\Client as HttpClient;

class Browser
{
/**
* @var HttpClient
*/
private $client;

/**
* @var ErrorDetector
*/
private $detector;

/**
* @var string
*/
Expand All @@ -31,19 +40,16 @@ class Browser
private $app_client;

/**
* @var Client
* @param HttpClient $client
* @param ErrorDetector $detector
* @param string $host
* @param string $prefix
* @param string $app_client
*/
private $client;

/**
* @param Client $client
* @param string $host
* @param string $prefix
* @param string $app_client
*/
public function __construct(Client $client, $host, $prefix, $app_client)
public function __construct(HttpClient $client, ErrorDetector $detector, $host, $prefix, $app_client)
{
$this->client = $client;
$this->detector = $detector;
$this->host = $host;
$this->prefix = $prefix;
$this->app_client = $app_client;
Expand Down Expand Up @@ -114,22 +120,14 @@ public function delete($resource, array $options = [])
private function request($method, $path = '', array $options = [])
{
$options['headers'] = array_merge(
['User-Agent' => $this->app_client],
[
'User-Agent' => $this->app_client,
],
isset($options['headers']) ? $options['headers'] : []
);

try {
$response = $this->client->request($method, $this->host.$this->prefix.$path, $options);
} catch (\Exception $e) {
throw ResponseException::failed($this->host, $e);
}

$body = json_decode($response->getBody()->getContents(), true);

if (json_last_error() !== JSON_ERROR_NONE || !is_array($body)) {
throw ResponseException::invalidResponse($this->host);
}
$response = $this->client->request($method, $this->host.$this->prefix.$path, $options);

return $body;
return $this->detector->detect($response);
}
}
51 changes: 51 additions & 0 deletions src/Service/ErrorDetector.php
@@ -0,0 +1,51 @@
<?php

/**
* AnimeDb package.
*
* @author Peter Gribanov <info@peter-gribanov.ru>
* @copyright Copyright (c) 2011, Peter Gribanov
* @license http://opensource.org/licenses/GPL-3.0 GPL v3
*/

namespace AnimeDb\Bundle\ShikimoriBrowserBundle\Service;

use AnimeDb\Bundle\ShikimoriBrowserBundle\Exception\ErrorException;
use AnimeDb\Bundle\ShikimoriBrowserBundle\Exception\NotFoundException;
use Psr\Http\Message\ResponseInterface;

class ErrorDetector
{
/**
* @param ResponseInterface $response
*
* @return array
*/
public function detect(ResponseInterface $response)
{
if ($response->getStatusCode() == 404) {
throw NotFoundException::page();
}

$content = $response->getBody()->getContents();

if ($content == '') {
return [];
}

$data = json_decode($content, true);

if (json_last_error() !== JSON_ERROR_NONE) {
throw ErrorException::invalidResponse(json_last_error_msg(), json_last_error());
}

if (!empty($data['code'])) {
throw ErrorException::failed(
isset($data['message']) ? $data['message'] : '',
$data['code']
);
}

return (array) $data;
}
}
35 changes: 0 additions & 35 deletions src/Service/Exception/ResponseException.php

This file was deleted.

Expand Up @@ -74,19 +74,19 @@ public function testLoad(array $config, $host, $prefix, $client)
$browser
->expects($this->at(0))
->method('replaceArgument')
->with(1, $host)
->with(2, $host)
->will($this->returnSelf())
;
$browser
->expects($this->at(1))
->method('replaceArgument')
->with(2, $prefix)
->with(3, $prefix)
->will($this->returnSelf())
;
$browser
->expects($this->at(2))
->method('replaceArgument')
->with(3, $client)
->with(4, $client)
->will($this->returnSelf())
;

Expand Down

0 comments on commit 42303c9

Please sign in to comment.