Skip to content

Commit

Permalink
Replace HTTPlug with PSR http client
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Dec 8, 2019
1 parent 7487033 commit ded762f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,21 @@ Open a command console, enter your project directory and execute the following c

```
composer require core23/lastfm-api
composer require php-http/guzzle6-adapter # if you want to use HTTPlug with Guzzle
# To define a default http client and message factory
composer require symfony/http-client nyholm/psr7
```

## Usage

```php
// 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);
$connection = new \Core23\LastFm\Connection\PsrClientConnection($httpClient, $requestFactory);

// Auth user to get a token
// http://www.last.fm/api/auth/?api_key=API_KEY

// Create a session (with generated token)
$token = 'API token';
$authApi = new \Core23\LastFm\Service\AuthService($connection);
$session = $authApi->createSession($token);

Expand Down
14 changes: 6 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,21 @@
"ext-json": "*",
"ext-mbstring": "*",
"ext-pcre": "*",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.0",
"psr/http-message": "^1.0",
"psr/log": "^1.0",
"symfony/css-selector": "^4.2 || ^5.0",
"symfony/dom-crawler": "^4.2 || ^5.0"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.2",
"php-http/client-implementation": "*",
"php-http/httplug": "^2.0",
"php-http/message-factory": "^1.0",
"php-http/mock-client": "^1.3",
"sllh/composer-lint": "^1.0"
"nyholm/psr7": "^1.0",
"sllh/composer-lint": "^1.0",
"symfony/http-client": "^4.4 || ^5.0"
},
"suggest": {
"php-http/buzz-adapter": "Buzz HTTP client implementation",
"php-http/cache-plugin": "Cache plugin to reduce API usage",
"php-http/guzzle6-adapter": "Guzzle HTTP client implementation"
"symfony/http-client": "Symfony HTTP client implementation"
},
"config": {
"sort-packages": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@
namespace Core23\LastFm\Connection;

use Core23\LastFm\Exception\ApiException;
use Http\Client\Exception;
use Http\Client\HttpClient;
use Http\Message\MessageFactory;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

final class HTTPlugConnection implements ConnectionInterface
final class PsrClientConnection implements ConnectionInterface
{
/**
* @var HttpClient
* @var ClientInterface
*/
private $client;

/**
* @var MessageFactory
* @var RequestFactoryInterface
*/
private $messageFactory;
private $requestFactory;

/**
* @var string
Expand All @@ -38,10 +38,10 @@ final class HTTPlugConnection implements ConnectionInterface
/**
* Initialize client.
*/
public function __construct(HttpClient $client, MessageFactory $messageFactory, string $endpoint = ConnectionInterface::DEFAULT_ENDPOINT)
public function __construct(ClientInterface $client, RequestFactoryInterface $messageFactory, string $endpoint = ConnectionInterface::DEFAULT_ENDPOINT)
{
$this->client = $client;
$this->messageFactory = $messageFactory;
$this->requestFactory = $messageFactory;
$this->endpoint = $endpoint;
}

Expand All @@ -51,10 +51,10 @@ public function getPageBody(string $url, array $params = [], string $method = 'G

try {
$response = $this->client->sendRequest($request);
} catch (Exception $e) {
} catch (ClientExceptionInterface $e) {
throw new ApiException(
sprintf('Error fetching page body for url: %s', (string) $request->getUri()),
$e->getCode(),
500,
$e
);
}
Expand All @@ -80,8 +80,8 @@ public function call(string $url, array $params = [], string $method = 'GET'): a
throw $e;
} catch (\Exception $e) {
throw new ApiException('Technical error occurred.', 500, $e);
} catch (Exception $e) {
throw new ApiException('Technical error occurred.', $e->getCode(), $e);
} catch (ClientExceptionInterface $e) {
throw new ApiException('Technical error occurred.', 500, $e);
}
}

Expand All @@ -90,10 +90,13 @@ private function createRequest(string $method, string $url, array $params): Requ
$query = http_build_query($params);

if ('POST' === $method) {
return $this->messageFactory->createRequest($method, $url, [], $query);
$request = $this->requestFactory->createRequest($method, $url);
$request->getBody()->write($query);

return $request;
}

return $this->messageFactory->createRequest($method, $url.'?'.$query);
return $this->requestFactory->createRequest($method, $url.'?'.$query);
}

/**
Expand Down

0 comments on commit ded762f

Please sign in to comment.