1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-25 02:22:57 +01:00

Adding support for default header and query string values

This commit is contained in:
Michael Dowling 2013-08-01 20:42:15 -07:00
parent 09f55fabc0
commit a461bf19a9
3 changed files with 25 additions and 5 deletions

View File

@ -123,7 +123,7 @@ class Client implements ClientInterface
$options = array_replace_recursive($default, $options);
}
$request = $this->messageFactory->createRequest($method, (string) $url, $body, $options);
$request = $this->messageFactory->createRequest($method, (string) $url, $headers, $body, $options);
$request->setEventDispatcher(clone $this->getEventDispatcher());
if ($this->userAgent && !$request->hasHeader('User-Agent')) {
$request->setHeader('User-Agent', $this->userAgent);

View File

@ -34,9 +34,8 @@ class MessageFactory implements MessageFactoryInterface
return new Response();
}
public function createRequest($method, $url, $body = null, array $options = array())
public function createRequest($method, $url, array $headers = [], $body = null, array $options = array())
{
$headers = isset($options['headers']) ? $options['headers'] : array();
$request = new Request($method, $url, $headers);
if ($body) {
@ -138,7 +137,27 @@ class MessageFactory implements MessageFactoryInterface
throw new \InvalidArgumentException('query value must be an array');
}
$request->getQuery()->overwriteWith($value);
// Do not overwrite existing query string variables
$query = $request->getQuery();
foreach ($value as $k => $v) {
if (!isset($query[$k])) {
$query[$k] = $v;
}
}
}
private function visit_headers(RequestInterface $request, $value)
{
if (!is_array($value)) {
throw new \InvalidArgumentException('header value must be an array');
}
// Do not overwrite existing headers
foreach ($value as $k => $v) {
if (!$request->hasHeader($k)) {
$request->setHeader($k, $v);
}
}
}
private function visit_cookies(RequestInterface $request, $value)

View File

@ -15,6 +15,7 @@ interface MessageFactoryInterface
*
* @param string $method HTTP method (GET, POST, PUT, PATCH, HEAD, DELETE, ...)
* @param string|Url $url HTTP URL to connect to
* @param array $headers HTTP request headers
* @param string|resource|array|StreamInterface $body Body to send in the request
* @param array $options Array of options to apply to the request
* "headers": Associative array of headers
@ -44,7 +45,7 @@ interface MessageFactoryInterface
*
* @return RequestInterface
*/
public function createRequest($method, $url, $body = null, array $options = array());
public function createRequest($method, $url, array $headers = [], $body = null, array $options = array());
/**
* Create a response object