From 0bfb7cf2b24dc5bfc81e8320d99712e204c32b1e Mon Sep 17 00:00:00 2001 From: LydiaNtafa Date: Thu, 11 Apr 2024 08:53:58 +0000 Subject: [PATCH 1/3] added exception handling and length of key check --- php/generate_key.php | 58 ++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/php/generate_key.php b/php/generate_key.php index 153bcca..0c9d3c7 100644 --- a/php/generate_key.php +++ b/php/generate_key.php @@ -24,25 +24,29 @@ # https://www.algolia.com/doc/api-reference/api-methods/add-api-key/#method-param-acl $acl = ["search"]; -// Set the parameters for API key -// https://www.algolia.com/doc/api-reference/api-methods/add-api-key/#method-param-maxqueriesperipperhour -$params = [ - 'description' => 'Restricted search-only API key for algolia.com', - // Rate-limit to 100 requests per hour per IP address - 'maxQueriesPerIPPerHour' => 100 -]; +try { + // Set the parameters for API key + // https://www.algolia.com/doc/api-reference/api-methods/add-api-key/#method-param-maxqueriesperipperhour + $params = [ + 'description'=> 'Restricted search-only API key for algolia.com', + // Rate-limit to 100 requests per hour per IP address + 'maxQueriesPerIPPerHour' => 100 + ]; -# Create a new restricted search-only API key -print("Creating new key...\n"); -$res = $client->addApiKey($acl, $params)->wait(); - -$new_key = $res['key']; - -if ($new_key = $res['key']) { - echo "Key generated successfully: $new_key \n"; -} else { - echo "Error while creating key\n"; + # Create a new restricted search-only API key + print("Creating new key...\n"); + $res = $client->addApiKey($acl, $params)->wait(); + $new_key = $res['key']; + if (strlen($new_key)==32) { + echo "Key generated successfully: $new_key \n"; + } else { + echo "Failed search with the new key\n"; + } + +} +catch (Exception $e) { + echo 'Message: ' .$e->getMessage(); } # Test the created key @@ -55,9 +59,17 @@ # https://www.algolia.com/doc/api-client/getting-started/instantiate-client-index/#initialize-an-index $index = $client->initIndex($ALGOLIA_INDEX_NAME); -# Test the new generated key by performing a search -if ($search_res = $index->search('')) { - echo "Successful key test\n"; -} else { - echo "Failed search with the new key\n"; -} \ No newline at end of file +try { + # Test the new generated key by performing a search + if ($search_res = $index->search('')) { + echo "Successful key test\n"; + } else { + echo "Failed search with the new key\n"; + } +} +catch (Exception $e) { + echo 'Message: ' .$e->getMessage(); +} + +?> + From 021ea9a6f96980926f2099f201d2890b0f5acd43 Mon Sep 17 00:00:00 2001 From: Lydia Ntafa Date: Thu, 18 Apr 2024 09:15:10 +0100 Subject: [PATCH 2/3] Update php/generate_key.php Co-authored-by: mbond <59652368+matthewbond@users.noreply.github.com> --- php/generate_key.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/generate_key.php b/php/generate_key.php index 0c9d3c7..ebe2a38 100644 --- a/php/generate_key.php +++ b/php/generate_key.php @@ -26,7 +26,7 @@ try { - // Set the parameters for API key + // Set the parameters for the API key // https://www.algolia.com/doc/api-reference/api-methods/add-api-key/#method-param-maxqueriesperipperhour $params = [ 'description'=> 'Restricted search-only API key for algolia.com', From 3519df8dd2fd9b1875c9736949d0c77be4cbd88d Mon Sep 17 00:00:00 2001 From: Lydia Ntafa Date: Thu, 18 Apr 2024 09:18:08 +0100 Subject: [PATCH 3/3] Update php/generate_key.php Co-authored-by: mbond <59652368+matthewbond@users.noreply.github.com> --- php/generate_key.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/generate_key.php b/php/generate_key.php index ebe2a38..6491f16 100644 --- a/php/generate_key.php +++ b/php/generate_key.php @@ -38,7 +38,7 @@ print("Creating new key...\n"); $res = $client->addApiKey($acl, $params)->wait(); $new_key = $res['key']; - if (strlen($new_key)==32) { + if ($new_key = $res['key'] ?? false && strlen($new_key)===32) { echo "Key generated successfully: $new_key \n"; } else { echo "Failed search with the new key\n";