Request.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace AlibabaCloud\Tea;
  3. use GuzzleHttp\Psr7\Request as PsrRequest;
  4. use InvalidArgumentException;
  5. use Psr\Http\Message\StreamInterface;
  6. /**
  7. * Class Request.
  8. */
  9. class Request extends PsrRequest
  10. {
  11. /**
  12. * @var string
  13. */
  14. public $protocol = 'https';
  15. /**
  16. * @var string
  17. */
  18. public $pathname = '/';
  19. /**
  20. * @var array
  21. */
  22. public $headers = [];
  23. /**
  24. * @var array
  25. */
  26. public $query = [];
  27. /**
  28. * @var string
  29. */
  30. public $body;
  31. /**
  32. * @var int
  33. */
  34. public $port;
  35. public $method;
  36. public function __construct($method = 'GET', $uri = '', array $headers = [], $body = null, $version = '1.1')
  37. {
  38. parent::__construct($method, $uri, $headers, $body, $version);
  39. $this->method = $method;
  40. }
  41. /**
  42. * These fields are compatible if you define other fields.
  43. * Mainly for compatibility situations where the code generator cannot generate set properties.
  44. *
  45. * @return PsrRequest
  46. */
  47. public function getPsrRequest()
  48. {
  49. $this->assertQuery($this->query);
  50. $request = clone $this;
  51. $uri = $request->getUri();
  52. if ($this->query) {
  53. $uri = $uri->withQuery(http_build_query($this->query));
  54. }
  55. if ($this->port) {
  56. $uri = $uri->withPort($this->port);
  57. }
  58. if ($this->protocol) {
  59. $uri = $uri->withScheme($this->protocol);
  60. }
  61. if ($this->pathname) {
  62. $uri = $uri->withPath($this->pathname);
  63. }
  64. if (isset($this->headers['host'])) {
  65. $uri = $uri->withHost($this->headers['host']);
  66. }
  67. $request = $request->withUri($uri);
  68. $request = $request->withMethod($this->method);
  69. if ('' !== $this->body && null !== $this->body) {
  70. if ($this->body instanceof StreamInterface) {
  71. $request = $request->withBody($this->body);
  72. } else {
  73. $body = $this->body;
  74. if (Helper::isBytes($this->body)) {
  75. $body = Helper::toString($this->body);
  76. }
  77. if (\function_exists('\GuzzleHttp\Psr7\stream_for')) {
  78. // @deprecated stream_for will be removed in guzzlehttp/psr7:2.0
  79. $request = $request->withBody(\GuzzleHttp\Psr7\stream_for($body));
  80. } else {
  81. $request = $request->withBody(\GuzzleHttp\Psr7\Utils::streamFor($body));
  82. }
  83. }
  84. }
  85. if ($this->headers) {
  86. foreach ($this->headers as $key => $value) {
  87. $request = $request->withHeader($key, $value);
  88. }
  89. }
  90. return $request;
  91. }
  92. /**
  93. * @param array $query
  94. */
  95. private function assertQuery($query)
  96. {
  97. if (!\is_array($query) && $query !== null) {
  98. throw new InvalidArgumentException('Query must be array.');
  99. }
  100. }
  101. }