mirror of
https://github.com/guzzle/guzzle.git
synced 2025-02-25 02:22:57 +01:00
Hooking up additionalParameters and additionalProperties
This commit is contained in:
parent
f484fb2235
commit
0db7702362
@ -2,11 +2,26 @@
|
||||
|
||||
namespace GuzzleHttp\Service\Guzzle\RequestLocation;
|
||||
|
||||
use GuzzleHttp\Service\Guzzle\Description\Operation;
|
||||
use GuzzleHttp\Service\Guzzle\Description\Parameter;
|
||||
use GuzzleHttp\Message\RequestInterface;
|
||||
use GuzzleHttp\Service\Guzzle\GuzzleCommandInterface;
|
||||
|
||||
abstract class AbstractLocation implements RequestLocationInterface
|
||||
{
|
||||
/** @var string */
|
||||
protected $locationName;
|
||||
|
||||
/**
|
||||
* Set the name of the location
|
||||
*
|
||||
* @param $locationName
|
||||
*/
|
||||
public function __construct($locationName)
|
||||
{
|
||||
$this->locationName = $locationName;
|
||||
}
|
||||
|
||||
public function visit(
|
||||
RequestInterface $request,
|
||||
Parameter $param,
|
||||
@ -15,7 +30,9 @@ abstract class AbstractLocation implements RequestLocationInterface
|
||||
) {}
|
||||
|
||||
public function after(
|
||||
GuzzleCommandInterface $command,
|
||||
RequestInterface $request,
|
||||
Operation $operation,
|
||||
array $context
|
||||
) {}
|
||||
|
||||
|
@ -4,6 +4,8 @@ namespace GuzzleHttp\Service\Guzzle\RequestLocation;
|
||||
|
||||
use GuzzleHttp\Service\Guzzle\Description\Parameter;
|
||||
use GuzzleHttp\Message\RequestInterface;
|
||||
use GuzzleHttp\Service\Guzzle\Description\Operation;
|
||||
use GuzzleHttp\Service\Guzzle\GuzzleCommandInterface;
|
||||
|
||||
/**
|
||||
* Request header location
|
||||
@ -18,4 +20,20 @@ class HeaderLocation extends AbstractLocation
|
||||
) {
|
||||
$request->setHeader($param->getWireName(), $param->filter($value));
|
||||
}
|
||||
|
||||
public function after(
|
||||
GuzzleCommandInterface $command,
|
||||
RequestInterface $request,
|
||||
Operation $operation,
|
||||
array $context
|
||||
) {
|
||||
$additional = $operation->getAdditionalParameters();
|
||||
if ($additional && $additional->getLocation() == $this->locationName) {
|
||||
foreach ($command->toArray() as $key => $value) {
|
||||
if (!$operation->hasParam($key)) {
|
||||
$request->setHeader($key, $additional->filter($value));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,9 @@
|
||||
|
||||
namespace GuzzleHttp\Service\Guzzle\RequestLocation;
|
||||
|
||||
use GuzzleHttp\Service\Guzzle\Description\Operation;
|
||||
use GuzzleHttp\Service\Guzzle\Description\Parameter;
|
||||
use GuzzleHttp\Service\Guzzle\GuzzleCommandInterface;
|
||||
use GuzzleHttp\Message\RequestInterface;
|
||||
use GuzzleHttp\Stream\Stream;
|
||||
|
||||
@ -12,10 +14,10 @@ use GuzzleHttp\Stream\Stream;
|
||||
class JsonLocation extends AbstractLocation
|
||||
{
|
||||
/** @var bool Whether or not to add a Content-Type header when JSON is found */
|
||||
protected $jsonContentType;
|
||||
private $jsonContentType;
|
||||
|
||||
/** @var \SplObjectStorage Data object for persisting JSON data */
|
||||
protected $data;
|
||||
/** @var array */
|
||||
private $jsonData;
|
||||
|
||||
/**
|
||||
* @param string $contentType Content-Type header to add to the request if
|
||||
@ -24,7 +26,6 @@ class JsonLocation extends AbstractLocation
|
||||
public function __construct($contentType = 'application/json')
|
||||
{
|
||||
$this->jsonContentType = $contentType;
|
||||
$this->data = new \SplObjectStorage();
|
||||
}
|
||||
|
||||
public function visit(
|
||||
@ -33,19 +34,30 @@ class JsonLocation extends AbstractLocation
|
||||
$value,
|
||||
array $context
|
||||
) {
|
||||
$json = isset($this->data[$context['command']])
|
||||
? $this->data[$context['command']]
|
||||
: [];
|
||||
$json[$param->getWireName()] = $this->prepareValue($value, $param);
|
||||
$this->data[$context['command']] = $json;
|
||||
if (null === $this->jsonData) {
|
||||
$this->jsonData = [];
|
||||
}
|
||||
|
||||
$this->jsonData[$param->getWireName()] = $this->prepareValue($value, $param);
|
||||
}
|
||||
|
||||
public function after(
|
||||
GuzzleCommandInterface $command,
|
||||
RequestInterface $request,
|
||||
Operation $operation,
|
||||
array $context
|
||||
) {
|
||||
if (!isset($this->data[$context['command']])) {
|
||||
return;
|
||||
$data = $this->jsonData;
|
||||
$this->jsonData = null;
|
||||
|
||||
// Add additional parameters to the JSON document
|
||||
$additional = $operation->getAdditionalParameters();
|
||||
if ($additional && $additional->getLocation() == $this->locationName) {
|
||||
foreach ($command->toArray() as $key => $value) {
|
||||
if (!$operation->hasParam($key)) {
|
||||
$data[$key] = $this->prepareValue($value, $additional);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Don't overwrite the Content-Type if one is set
|
||||
@ -53,7 +65,6 @@ class JsonLocation extends AbstractLocation
|
||||
$request->setHeader('Content-Type', $this->jsonContentType);
|
||||
}
|
||||
|
||||
$request->setBody(Stream::factory(json_encode($this->data[$context['command']])));
|
||||
unset($this->data[$context['command']]);
|
||||
$request->setBody(Stream::factory(json_encode($data)));
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,8 @@ namespace GuzzleHttp\Service\Guzzle\RequestLocation;
|
||||
use GuzzleHttp\Service\Guzzle\Description\Parameter;
|
||||
use GuzzleHttp\Message\RequestInterface;
|
||||
use GuzzleHttp\Post\PostBodyInterface;
|
||||
use GuzzleHttp\Service\Guzzle\GuzzleCommandInterface;
|
||||
use GuzzleHttp\Service\Guzzle\Description\Operation;
|
||||
|
||||
/**
|
||||
* Adds POST fields to a request
|
||||
@ -24,7 +26,32 @@ class PostFieldLocation extends AbstractLocation
|
||||
|
||||
$body->setField(
|
||||
$param->getWireName(),
|
||||
$this->prepValue($value, $param)
|
||||
$this->prepareValue($value, $param)
|
||||
);
|
||||
}
|
||||
|
||||
public function after(
|
||||
GuzzleCommandInterface $command,
|
||||
RequestInterface $request,
|
||||
Operation $operation,
|
||||
array $context
|
||||
) {
|
||||
$additional = $operation->getAdditionalParameters();
|
||||
if ($additional && $additional->getLocation() == $this->locationName) {
|
||||
|
||||
$body = $request->getBody();
|
||||
if (!($body instanceof PostBodyInterface)) {
|
||||
throw new \RuntimeException('Must be a POST body interface');
|
||||
}
|
||||
|
||||
foreach ($command->toArray() as $key => $value) {
|
||||
if (!$operation->hasParam($key)) {
|
||||
$body->setField(
|
||||
$key,
|
||||
$this->prepareValue($value, $additional)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,8 @@ namespace GuzzleHttp\Service\Guzzle\RequestLocation;
|
||||
|
||||
use GuzzleHttp\Service\Guzzle\Description\Parameter;
|
||||
use GuzzleHttp\Message\RequestInterface;
|
||||
use GuzzleHttp\Service\Guzzle\Description\Operation;
|
||||
use GuzzleHttp\Service\Guzzle\GuzzleCommandInterface;
|
||||
|
||||
/**
|
||||
* Adds query string values to requests
|
||||
@ -18,7 +20,26 @@ class QueryLocation extends AbstractLocation
|
||||
) {
|
||||
$request->setHeader(
|
||||
$param->getWireName(),
|
||||
$this->prepValue($value, $param)
|
||||
$this->prepareValue($value, $param)
|
||||
);
|
||||
}
|
||||
|
||||
public function after(
|
||||
GuzzleCommandInterface $command,
|
||||
RequestInterface $request,
|
||||
Operation $operation,
|
||||
array $context
|
||||
) {
|
||||
$additional = $operation->getAdditionalParameters();
|
||||
if ($additional && $additional->getLocation() == $this->locationName) {
|
||||
foreach ($command->toArray() as $key => $value) {
|
||||
if (!$operation->hasParam($key)) {
|
||||
$request->getQuery()[$key] = $this->prepareValue(
|
||||
$value,
|
||||
$additional
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,9 @@
|
||||
|
||||
namespace GuzzleHttp\Service\Guzzle\RequestLocation;
|
||||
|
||||
use GuzzleHttp\Service\Guzzle\Description\Operation;
|
||||
use GuzzleHttp\Service\Guzzle\Description\Parameter;
|
||||
use GuzzleHttp\Service\Guzzle\GuzzleCommandInterface;
|
||||
use GuzzleHttp\Message\RequestInterface;
|
||||
|
||||
/**
|
||||
@ -29,12 +31,16 @@ interface RequestLocationInterface
|
||||
/**
|
||||
* Called when all of the parameters of a command have been visited.
|
||||
*
|
||||
* @param RequestInterface $request Request being modified
|
||||
* @param array $context Associative array containing a client
|
||||
* and command key.
|
||||
* @param GuzzleCommandInterface $command Command being prepared
|
||||
* @param RequestInterface $request Request being modified
|
||||
* @param array $context Associative array containing a
|
||||
* client and command key.
|
||||
* @param Operation $operation Operation being serialized
|
||||
*/
|
||||
public function after(
|
||||
GuzzleCommandInterface $command,
|
||||
RequestInterface $request,
|
||||
Operation $operation,
|
||||
array $context
|
||||
);
|
||||
}
|
||||
|
@ -13,9 +13,6 @@ use GuzzleHttp\Stream\Stream;
|
||||
*/
|
||||
class XmlLocation extends AbstractLocation
|
||||
{
|
||||
/** @var \SplObjectStorage Data object for persisting XML data */
|
||||
protected $data;
|
||||
|
||||
/** @var \XMLWriter XML writer resource */
|
||||
protected $writer;
|
||||
|
||||
@ -39,34 +36,38 @@ class XmlLocation extends AbstractLocation
|
||||
$value,
|
||||
array $context
|
||||
) {
|
||||
/* @var GuzzleCommandInterface $command */
|
||||
$command = $context['command'];
|
||||
$xml = isset($this->data[$command])
|
||||
? $this->data[$command]
|
||||
: $this->createRootElement($command->getOperation());
|
||||
$this->addXml($xml, $param, $value);
|
||||
$this->data[$command] = $xml;
|
||||
if (!$this->writer) {
|
||||
/* @var GuzzleCommandInterface $command */
|
||||
$command = $context['command'];
|
||||
$this->createRootElement($command->getOperation());
|
||||
}
|
||||
|
||||
$this->addXml($this->writer, $param, $value);
|
||||
}
|
||||
|
||||
public function after(
|
||||
GuzzleCommandInterface $command,
|
||||
RequestInterface $request,
|
||||
Operation $operation,
|
||||
array $context
|
||||
) {
|
||||
$xml = null;
|
||||
/* @var GuzzleCommandInterface $command */
|
||||
$command = $context['command'];
|
||||
$additional = $operation->getAdditionalParameters();
|
||||
if ($additional && $additional->getLocation() == $this->locationName) {
|
||||
foreach ($command->toArray() as $key => $value) {
|
||||
if (!$operation->hasParam($key)) {
|
||||
$this->visit($request, $additional, $value, ['command' => $command]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If data was found that needs to be serialized, then do so
|
||||
if (isset($this->data[$command])) {
|
||||
$xml = null;
|
||||
if ($this->writer) {
|
||||
$xml = $this->finishDocument($this->writer);
|
||||
unset($this->data[$command]);
|
||||
} else {
|
||||
} elseif ($operation->getData('xmlAllowEmpty')) {
|
||||
// Check if XML should always be sent for the command
|
||||
$operation = $command->getOperation();
|
||||
if ($operation->getData('xmlAllowEmpty')) {
|
||||
$writer = $this->createRootElement($operation);
|
||||
$xml = $this->finishDocument($writer);
|
||||
}
|
||||
$writer = $this->createRootElement($operation);
|
||||
$xml = $this->finishDocument($writer);
|
||||
}
|
||||
|
||||
if ($xml) {
|
||||
@ -76,6 +77,8 @@ class XmlLocation extends AbstractLocation
|
||||
$request->setHeader('Content-Type', $this->contentType);
|
||||
}
|
||||
}
|
||||
|
||||
$this->writer = null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -8,6 +8,19 @@ use GuzzleHttp\Service\Guzzle\GuzzleCommandInterface;
|
||||
|
||||
abstract class AbstractLocation implements ResponseLocationInterface
|
||||
{
|
||||
/** @var string */
|
||||
protected $locationName;
|
||||
|
||||
/**
|
||||
* Set the name of the location
|
||||
*
|
||||
* @param $locationName
|
||||
*/
|
||||
public function __construct($locationName)
|
||||
{
|
||||
$this->locationName = $locationName;
|
||||
}
|
||||
|
||||
public function before(
|
||||
GuzzleCommandInterface $command,
|
||||
ResponseInterface $response,
|
||||
|
@ -32,15 +32,14 @@ class HeaderLocation extends AbstractLocation
|
||||
&$result,
|
||||
array $context = []
|
||||
) {
|
||||
// Handle additionalProperties
|
||||
$additional = $model->getAdditionalProperties();
|
||||
if (!($additional instanceof Parameter)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($response->getHeaders() as $key => $header) {
|
||||
if (!isset($result[$key])) {
|
||||
$result[$key] = $additional->filter(implode($header, ', '));
|
||||
if ($additional instanceof Parameter &&
|
||||
$additional->getLocation() == $this->locationName
|
||||
) {
|
||||
foreach ($response->getHeaders() as $key => $header) {
|
||||
if (!isset($result[$key])) {
|
||||
$result[$key] = $additional->filter(implode($header, ', '));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ class JsonLocation extends AbstractLocation
|
||||
// Handle additional, undefined properties
|
||||
$additional = $model->getAdditionalProperties();
|
||||
if ($additional instanceof Parameter &&
|
||||
$additional->getLocation() == 'json'
|
||||
$additional->getLocation() == $this->locationName
|
||||
) {
|
||||
foreach ($this->json as $prop => $val) {
|
||||
if (!isset($result[$prop])) {
|
||||
|
@ -34,7 +34,7 @@ class XmlLocation extends AbstractLocation
|
||||
// Handle additional, undefined properties
|
||||
$additional = $model->getAdditionalProperties();
|
||||
if ($additional instanceof Parameter &&
|
||||
$additional->getLocation() == 'xml'
|
||||
$additional->getLocation() == $this->locationName
|
||||
) {
|
||||
if ($additional->getType()) {
|
||||
$this->recursiveProcess($additional, $this->xml);
|
||||
|
@ -39,13 +39,13 @@ class PrepareRequest implements SubscriberInterface
|
||||
static $defaultRequestLocations;
|
||||
if (!$defaultRequestLocations) {
|
||||
$defaultRequestLocations = [
|
||||
'body' => new BodyLocation(),
|
||||
'query' => new QueryLocation(),
|
||||
'header' => new HeaderLocation(),
|
||||
'json' => new JsonLocation(),
|
||||
'xml' => new XmlLocation(),
|
||||
'postField' => new PostFieldLocation(),
|
||||
'postFile' => new PostFileLocation()
|
||||
'body' => new BodyLocation('body'),
|
||||
'query' => new QueryLocation('query'),
|
||||
'header' => new HeaderLocation('header'),
|
||||
'json' => new JsonLocation('json'),
|
||||
'xml' => new XmlLocation('xml'),
|
||||
'postField' => new PostFieldLocation('postField'),
|
||||
'postFile' => new PostFileLocation('postFile')
|
||||
];
|
||||
}
|
||||
|
||||
@ -78,7 +78,10 @@ class PrepareRequest implements SubscriberInterface
|
||||
) {
|
||||
$visitedLocations = [];
|
||||
$context = ['client' => $client, 'command' => $command];
|
||||
foreach ($command->getOperation()->getParams() as $name => $param) {
|
||||
$operation = $command->getOperation();
|
||||
|
||||
// Visit each actual parameter
|
||||
foreach ($operation->getParams() as $name => $param) {
|
||||
/* @var Parameter $param */
|
||||
$location = $param->getLocation();
|
||||
// Skip parameters that have not been set or are URI location
|
||||
@ -97,8 +100,19 @@ class PrepareRequest implements SubscriberInterface
|
||||
);
|
||||
}
|
||||
|
||||
// Ensure that the after() method is invoked for additionalParameters
|
||||
if ($additional = $operation->getAdditionalParameters()) {
|
||||
$visitedLocations[$additional->getLocation()] = true;
|
||||
}
|
||||
|
||||
// Call the after() method for each visited location
|
||||
foreach (array_keys($visitedLocations) as $location) {
|
||||
$this->requestLocations[$location]->after($request, $context);
|
||||
$this->requestLocations[$location]->after(
|
||||
$command,
|
||||
$request,
|
||||
$operation,
|
||||
$context
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -47,12 +47,12 @@ class ProcessResponse implements SubscriberInterface
|
||||
static $defaultResponseLocations;
|
||||
if (!$defaultResponseLocations) {
|
||||
$defaultResponseLocations = [
|
||||
'body' => new BodyLocation(),
|
||||
'header' => new HeaderLocation(),
|
||||
'reasonPhrase' => new ReasonPhraseLocation(),
|
||||
'statusCode' => new StatusCodeLocation(),
|
||||
'xml' => new XmlLocation(),
|
||||
'json' => new JsonLocation()
|
||||
'body' => new BodyLocation('body'),
|
||||
'header' => new HeaderLocation('header'),
|
||||
'reasonPhrase' => new ReasonPhraseLocation('reasonPhrase'),
|
||||
'statusCode' => new StatusCodeLocation('statusCode'),
|
||||
'xml' => new XmlLocation('xml'),
|
||||
'json' => new JsonLocation('json')
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user