3declare(strict_types=1);
13use GuzzleHttp\ClientInterface;
14use GuzzleHttp\Exception\GuzzleException;
15use GuzzleHttp\Promise\PromiseInterface;
16use GuzzleHttp\Psr7\Query;
17use GuzzleHttp\Psr7\Request;
18use Psr\Http\Message\ResponseInterface;
39 public function __construct(?ClientInterface $client,
string $method,
string $uri, array $headers = [], $body =
null,
string $version =
'1.1')
41 $this->client = $client;
42 $headers = $this->
ensureHeader($headers,
'Content-Type',
'application/json');
44 parent::__construct($method, $uri, $headers, $body, $version);
55 if ($value ===
'.' || $value ===
'..') {
59 return urlencode($value);
71 protected function ensureHeader(array $headers,
string $header, $defaultValue): array
73 $normalizedHeader = strtolower($header);
74 foreach (array_keys($headers) as $headerName) {
75 $normalized = strtolower($headerName);
76 if ($normalized !== $normalizedHeader) {
81 $headers[$header] = $defaultValue;
94 $query = $this->getUri()->getQuery();
95 if ($this->query !== $query) {
97 $this->queryParts = array_map(
102 function ($value): array {
103 if (is_array($value)) {
111 if (is_array($value)) {
112 foreach ($value as $v) {
113 $this->queryParts[$parameterName][] = $v;
116 $this->queryParts[$parameterName][] = $value;
118 ksort($this->queryParts);
119 $this->query = Query::build($this->queryParts);
121 return $this->withUri($this->getUri()->withQuery($this->query));
130 public function send(array $options = []): ResponseInterface
132 if (is_null($this->client)) {
135 return $this->client->send($this, $options);
144 public function sendAsync(array $options = []): PromiseInterface
146 if (is_null($this->client)) {
149 return $this->client->sendAsync($this, $options);
154 return $this->client;
162 $body = (string)$response->getBody();
164 $data = json_decode($body);
165 if (is_null($data)) {
166 return new stdClass();