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 3f3da23 commit be457e6
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 56 deletions.
46 changes: 5 additions & 41 deletions README.md
Expand Up @@ -21,7 +21,8 @@ Open a command console, enter your project directory and execute the following c

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

### Enable the Bundle
Expand All @@ -33,7 +34,6 @@ Then, enable the bundle by adding it to the list of registered bundles in `confi

return [
// ...
Http\HttplugBundle\HttplugBundle::class => ['all' => true],
Core23\LastFmBundle\Core23LastFmBundle::class => ['all' => true],
];
```
Expand All @@ -49,50 +49,14 @@ core23_lastfm:
api:
app_id: "%lastfm_api.id%"
shared_secret: "%lastfm_api.secret%"
```

## Usage

Define a [HTTPlug] client in your configuration.

```yaml
# config/packages/httplug.yaml

httplug:
classes:
client: Http\Adapter\Guzzle6\Client
message_factory: Http\Message\MessageFactory\GuzzleMessageFactory
uri_factory: Http\Message\UriFactory\GuzzleUriFactory
stream_factory: Http\Message\StreamFactory\GuzzleStreamFactory
clients:
default:
# ...
plugins:
- httplug.plugin.redirect # plugin is needed for the webcrawler
```

### API cache

It is recommended to use a cache to reduce the API usage.

```yaml
# config/packages/httplug.yaml

httplug:
plugins:
cache:
cache_pool: 'acme.httplug_cache'
config:
default_ttl: 7200 # Two hours
clients:
default:
plugins:
- httplug.plugin.cache
http:
client: 'httplug.client'
message_factory: 'nyholm.psr7.psr17_factory'
```

## License

This bundle is under the [MIT license](LICENSE.md).

[HTTPlug]: http://docs.php-http.org/en/latest/index.html
[Last.fm API]: http://www.last.fm/api
13 changes: 6 additions & 7 deletions composer.json
Expand Up @@ -21,8 +21,8 @@
"require": {
"php": "^7.2",
"core23/lastfm-api": "^2.0 || dev-master",
"php-http/client-implementation": "*",
"php-http/httplug-bundle": "^1.15",
"psr/http-client": "^1.0",
"psr/http-message": "^1.0",
"symfony/config": "^3.4 || ^4.2",
"symfony/dependency-injection": "^3.4 || ^4.2",
"symfony/event-dispatcher-contracts": "^1.1",
Expand All @@ -36,14 +36,13 @@
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.2",
"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": {
"cache/adapter-bundle": "Symfony cache adapter",
"php-http/buzz-adapter": "Buzz HTTP client implementation",
"php-http/guzzle6-adapter": "Guzzle HTTP client implementation"
"symfony/http-client": "Symfony HTTP client implementation"
},
"config": {
"sort-packages": true
Expand Down
4 changes: 2 additions & 2 deletions src/DependencyInjection/Configuration.php
Expand Up @@ -59,8 +59,8 @@ private function addHttpClientSection(ArrayNodeDefinition $node): void
->arrayNode('http')
->addDefaultsIfNotSet()
->children()
->scalarNode('client')->defaultValue('httplug.client.default')->end()
->scalarNode('message_factory')->defaultValue('httplug.message_factory.default')->end()
->scalarNode('client')->defaultNull()->end()
->scalarNode('message_factory')->defaultNull()->end()
->end()
->end()
->end()
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/config/services.xml
Expand Up @@ -4,12 +4,12 @@
<service id="Core23\LastFmBundle\Session\SessionManagerInterface" class="Core23\LastFmBundle\Session\SessionManager">
<argument type="service" id="session"/>
</service>
<service id="Core23\LastFm\Connection\HTTPlugConnection">
<service id="Core23\LastFm\Connection\PsrClientConnection">
<argument type="service" id="core23_lastfm.http.client"/>
<argument type="service" id="core23_lastfm.http.message_factory"/>
<argument>%core23_lastfm.api.endpoint%</argument>
</service>
<service id="Core23\LastFm\Connection\ConnectionInterface" alias="Core23\LastFm\Connection\HTTPlugConnection"/>
<service id="Core23\LastFm\Connection\ConnectionInterface" alias="Core23\LastFm\Connection\PsrClientConnection"/>
<service id="Core23\LastFm\Client\ApiClientInterface" class="Core23\LastFm\Client\ApiClient">
<argument type="service" id="Core23\LastFm\Connection\ConnectionInterface"/>
<argument>%core23_lastfm.api.app_id%</argument>
Expand Down
4 changes: 2 additions & 2 deletions tests/DependencyInjection/ConfigurationTest.php
Expand Up @@ -36,8 +36,8 @@ public function testOptions(): void
'auth_url' => 'http://www.last.fm/api/auth/',
],
'http' => [
'client' => 'httplug.client.default',
'message_factory' => 'httplug.message_factory.default',
'client' => null,
'message_factory' => null,
],
];

Expand Down
8 changes: 6 additions & 2 deletions tests/DependencyInjection/Core23LastFmExtensionTest.php
Expand Up @@ -23,15 +23,19 @@ public function testLoadDefault(): void
'app_id' => 'foo_id',
'shared_secret' => 'bar_secret',
],
'http' => [
'client' => 'acme.client',
'message_factory' => 'acme.message_factory',
],
]);

$this->assertContainerBuilderHasParameter('core23_lastfm.api.app_id', 'foo_id');
$this->assertContainerBuilderHasParameter('core23_lastfm.api.shared_secret', 'bar_secret');
$this->assertContainerBuilderHasParameter('core23_lastfm.api.endpoint', 'http://ws.audioscrobbler.com/2.0/');
$this->assertContainerBuilderHasParameter('core23_lastfm.api.auth_url', 'http://www.last.fm/api/auth/');

$this->assertContainerBuilderHasAlias('core23_lastfm.http.client', 'httplug.client.default');
$this->assertContainerBuilderHasAlias('core23_lastfm.http.message_factory', 'httplug.message_factory.default');
$this->assertContainerBuilderHasAlias('core23_lastfm.http.client', 'acme.client');
$this->assertContainerBuilderHasAlias('core23_lastfm.http.message_factory', 'acme.message_factory');
}

protected function getContainerExtensions(): array
Expand Down

0 comments on commit be457e6

Please sign in to comment.