diff --git a/README.md b/README.md index 4a1ce26..10f8148 100644 --- a/README.md +++ b/README.md @@ -26,8 +26,12 @@ php composer.phar require php-http/guzzle6-adapter # if you want to use HTTPlug ### Usage ```php - // Create Guzzle connection - $connection = new \Core23\LastFm\Connection\GuzzleConnection(API_KEY, SHARED_SECRET); + // Get HTTPlug client and message factory + $client = \Http\Discovery\HttpClientDiscovery::find(); + $messageFactory = \Http\Discovery\MessageFactoryDiscovery::find(); + + // Create connection + $connection = new \Core23\LastFm\Connection\HTTPlugConnection($client, $messageFactory); // Auth user to get a token // http://www.last.fm/api/auth/?api_key=API_KEY diff --git a/composer.json b/composer.json index 8bb3ae8..8013f5d 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,6 @@ "require-dev": { "phpunit/phpunit": "^5.7", "friendsofphp/php-cs-fixer": "^2.0", - "guzzlehttp/guzzle": "^5.0 || ^6.0", "php-http/httplug-bundle": "^1.1", "php-http/mock-client": "^0.3", "sllh/php-cs-fixer-styleci-bridge": "^2.0" diff --git a/src/Connection/GuzzleConnection.php b/src/Connection/GuzzleConnection.php deleted file mode 100644 index aabac24..0000000 --- a/src/Connection/GuzzleConnection.php +++ /dev/null @@ -1,108 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Core23\LastFm\Connection; - -use Core23\LastFm\Exception\ApiException; -use GuzzleHttp\Client; -use GuzzleHttp\Exception\ClientException; -use Psr\Http\Message\ResponseInterface; - -final class GuzzleConnection extends AbstractConnection -{ - /** - * @var Client - */ - private $client; - - /** - * {@inheritdoc} - */ - public function loadPage(string $url): string - { - $response = $this->getClient()->request('GET', $url); - - return $response->getBody()->getContents(); - } - - /** - * @return Client - */ - protected function getClient(): Client - { - if (null === $this->client) { - $this->client = new Client(array('base_uri' => $this->uri)); - } - - return $this->client; - } - - /** - * {@inheritdoc} - */ - protected function call(array $params, string $requestMethod = 'GET'): array - { - $params = array_merge($params, array('format' => 'json')); - $data = $this->buildParameter($params); - - try { - if ($requestMethod == 'POST') { - $response = $this->getClient()->request($requestMethod, '', array( - 'body' => $data, - )); - } else { - $response = $this->getClient()->request($requestMethod, '?'.$data); - } - - // Parse response - return $this->parseResponse($response); - } catch (ClientException $e) { - $response = $e->getResponse(); - if (null === $response) { - throw new ApiException('Client exception with empty response occurred.', 500); - } - - return $this->parseResponse($response); - } catch (ApiException $e) { - throw $e; - } catch (\Exception $e) { - throw new ApiException('Technical error occurred.', 500); - } - } - - /** - * @param ResponseInterface $response - * - * @return array - * - * @throws ApiException - */ - private function parseResponse(ResponseInterface $response): array - { - $array = json_decode($response->getBody()->getContents(), true); - - if (is_array($array) && array_key_exists('error', $array) && array_key_exists('message', $array)) { - throw new ApiException($array['message'], $array['error']); - } - - return $array; - } - - /** - * Builds request parameter. - * - * @param array $parameter - * - * @return string - */ - private function buildParameter(array $parameter): string - { - return http_build_query($parameter); - } -}