commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
ApiRequest.php
1 <?php
2 
3 declare(strict_types=1);
10 namespace Commercetools\Client;
11 
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;
19 use stdClass;
20 
22 class 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)
Definition: ApiRequest.php:145
ensureHeader(array $headers, string $header, $defaultValue)
Definition: ApiRequest.php:56
__construct(?ClientInterface $client, string $method, string $uri, array $headers=[], $body=null, string $version='1.1')
Definition: ApiRequest.php:39
withQueryParam(string $parameterName, $value)
Definition: ApiRequest.php:77
sendAsync(array $options=[])
Definition: ApiRequest.php:129