Skip to content

Commit d21c3b7

Browse files
authored
feat(php): add waitForApiKey method (#792)
1 parent e1883b9 commit d21c3b7

File tree

6 files changed

+174
-3
lines changed

6 files changed

+174
-3
lines changed

clients/algoliasearch-client-php/lib/Support/Helpers.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Algolia\AlgoliaSearch\Support;
44

5+
use Algolia\AlgoliaSearch\Api\SearchClient;
56
use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException;
7+
use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
68

79
final class Helpers
810
{
@@ -138,4 +140,85 @@ public static function retryUntil(
138140
'Maximum number of retries (' . $maxRetries . ') exceeded.'
139141
);
140142
}
143+
144+
/**
145+
* Helper for Api keys which retries a function until some conditions are met
146+
*
147+
* @param string $operation
148+
* @param SearchClient $searchClient search client
149+
* @param string $key
150+
* @param array $apiKey
151+
* @param int $maxRetries Max number of retries
152+
* @param int $timeout Timeout
153+
* @param string $timeoutCalculation name of the method to call to calculate the timeout
154+
* @param array $requestOptions
155+
*
156+
* @throws ExceededRetriesException
157+
*
158+
* @return void
159+
*
160+
*/
161+
public static function retryForApiKeyUntil(
162+
$operation,
163+
$searchClient,
164+
$key,
165+
$apiKey,
166+
$maxRetries,
167+
$timeout,
168+
$timeoutCalculation = 'Algolia\AlgoliaSearch\Support\Helpers::linearTimeout',
169+
$requestOptions = []
170+
) {
171+
$retry = 0;
172+
173+
while ($retry < $maxRetries) {
174+
try {
175+
$response = $searchClient->getApiKey($key, $requestOptions);
176+
177+
// In case of an addition, if there was no error, the $key has been added as it should be
178+
if ($operation === 'add') {
179+
return;
180+
}
181+
182+
// In case of an update, check if the key has been updated as it should be
183+
if ($operation === 'update') {
184+
if (self::isKeyUpdated($response, $apiKey)) {
185+
return;
186+
}
187+
}
188+
189+
// Else try again ...
190+
} catch (NotFoundException $e) {
191+
// In case of a deletion, if there was an error, the $key has been deleted as it should be
192+
if (
193+
$operation === 'delete' &&
194+
$e->getMessage() === 'Key does not exist'
195+
) {
196+
return;
197+
}
198+
199+
// Else try again ...
200+
}
201+
202+
$retry++;
203+
usleep(
204+
call_user_func_array($timeoutCalculation, [$timeout, $retry])
205+
);
206+
}
207+
208+
throw new ExceededRetriesException(
209+
'Maximum number of retries (' . $maxRetries . ') exceeded.'
210+
);
211+
}
212+
213+
private static function isKeyUpdated($key, $keyParams)
214+
{
215+
$upToDate = true;
216+
foreach ($keyParams as $param => $value) {
217+
if (isset($key[$param])) {
218+
$upToDate &= $key[$param] === $value;
219+
}
220+
}
221+
222+
return $upToDate;
223+
}
141224
}

templates/javascript/clients/client/api/helpers.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ waitForTask({
1919
},
2020

2121
/**
22-
* Helper: Wait for an API key to be valid, updated or deleted based on a given `operation`.
22+
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
2323
*
2424
* @summary Wait for an API key task to be processed.
2525
* @param waitForApiKeyOptions - The waitForApiKeyOptions object.

templates/php/api.mustache

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,48 @@ use {{invokerPackage}}\Support\Helpers;
322322
$timeout
323323
);
324324
}
325+
326+
/**
327+
* Wait for an API key to be added, updated or deleted based on a given `operation`.
328+
*
329+
* @param string $operation the `operation` that was done on a `key`
330+
* @param string $key the `key` that has been added, deleted or updated
331+
* @param array $apiKey Necessary to know if an `update` operation has been processed, compare fields of the response with it.
332+
* @param int|null $maxRetries Maximum number of retries
333+
* @param int|null $timeout Timeout
334+
* @param array $requestOptions the requestOptions to send along with the query, they will be merged with the transporter requestOptions
335+
*
336+
* @throws ExceededRetriesException
337+
*
338+
* @return void
339+
*/
340+
public function waitForApiKey(
341+
$operation,
342+
$key,
343+
$apiKey = null,
344+
$maxRetries = null,
345+
$timeout = null,
346+
$requestOptions = []
347+
) {
348+
if ($timeout === null) {
349+
$timeout = $this->config->getWaitTaskTimeBeforeRetry();
350+
}
351+
352+
if ($maxRetries === null) {
353+
$maxRetries = $this->config->getDefaultMaxRetries();
354+
}
355+
356+
Helpers::retryForApiKeyUntil(
357+
$operation,
358+
$this,
359+
$key,
360+
$apiKey,
361+
$maxRetries,
362+
$timeout,
363+
null,
364+
$requestOptions
365+
);
366+
}
325367
{{/isSearchClient}}
326368

327369
private function sendRequest($method, $resourcePath, $headers, $queryParameters, $httpBody, $requestOptions, $useReadTransporter = false)

website/docs/clients/guides/copy-or-move-index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ await client.waitForTask({ indexName: '<SOURCE_INDEX_NAME>', taskID });
4141
```php
4242
$client = SearchClient::create(
4343
'<YOUR_APP_ID>',
44-
'<YOUR_APP_ID>'
44+
'<YOUR_API_KEY>'
4545
);
4646

4747
$response = $client->operationIndex(

website/docs/clients/guides/wait-for-a-task-to-finish.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ await client.waitForTask({
4646
```php
4747
$client = SearchClient::create(
4848
'<YOUR_APP_ID>',
49-
'<YOUR_APP_ID>'
49+
'<YOUR_API_KEY>'
5050
);
5151

5252
$response = $client->saveObject(

website/docs/clients/guides/wait-for-api-key-to-be-valid.mdx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,52 @@ await client.waitForApiKey({
4747
// We provide the updated fields to check if the changes have been applied
4848
apiKey: updatesToPerform,
4949
});
50+
51+
// Call for delete
52+
await client.deleteApiKey({ key });
53+
54+
// Wait for delete to be done
55+
await client.waitForApiKey({ operation: 'delete', key });
56+
```
57+
58+
</TabItem>
59+
<TabItem value="php">
60+
61+
> An `operation` can either be `add` | `update` | `delete`
62+
63+
```php
64+
$client = SearchClient::create(
65+
'<YOUR_APP_ID>',
66+
'<YOUR_API_KEY>'
67+
);
68+
69+
$response = $client->addApiKey([
70+
'acl' => ['analytics', 'browse', 'editSettings'],
71+
]);
72+
73+
$key = $response['key'];
74+
75+
// Poll the task status with defaults values
76+
$client->waitForApiKey('add', $key);
77+
78+
// The fields to update on your API key
79+
$updatesToPerform = [
80+
'acl' => ['analytics', 'search'],
81+
'indexes' => ['products'],
82+
];
83+
84+
// Call for update
85+
$client->updateApiKey($key, $updatesToPerform);
86+
87+
// Wait for update to be done
88+
$client->waitForApiKey('update', $key, $updatesToPerform);
89+
90+
// Call for delete
91+
$client->deleteApiKey($key);
92+
93+
// Wait for delete to be done
94+
$client->waitForApiKey('delete', $key);
95+
5096
```
5197

5298
</TabItem>

0 commit comments

Comments
 (0)