Skip to content

Commit

Permalink
[MAJOR] Refactored client
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Apr 12, 2019
1 parent 6256743 commit 909c289
Show file tree
Hide file tree
Showing 19 changed files with 630 additions and 429 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Expand Up @@ -8,4 +8,4 @@ parameters:
- vendor-bin/test/vendor/autoload.php

ignoreErrors:
- '#PHPDoc tag \@throws with type Http\\Client\\Exception is not subtype of Throwable#'
- '#Parameter \#3 \$previous of class Core23\\LastFm\\Exception\\ApiException constructor expects Throwable\|null, string given.#'
100 changes: 49 additions & 51 deletions src/Connection/AbstractConnection.php → src/Client/ApiClient.php
@@ -1,55 +1,62 @@
<?php

declare(strict_types=1);

/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Core23\LastFm\Connection;
namespace Core23\LastFm\Client;

use Core23\LastFm\Connection\ConnectionInterface;
use Core23\LastFm\Exception\ApiException;
use Core23\LastFm\Exception\NotFoundException;
use Core23\LastFm\Session\SessionInterface;

abstract class AbstractConnection implements ConnectionInterface
final class ApiClient implements ApiClientInterface
{
/**
* Default Endpoint.
* @var ConnectionInterface
*/
public const DEFAULT_WS_ENDPOINT = 'http://ws.audioscrobbler.com/2.0/';
private $connection;

/**
* @var string
*/
protected $apiKey;
private $apiKey;

/**
* @var string
*/
protected $sharedSecret;
private $sharedSecret;

/**
* @var string
* @param ConnectionInterface $connection
* @param string $apiKey
* @param string $sharedSecret
*/
protected $uri;
public function __construct(ConnectionInterface $connection, string $apiKey, string $sharedSecret)
{
$this->connection = $connection;
$this->apiKey = $apiKey;
$this->sharedSecret = $sharedSecret;
}

/**
* @param string $apikey
* @param string $sharedSecret
* @param string $uri
* {@inheritdoc}
*/
public function __construct(string $apikey, string $sharedSecret, string $uri = null)
public function getApiKey(): string
{
if (null === $uri) {
$uri = static::DEFAULT_WS_ENDPOINT;
}
return $this->apiKey;
}

$this->apiKey = $apikey;
$this->sharedSecret = $sharedSecret;
$this->uri = $uri;
/**
* {@inheritdoc}
*/
public function getSharedSecret(): string
{
return $this->sharedSecret;
}

/**
Expand All @@ -75,7 +82,7 @@ public function signedCall(string $method, array $params = [], SessionInterface
// Sign parameter
$params['api_sig'] = $this->signParams($params);

return $this->call($params, $requestMethod);
return $this->call($method, $params);
}

/**
Expand All @@ -93,37 +100,9 @@ public function unsignedCall(string $method, array $params = [], string $request
$params = $this->filterNull($params);
$params = $this->encodeUTF8($params);

return $this->call($params, $requestMethod);
}

/**
* {@inheritdoc}
*/
public function getApiKey(): string
{
return $this->apiKey;
}

/**
* {@inheritdoc}
*/
public function getSharedSecret(): string
{
return $this->sharedSecret;
return $this->call($method, $params);
}

/**
* Performs the webservice call.
*
* @param array $params
* @param string $requestMethod
*
* @throws ApiException
*
* @return array
*/
abstract protected function call(array $params, string $requestMethod = 'GET'): array;

/**
* Filter null values.
*
Expand All @@ -133,7 +112,7 @@ abstract protected function call(array $params, string $requestMethod = 'GET'):
*/
private function filterNull(array $object): array
{
return array_filter($object, function ($val) {
return array_filter($object, static function ($val) {
return null !== $val;
});
}
Expand Down Expand Up @@ -171,4 +150,23 @@ private function signParams(array $params): string

return md5($signature);
}

/**
* @param string $method
* @param array $params
*
* @return array
*/
private function call(string $method, array $params): array
{
try {
return $this->connection->call($method, $params);
} catch (ApiException $e) {
if (6 === (int) $e->getCode()) {
throw new NotFoundException('No entity was found for your request.', $e->getCode(), $e);
}

throw $e;
}
}
}
57 changes: 57 additions & 0 deletions src/Client/ApiClientInterface.php
@@ -0,0 +1,57 @@
<?php

/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Core23\LastFm\Client;

use Core23\LastFm\Exception\ApiException;
use Core23\LastFm\Session\SessionInterface;

interface ApiClientInterface
{
/**
* Calls the API with signed session.
*
* @param string $method
* @param array $params
* @param SessionInterface|null $session
* @param string $requestMethod
*
* @throws ApiException
*
* @return array
*/
public function signedCall(string $method, array $params = [], SessionInterface $session = null, $requestMethod = 'GET'): array;

/**
* Calls the API unsigned.
*
* @param string $method
* @param array $params
* @param string $requestMethod
*
* @throws ApiException
*
* @return array
*/
public function unsignedCall(string $method, array $params = [], string $requestMethod = 'GET'): array;

/**
* Get the api key.
*
* @return string
*/
public function getApiKey(): string;

/**
* Get the shared secret.
*
* @return string
*/
public function getSharedSecret(): string;
}
36 changes: 6 additions & 30 deletions src/Connection/ConnectionInterface.php
Expand Up @@ -12,26 +12,16 @@
namespace Core23\LastFm\Connection;

use Core23\LastFm\Exception\ApiException;
use Core23\LastFm\Session\SessionInterface;

interface ConnectionInterface
{
/**
* Calls the API with signed session.
*
* @param string $method
* @param array $params
* @param SessionInterface|null $session
* @param string $requestMethod
*
* @throws ApiException
*
* @return array
* Default Endpoint.
*/
public function signedCall(string $method, array $params = [], SessionInterface $session = null, $requestMethod = 'GET'): array;
public const DEFAULT_ENDPOINT = 'http://ws.audioscrobbler.com/2.0/';

/**
* Calls the API unsigned.
* Calls the API.
*
* @param string $method
* @param array $params
Expand All @@ -41,28 +31,14 @@ public function signedCall(string $method, array $params = [], SessionInterface
*
* @return array
*/
public function unsignedCall(string $method, array $params = [], string $requestMethod = 'GET'): array;
public function call(string $method, array $params = [], string $requestMethod = 'GET'): array;

/**
* Loads a page and returns the page body.
*
* @param string $url
*
* @throws ApiException
*
* @return string|null
*/
public function getPageBody(string $url): ?string;

/**
* Get the api key.
*
* @return string
*/
public function getApiKey(): string;

/**
* Get the shared secret.
*
* @return string
*/
public function getSharedSecret(): string;
}
37 changes: 20 additions & 17 deletions src/Connection/HTTPlugConnection.php
Expand Up @@ -17,7 +17,7 @@
use Http\Message\MessageFactory;
use Psr\Http\Message\ResponseInterface;

final class HTTPlugConnection extends AbstractConnection
final class HTTPlugConnection implements ConnectionInterface
{
/**
* @var HttpClient
Expand All @@ -29,34 +29,37 @@ final class HTTPlugConnection extends AbstractConnection
*/
private $messageFactory;

/**
* @var string
*/
private $endpoint;

/**
* Initialize client.
*
* @param HttpClient $client
* @param MessageFactory $messageFactory
* @param string $apikey
* @param string $sharedSecret
* @param string $uri
* @param string $endpoint
*/
public function __construct(HttpClient $client, MessageFactory $messageFactory, string $apikey, string $sharedSecret, string $uri = null)
public function __construct(HttpClient $client, MessageFactory $messageFactory, string $endpoint = ConnectionInterface::DEFAULT_ENDPOINT)
{
parent::__construct($apikey, $sharedSecret, $uri);

$this->client = $client;
$this->messageFactory = $messageFactory;
$this->endpoint = $endpoint;
}

/**
* @param string $url
*
* @throws Exception
*
* @return string|null
* {@inheritdoc}
*/
public function getPageBody(string $url): ?string
public function getPageBody(string $url, string $method = 'GET'): ?string
{
$request = $this->messageFactory->createRequest('GET', $url);
$response = $this->client->sendRequest($request);
$request = $this->messageFactory->createRequest($method, $url);

try {
$response = $this->client->sendRequest($request);
} catch (Exception $e) {
throw new ApiException('Error fetching page body', $e->getCode(), $e->getMessage());
}

if ($response->getStatusCode() >= 400) {
return null;
Expand All @@ -68,11 +71,11 @@ public function getPageBody(string $url): ?string
/**
* {@inheritdoc}
*/
protected function call(array $params, string $requestMethod = 'GET'): array
public function call(string $method, array $params = [], string $requestMethod = 'GET'): array
{
$params = array_merge($params, ['format' => 'json']);
$data = $this->buildParameter($params);
$request = $this->messageFactory->createRequest($requestMethod, $this->uri, [], $data);
$request = $this->messageFactory->createRequest($requestMethod, $this->endpoint, [], $data);

try {
$response = $this->client->sendRequest($request);
Expand Down

0 comments on commit 909c289

Please sign in to comment.