commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
Loading...
Searching...
No Matches
ApiRequest.php
1<?php
2
3declare(strict_types=1);
10namespace Commercetools\Client;
11
13use GuzzleHttp\ClientInterface;
14use GuzzleHttp\Exception\GuzzleException;
15use GuzzleHttp\Promise\PromiseInterface;
16use GuzzleHttp\Psr7\Query;
17use GuzzleHttp\Psr7\Request;
18use Psr\Http\Message\ResponseInterface;
19use stdClass;
20
22class ApiRequest extends Request implements ApiRequestInterface
23{
25 private $queryParts;
27 private $query;
28
33 private $client;
34
39 public function __construct(?ClientInterface $client, string $method, string $uri, array $headers = [], $body = null, string $version = '1.1')
40 {
41 $this->client = $client;
42 $headers = $this->ensureHeader($headers, 'Content-Type', 'application/json');
43
44 parent::__construct($method, $uri, $headers, $body, $version);
45 }
46
56 protected function ensureHeader(array $headers, string $header, $defaultValue): array
57 {
58 $normalizedHeader = strtolower($header);
59 foreach ($headers as $headerName => $value) {
60 $normalized = strtolower($headerName);
61 if ($normalized !== $normalizedHeader) {
62 continue;
63 }
64 return $headers;
65 }
66 $headers[$header] = $defaultValue;
67
68 return $headers;
69 }
70
77 public function withQueryParam(string $parameterName, $value): ApiRequestInterface
78 {
79 $query = $this->getUri()->getQuery();
80 if ($this->query !== $query) {
82 $this->queryParts = array_map(
87 function ($value): array {
88 if (is_array($value)) {
89 return $value;
90 }
91 return [$value];
92 },
93 Query::parse($query)
94 );
95 }
96 if (is_array($value)) {
97 foreach ($value as $v) {
98 $this->queryParts[$parameterName][] = $v;
99 }
100 } else {
101 $this->queryParts[$parameterName][] = $value;
102 }
103 ksort($this->queryParts);
104 $this->query = Query::build($this->queryParts);
105
106 return $this->withUri($this->getUri()->withQuery($this->query));
107 }
108
115 public function send(array $options = []): ResponseInterface
116 {
117 if (is_null($this->client)) {
118 throw new InvalidArgumentException('No http client set to send the request');
119 }
120 return $this->client->send($this, $options);
121 }
122
129 public function sendAsync(array $options = []): PromiseInterface
130 {
131 if (is_null($this->client)) {
132 throw new InvalidArgumentException('No http client set to send the request');
133 }
134 return $this->client->sendAsync($this, $options);
135 }
136
137 public function getClient(): ?ClientInterface
138 {
139 return $this->client;
140 }
141
145 final protected function responseData(ResponseInterface $response)
146 {
147 $body = (string)$response->getBody();
149 $data = json_decode($body);
150 if (is_null($data)) {
151 return new stdClass();
152 }
153 return $data;
154 }
155}
responseData(ResponseInterface $response)
ensureHeader(array $headers, string $header, $defaultValue)
__construct(?ClientInterface $client, string $method, string $uri, array $headers=[], $body=null, string $version='1.1')
withQueryParam(string $parameterName, $value)