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