BodyLocation.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace Qcloud\Cos\Request;
  3. use GuzzleHttp\Command\Guzzle\RequestLocation\AbstractLocation;
  4. use GuzzleHttp\Command\CommandInterface;
  5. use GuzzleHttp\Command\Guzzle\Parameter;
  6. use GuzzleHttp\Psr7;
  7. use Psr\Http\Message\MessageInterface;
  8. use Psr\Http\Message\RequestInterface;
  9. /**
  10. * Adds a raw/binary body to a request.
  11. * This is here because: https://github.com/guzzle/guzzle-services/issues/160
  12. */
  13. class BodyLocation extends AbstractLocation
  14. {
  15. /**
  16. * Set the name of the location
  17. *
  18. * @param string $locationName
  19. */
  20. public function __construct($locationName = 'body')
  21. {
  22. parent::__construct($locationName);
  23. }
  24. /**
  25. * @param CommandInterface $command
  26. * @param RequestInterface $request
  27. * @param Parameter $param
  28. *
  29. * @return MessageInterface
  30. */
  31. public function visit(
  32. CommandInterface $command,
  33. RequestInterface $request,
  34. Parameter $param
  35. ) {
  36. $value = $request->getBody()->getContents();
  37. if ('' !== $value) {
  38. throw new \RuntimeException('Only one "body" location may exist per operation');
  39. }
  40. // binary string data from bound parameter
  41. $value = $command[$param->getName()];
  42. return $request->withBody(Psr7\stream_for($value));
  43. }
  44. }