Skip to content

Commit 1a79001

Browse files
committed
Changing paths and verbs
1 parent 0146c63 commit 1a79001

12 files changed

+206
-405
lines changed

App/HttpAppRepository.php

+73-31
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ public function addToken(Token $token)
4040
$response = $this
4141
->httpClient
4242
->get(
43-
'/token',
44-
'post',
45-
Http::getAppQueryValues($this),
46-
[
47-
Http::TOKEN_FIELD => $token->toArray(),
48-
]
43+
sprintf('/%s/tokens', $this->getAppUUID()->getId()),
44+
'put',
45+
[],
46+
$token->toArray(),
47+
Http::getApisearchHeaders($this)
4948
);
5049

5150
self::throwTransportableExceptionIfNeeded($response);
@@ -61,12 +60,15 @@ public function deleteToken(TokenUUID $tokenUUID)
6160
$response = $this
6261
->httpClient
6362
->get(
64-
'/token',
63+
sprintf(
64+
'/%s/tokens/%s',
65+
$this->getAppUUID()->composeUUID(),
66+
$tokenUUID->composeUUID()
67+
),
6568
'delete',
66-
Http::getAppQueryValues($this),
67-
[
68-
Http::TOKEN_FIELD => $tokenUUID->toArray(),
69-
]
69+
[],
70+
[],
71+
Http::getApisearchHeaders($this)
7072
);
7173

7274
self::throwTransportableExceptionIfNeeded($response);
@@ -82,9 +84,14 @@ public function getTokens(): array
8284
$response = $this
8385
->httpClient
8486
->get(
85-
'/tokens',
87+
sprintf(
88+
'/%s/tokens',
89+
$this->getAppUUID()->getId()
90+
),
8691
'get',
87-
Http::getAppQueryValues($this)
92+
[],
93+
[],
94+
Http::getApisearchHeaders($this)
8895
);
8996

9097
self::throwTransportableExceptionIfNeeded($response);
@@ -102,9 +109,14 @@ public function deleteTokens()
102109
$response = $this
103110
->httpClient
104111
->get(
105-
'/tokens',
112+
sprintf(
113+
'/%s/tokens',
114+
$this->getAppUUID()->getId()
115+
),
106116
'delete',
107-
Http::getAppQueryValues($this)
117+
[],
118+
[],
119+
Http::getApisearchHeaders($this)
108120
);
109121

110122
self::throwTransportableExceptionIfNeeded($response);
@@ -120,9 +132,14 @@ public function getIndices(): array
120132
$response = $this
121133
->httpClient
122134
->get(
123-
'/indices',
135+
sprintf(
136+
'/%s/indices',
137+
$this->getAppUUID()->getId()
138+
),
124139
'get',
125-
Http::getAppQueryValues($this)
140+
[],
141+
[],
142+
Http::getApisearchHeaders($this)
126143
);
127144

128145
self::throwTransportableExceptionIfNeeded($response);
@@ -150,13 +167,17 @@ public function createIndex(
150167
$response = $this
151168
->httpClient
152169
->get(
153-
'/index',
170+
sprintf(
171+
'/%s/indices',
172+
$this->getAppUUID()->getId()
173+
),
154174
'put',
155-
Http::getAppQueryValues($this),
175+
[],
156176
[
157177
Http::INDEX_FIELD => $indexUUID->toArray(),
158178
Http::CONFIG_FIELD => $config->toArray(),
159-
]
179+
],
180+
Http::getApisearchHeaders($this)
160181
);
161182

162183
self::throwTransportableExceptionIfNeeded($response);
@@ -174,9 +195,15 @@ public function deleteIndex(IndexUUID $indexUUID)
174195
$response = $this
175196
->httpClient
176197
->get(
177-
'/index',
198+
sprintf(
199+
'/%s/indices/%s',
200+
$this->getAppUUID()->composeUUID(),
201+
$indexUUID->composeUUID()
202+
),
178203
'delete',
179-
Http::getAppQueryValues($this, $indexUUID)
204+
[],
205+
[],
206+
Http::getApisearchHeaders($this)
180207
);
181208

182209
self::throwTransportableExceptionIfNeeded($response);
@@ -194,9 +221,15 @@ public function resetIndex(IndexUUID $indexUUID)
194221
$response = $this
195222
->httpClient
196223
->get(
197-
'/index/reset',
224+
sprintf(
225+
'/%s/indices/%s/reset',
226+
$this->getAppUUID()->composeUUID(),
227+
$indexUUID->composeUUID()
228+
),
198229
'post',
199-
Http::getAppQueryValues($this, $indexUUID)
230+
[],
231+
[],
232+
Http::getApisearchHeaders($this)
200233
);
201234

202235
self::throwTransportableExceptionIfNeeded($response);
@@ -214,9 +247,15 @@ public function checkIndex(IndexUUID $indexUUID): bool
214247
$response = $this
215248
->httpClient
216249
->get(
217-
'/index',
250+
sprintf(
251+
'/%s/indices/%s',
252+
$this->getAppUUID()->composeUUID(),
253+
$indexUUID->composeUUID()
254+
),
218255
'head',
219-
Http::getAppQueryValues($this, $indexUUID)
256+
[],
257+
[],
258+
Http::getApisearchHeaders($this)
220259
);
221260

222261
if (null === $response) {
@@ -241,12 +280,15 @@ public function configureIndex(
241280
$response = $this
242281
->httpClient
243282
->get(
244-
'/index',
283+
sprintf(
284+
'/%s/indices/%s/configure',
285+
$this->getAppUUID()->composeUUID(),
286+
$indexUUID->composeUUID()
287+
),
245288
'post',
246-
Http::getAppQueryValues($this, $indexUUID),
247-
[
248-
Http::CONFIG_FIELD => $config->toArray(),
249-
]
289+
[],
290+
$config->toArray(),
291+
Http::getApisearchHeaders($this)
250292
);
251293

252294
if (null === $response) {

Http/CurlAdapter.php

+15-6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public function getByRequestParts(
4141
$json = json_encode($requestParts->getParameters()['json']);
4242
$formattedUrl = rtrim($host, '/').'/'.ltrim($requestParts->getUrl(), '/');
4343
$method = strtoupper($method);
44+
$headers = [];
45+
foreach ($requestParts->getParameters()['headers'] ?? [] as $header => $value) {
46+
$headers[] = "$header: $value";
47+
}
48+
4449
$c = curl_init();
4550
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
4651
curl_setopt($c, CURLOPT_URL, $formattedUrl);
@@ -50,23 +55,27 @@ public function getByRequestParts(
5055

5156
if (!in_array($method, ['GET', 'HEAD'])) {
5257
curl_setopt($c, CURLOPT_POSTFIELDS, $json);
53-
curl_setopt($c, CURLOPT_HTTPHEADER, [
54-
'Content-Type: application/json',
55-
'Content-Length: '.strlen($json),
56-
]);
58+
$headers[] = 'Content-Type: application/json';
59+
$headers[] = 'Content-Length: '.strlen($json);
5760
}
5861

62+
curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
5963
$content = curl_exec($c);
6064
$responseCode = curl_getinfo($c, CURLINFO_HTTP_CODE);
6165
curl_close($c);
6266

63-
if (false === $content) {
67+
if (
68+
'HEAD' !== $method &&
69+
false === $content
70+
) {
6471
throw ConnectionException::buildConnectExceptionByUrl($requestParts->getUrl());
6572
}
6673

6774
return [
6875
'code' => (int) $responseCode,
69-
'body' => json_decode($content, true),
76+
'body' => empty($content)
77+
? []
78+
: json_decode($content, true),
7079
];
7180
}
7281
}

0 commit comments

Comments
 (0)