3 declare(strict_types=1);
13 use GuzzleHttp\ClientInterface;
14 use GuzzleHttp\Exception\GuzzleException;
15 use GuzzleHttp\Promise\PromiseInterface;
16 use GuzzleHttp\Psr7\Query;
17 use GuzzleHttp\Psr7\Request;
18 use 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);
56 protected function ensureHeader(array $headers,
string $header, $defaultValue): array
58 $normalizedHeader = strtolower($header);
59 foreach ($headers as $headerName => $value) {
60 $normalized = strtolower($headerName);
61 if ($normalized !== $normalizedHeader) {
66 $headers[$header] = $defaultValue;
79 $query = $this->getUri()->getQuery();
80 if ($this->query !== $query) {
82 $this->queryParts = array_map(
87 function ($value): array {
88 if (is_array($value)) {
96 if (is_array($value)) {
97 foreach ($value as $v) {
98 $this->queryParts[$parameterName][] = $v;
101 $this->queryParts[$parameterName][] = $value;
103 ksort($this->queryParts);
104 $this->query = Query::build($this->queryParts);
106 return $this->withUri($this->getUri()->withQuery($this->query));
115 public function send(array $options = []): ResponseInterface
117 if (is_null($this->client)) {
120 return $this->client->send($this, $options);
129 public function sendAsync(array $options = []): PromiseInterface
131 if (is_null($this->client)) {
134 return $this->client->sendAsync($this, $options);
139 return $this->client;
147 $body = (string)$response->getBody();
149 $data = json_decode($body);
150 if (is_null($data)) {
151 return new stdClass();