Skip to content

Commit ae32657

Browse files
authored
Do not modify http client state (#755)
1 parent ae69290 commit ae32657

File tree

2 files changed

+56
-26
lines changed

2 files changed

+56
-26
lines changed

src/Contracts/Http.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,46 @@
44

55
namespace Meilisearch\Contracts;
66

7+
use Meilisearch\Exceptions\ApiException;
8+
use Meilisearch\Exceptions\JsonDecodingException;
9+
use Meilisearch\Exceptions\JsonEncodingException;
10+
711
interface Http
812
{
13+
/**
14+
* @throws ApiException
15+
* @throws JsonDecodingException
16+
*/
917
public function get(string $path, array $query = []);
1018

19+
/**
20+
* @param non-empty-string|null $contentType
21+
*
22+
* @throws ApiException
23+
* @throws JsonEncodingException
24+
* @throws JsonDecodingException
25+
*/
1126
public function post(string $path, $body = null, array $query = [], ?string $contentType = null);
1227

28+
/**
29+
* @param non-empty-string|null $contentType
30+
*
31+
* @throws ApiException
32+
* @throws JsonEncodingException
33+
* @throws JsonDecodingException
34+
*/
1335
public function put(string $path, $body = null, array $query = [], ?string $contentType = null);
1436

37+
/**
38+
* @throws ApiException
39+
* @throws JsonEncodingException
40+
* @throws JsonDecodingException
41+
*/
1542
public function patch(string $path, $body = null, array $query = []);
1643

44+
/**
45+
* @throws ApiException
46+
* @throws JsonDecodingException
47+
*/
1748
public function delete(string $path, array $query = []);
1849
}

src/Http/Client.php

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ class Client implements Http
2727
private ClientInterface $http;
2828
private RequestFactoryInterface $requestFactory;
2929
private StreamFactoryInterface $streamFactory;
30-
/** @var array<string,string> */
30+
/**
31+
* @var array<string, string|string[]>
32+
*/
3133
private array $headers;
34+
/**
35+
* @var non-empty-string
36+
*/
3237
private string $baseUrl;
3338
private Json $json;
3439

3540
/**
41+
* @param non-empty-string $url
3642
* @param array<int, string> $clientAgents
3743
*/
3844
public function __construct(
@@ -72,7 +78,7 @@ public function get(string $path, array $query = [])
7278
}
7379

7480
/**
75-
* @param mixed|null $body
81+
* @param non-empty-string|null $contentType
7682
*
7783
* @throws ApiException
7884
* @throws ClientExceptionInterface
@@ -81,57 +87,48 @@ public function get(string $path, array $query = [])
8187
*/
8288
public function post(string $path, $body = null, array $query = [], ?string $contentType = null)
8389
{
84-
if (!\is_null($contentType)) {
85-
$this->headers['Content-type'] = $contentType;
86-
} else {
87-
$this->headers['Content-type'] = 'application/json';
90+
if (null === $contentType) {
8891
$body = $this->json->serialize($body);
8992
}
9093
$request = $this->requestFactory->createRequest(
9194
'POST',
9295
$this->baseUrl.$path.$this->buildQueryString($query)
9396
)->withBody($this->streamFactory->createStream($body));
9497

95-
return $this->execute($request);
98+
return $this->execute($request, ['Content-type' => $contentType ?? 'application/json']);
9699
}
97100

101+
/**
102+
* @param non-empty-string|null $contentType
103+
*
104+
* @throws ApiException
105+
* @throws ClientExceptionInterface
106+
* @throws CommunicationException
107+
* @throws JsonEncodingException
108+
*/
98109
public function put(string $path, $body = null, array $query = [], ?string $contentType = null)
99110
{
100-
if (!\is_null($contentType)) {
101-
$this->headers['Content-type'] = $contentType;
102-
} else {
103-
$this->headers['Content-type'] = 'application/json';
111+
if (null === $contentType) {
104112
$body = $this->json->serialize($body);
105113
}
106114
$request = $this->requestFactory->createRequest(
107115
'PUT',
108116
$this->baseUrl.$path.$this->buildQueryString($query)
109117
)->withBody($this->streamFactory->createStream($body));
110118

111-
return $this->execute($request);
119+
return $this->execute($request, ['Content-type' => $contentType ?? 'application/json']);
112120
}
113121

114-
/**
115-
* @param mixed|null $body
116-
*
117-
* @throws ApiException
118-
* @throws JsonEncodingException
119-
*/
120122
public function patch(string $path, $body = null, array $query = [])
121123
{
122-
$this->headers['Content-type'] = 'application/json';
123124
$request = $this->requestFactory->createRequest(
124125
'PATCH',
125126
$this->baseUrl.$path.$this->buildQueryString($query)
126127
)->withBody($this->streamFactory->createStream($this->json->serialize($body)));
127128

128-
return $this->execute($request);
129+
return $this->execute($request, ['Content-type' => 'application/json']);
129130
}
130131

131-
/**
132-
* @throws ClientExceptionInterface
133-
* @throws ApiException
134-
*/
135132
public function delete(string $path, array $query = [])
136133
{
137134
$request = $this->requestFactory->createRequest(
@@ -143,13 +140,15 @@ public function delete(string $path, array $query = [])
143140
}
144141

145142
/**
143+
* @param array<string, string|string[]> $headers
144+
*
146145
* @throws ApiException
147146
* @throws ClientExceptionInterface
148147
* @throws CommunicationException
149148
*/
150-
private function execute(RequestInterface $request)
149+
private function execute(RequestInterface $request, array $headers = [])
151150
{
152-
foreach ($this->headers as $header => $value) {
151+
foreach (array_merge($this->headers, $headers) as $header => $value) {
153152
$request = $request->withAddedHeader($header, $value);
154153
}
155154

0 commit comments

Comments
 (0)