Skip to content

Commit

Permalink
Allow to not set base_uri in clients configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Stéphane Reuille committed Jun 26, 2015
1 parent c94fc8a commit 9577b4f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ Add the `m6web_guzzlehttp` section in your configuration file. Here is the minim
# app/config/config.yml
m6web_guzzlehttp:
clients:
default:
default: ~
other:
base_uri: "http://domain.tld/"
```
Expand All @@ -57,19 +58,24 @@ Then you can ask container for your client :
```php
// in a controller
$client = $this->get('m6web_guzzlehttp');
$client = $this->get('m6web_guzzlehttp'); // default client
try {
$response = $client->get('path/to/resource'); // call http://domain.tld/path/to/resource
$response = $client->get('http://domain.tld/path/to/resource');
$promises = [
'first' => $client->getAsync('path/to/resource'),
'second' => $client->getAsync('http://other.domain.tld/path/to/resource')
'first' => $client->getAsync('http://domain.tld/path/to/resource'),
'second' => $client->getAsync('http://domain.tld/path/to/other/resource')
];
$result = \GuzzleHttp\Promise\unwrap($promises);
} catch(\GuzzleHttp\Exception\ConnectException $e) {
// connection problem like timeout
}
}
// use other client
$otherClient = $this->get('m6web_guzzlehttp_other');
$response = $otherClient->get('path/to/resource'); // call http://domain.tld/path/to/resource
```

The service return a configured guzzle client, for more information on how to use it, you can read the [guzzle6 documentation](http://guzzle.readthedocs.org/en/latest/index.html).
Expand Down Expand Up @@ -154,7 +160,7 @@ For more information on how to setup the RedisBundle, refer to the README in the
m6web_guzzlehttp:
clients:
default:
base_uri: "" # required. Base uri
base_uri: "" # Base uri to prepend on request uri
timeout: 5.0 # request timeout
http_errors: true # enable or disable exception on http errors
allow_redirects: true # enable or disable follow redirect
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function getConfigTreeBuilder()
->useAttributeAsKey('id', false)
->prototype('array')
->children()
->scalarNode('base_uri')->isRequired()->end()
->scalarNode('base_uri')->defaultValue("")->end()
->floatNode('timeout')->defaultValue(5.0)->end()
->booleanNode('http_errors')->defaultValue(true)->end()
->booleanNode('allow_redirects')->defaultValue(true)->end()
Expand Down
3 changes: 1 addition & 2 deletions tests/Fixtures/default-config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
m6web_guzzlehttp:
clients:
default:
base_uri: "http://domain.tld"
default: ~
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testDefaultConfig()
->array($arguments = $container->getDefinition('m6web_guzzlehttp')->getArgument(0))
->hasSize(8)
->string($arguments['base_uri'])
->isEqualTo('http://domain.tld')
->isEqualTo('')
->float($arguments['timeout'])
->isEqualTo(5.0)
->boolean($arguments['http_errors'])
Expand Down

0 comments on commit 9577b4f

Please sign in to comment.