Skip to content
Permalink
Browse files

Replace HTTPlug with PSR http client (#33)

  • Loading branch information
kodiakhq committed Dec 8, 2019
2 parents e09e692 + c2b73d6 commit 5bd1fadc9580baa6001eca76a8ef3a96f91aed6a
@@ -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
@@ -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],
];
```
@@ -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
@@ -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",
@@ -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
@@ -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()
@@ -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>
@@ -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,
],
];

@@ -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

0 comments on commit 5bd1fad

Please sign in to comment.
You can’t perform that action at this time.