mirror of
https://github.com/guzzle/guzzle.git
synced 2025-02-12 03:24:26 +01:00
Renaming getDefaultOption to getConfig. No longer removing the handler from the config. Allowing the handler to be overridden in request options
This commit is contained in:
parent
27823e9f41
commit
1cb0535933
@ -79,6 +79,9 @@ middleware functions that wrap handlers (or are injected into a
|
||||
- Static functions in `GuzzleHttp\Utils` have been moved to namespaced
|
||||
functions under the `GuzzleHttp` namespace. This requires either a Composer
|
||||
based autoloader or you to include functions.php.
|
||||
- `GuzzleHttp\ClientInterface::getDefaultOption` has been renamed to
|
||||
`GuzzleHttp\ClientInterface::getConfig`.
|
||||
- `GuzzleHttp\ClientInterface::setDefaultOption` has been removed.
|
||||
|
||||
4.x to 5.0
|
||||
----------
|
||||
|
@ -25,11 +25,8 @@ use \InvalidArgumentException as Iae;
|
||||
*/
|
||||
class Client implements ClientInterface
|
||||
{
|
||||
/** @var callable */
|
||||
private $handler;
|
||||
|
||||
/** @var array Default request options */
|
||||
private $defaults;
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* Clients accept an array of constructor parameters.
|
||||
@ -65,11 +62,8 @@ class Client implements ClientInterface
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
if (isset($config['handler'])) {
|
||||
$this->handler = $config['handler'];
|
||||
unset($config['handler']);
|
||||
} else {
|
||||
$this->handler = HandlerStack::create();
|
||||
if (!isset($config['handler'])) {
|
||||
$config['handler'] = HandlerStack::create();
|
||||
}
|
||||
|
||||
// Convert the base_uri to a UriInterface
|
||||
@ -133,13 +127,11 @@ class Client implements ClientInterface
|
||||
return $this->requestAsync($method, $uri, $options)->wait();
|
||||
}
|
||||
|
||||
public function getDefaultOption($option = null)
|
||||
public function getConfig($option = null)
|
||||
{
|
||||
return $option === null
|
||||
? $this->defaults
|
||||
: (isset($this->defaults[$option])
|
||||
? $this->defaults[$option]
|
||||
: null);
|
||||
? $this->config
|
||||
: (isset($this->config[$option]) ? $this->config[$option] : null);
|
||||
}
|
||||
|
||||
private function buildUri($uri, array $config)
|
||||
@ -177,23 +169,23 @@ class Client implements ClientInterface
|
||||
$defaults['proxy']['https'] = $proxy;
|
||||
}
|
||||
|
||||
$this->defaults = $config + $defaults;
|
||||
$this->config = $config + $defaults;
|
||||
|
||||
if (!empty($config['cookies']) && $config['cookies'] === true) {
|
||||
$this->defaults['cookies'] = new CookieJar();
|
||||
$this->config['cookies'] = new CookieJar();
|
||||
}
|
||||
|
||||
// Add the default user-agent header.
|
||||
if (!isset($this->defaults['headers'])) {
|
||||
$this->defaults['headers'] = ['User-Agent' => default_user_agent()];
|
||||
if (!isset($this->config['headers'])) {
|
||||
$this->config['headers'] = ['User-Agent' => default_user_agent()];
|
||||
} else {
|
||||
// Add the User-Agent header if one was not already set.
|
||||
foreach (array_keys($this->defaults['headers']) as $name) {
|
||||
foreach (array_keys($this->config['headers']) as $name) {
|
||||
if (strtolower($name) === 'user-agent') {
|
||||
return;
|
||||
}
|
||||
}
|
||||
$this->defaults['headers']['User-Agent'] = default_user_agent();
|
||||
$this->config['headers']['User-Agent'] = default_user_agent();
|
||||
}
|
||||
}
|
||||
|
||||
@ -206,7 +198,7 @@ class Client implements ClientInterface
|
||||
*/
|
||||
private function prepareDefaults($options)
|
||||
{
|
||||
$defaults = $this->defaults;
|
||||
$defaults = $this->config;
|
||||
|
||||
if (!empty($defaults['headers'])) {
|
||||
// Default headers are only added if they are not present.
|
||||
@ -265,7 +257,7 @@ class Client implements ClientInterface
|
||||
}
|
||||
|
||||
$request = $this->applyOptions($request, $options);
|
||||
$handler = $this->handler;
|
||||
$handler = $options['handler'];
|
||||
|
||||
try {
|
||||
return Promise\promise_for($handler($request, $options));
|
||||
|
@ -68,11 +68,15 @@ interface ClientInterface
|
||||
public function requestAsync($method, $uri = null, array $options = []);
|
||||
|
||||
/**
|
||||
* Get default request options of the client.
|
||||
* Get a client configuration option.
|
||||
*
|
||||
* @param string|null $option The default request option to retrieve.
|
||||
* These options include default request options of the client, a "handler"
|
||||
* (if utilized by the concrete client), and a "base_uri" if utilized by
|
||||
* the concrete client.
|
||||
*
|
||||
* @param string|null $option The config option to retrieve.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getDefaultOption($option = null);
|
||||
public function getConfig($option = null);
|
||||
}
|
||||
|
@ -59,13 +59,13 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
'headers' => ['bar' => 'baz'],
|
||||
'handler' => new MockHandler()
|
||||
]);
|
||||
$base = $client->getDefaultOption('base_uri');
|
||||
$base = $client->getConfig('base_uri');
|
||||
$this->assertEquals('http://foo.com', (string) $base);
|
||||
$this->assertInstanceOf('GuzzleHttp\Psr7\Uri', $base);
|
||||
$this->assertNull($client->getDefaultOption('handler'));
|
||||
$this->assertEquals(2, $client->getDefaultOption('timeout'));
|
||||
$this->assertArrayHasKey('timeout', $client->getDefaultOption());
|
||||
$this->assertArrayHasKey('headers', $client->getDefaultOption());
|
||||
$this->assertNotNull($client->getConfig('handler'));
|
||||
$this->assertEquals(2, $client->getConfig('timeout'));
|
||||
$this->assertArrayHasKey('timeout', $client->getConfig());
|
||||
$this->assertArrayHasKey('headers', $client->getConfig());
|
||||
}
|
||||
|
||||
public function testCanMergeOnBaseUri()
|
||||
@ -89,7 +89,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
'handler' => $mock,
|
||||
'base_uri' => 'http://bar.com'
|
||||
]);
|
||||
$this->assertEquals('http://bar.com', (string) $client->getDefaultOption('base_uri'));
|
||||
$this->assertEquals('http://bar.com', (string) $client->getConfig('base_uri'));
|
||||
$request = new Request('GET', '/baz');
|
||||
$client->send($request);
|
||||
$this->assertEquals(
|
||||
@ -101,11 +101,11 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
public function testMergesDefaultOptionsAndDoesNotOverwriteUa()
|
||||
{
|
||||
$c = new Client(['headers' => ['User-agent' => 'foo']]);
|
||||
$this->assertEquals(['User-agent' => 'foo'], $c->getDefaultOption('headers'));
|
||||
$this->assertInternalType('array', $c->getDefaultOption('allow_redirects'));
|
||||
$this->assertTrue($c->getDefaultOption('http_errors'));
|
||||
$this->assertTrue($c->getDefaultOption('decode_content'));
|
||||
$this->assertTrue($c->getDefaultOption('verify'));
|
||||
$this->assertEquals(['User-agent' => 'foo'], $c->getConfig('headers'));
|
||||
$this->assertInternalType('array', $c->getConfig('allow_redirects'));
|
||||
$this->assertTrue($c->getConfig('http_errors'));
|
||||
$this->assertTrue($c->getConfig('decode_content'));
|
||||
$this->assertTrue($c->getConfig('verify'));
|
||||
}
|
||||
|
||||
public function testDoesNotOverwriteHeaderWithDefault()
|
||||
@ -439,18 +439,18 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
$http = getenv('HTTP_PROXY');
|
||||
$https = getenv('HTTPS_PROXY');
|
||||
$client = new Client();
|
||||
$this->assertNull($client->getDefaultOption('proxy'));
|
||||
$this->assertNull($client->getConfig('proxy'));
|
||||
putenv('HTTP_PROXY=127.0.0.1');
|
||||
$client = new Client();
|
||||
$this->assertEquals(
|
||||
['http' => '127.0.0.1'],
|
||||
$client->getDefaultOption('proxy')
|
||||
$client->getConfig('proxy')
|
||||
);
|
||||
putenv('HTTPS_PROXY=127.0.0.2');
|
||||
$client = new Client();
|
||||
$this->assertEquals(
|
||||
['http' => '127.0.0.1', 'https' => '127.0.0.2'],
|
||||
$client->getDefaultOption('proxy')
|
||||
$client->getConfig('proxy')
|
||||
);
|
||||
putenv("HTTP_PROXY=$http");
|
||||
putenv("HTTPS_PROXY=$https");
|
||||
@ -472,16 +472,16 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertTrue($mock->getLastOptions()['synchronous']);
|
||||
}
|
||||
|
||||
public function testCanDisableDefaultMiddleware()
|
||||
public function testCanSetCustomHandler()
|
||||
{
|
||||
$mock = new MockHandler([new Response(500)]);
|
||||
$client = new Client([
|
||||
'handler' => $mock,
|
||||
'disable_default_middleware' => true
|
||||
]);
|
||||
$client = new Client(['handler' => $mock]);
|
||||
$mock2 = new MockHandler([new Response(200)]);
|
||||
$this->assertEquals(
|
||||
500,
|
||||
$client->send(new Request('GET', 'http://foo.com'))->getStatusCode()
|
||||
200,
|
||||
$client->send(new Request('GET', 'http://foo.com'), [
|
||||
'handler' => $mock2
|
||||
])->getStatusCode()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user