Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added exception handling and length of key check #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 35 additions & 23 deletions php/generate_key.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 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',
// 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 ($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";
}

}
catch (Exception $e) {
echo 'Message: ' .$e->getMessage();
}

# Test the created key
Expand All @@ -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";
}
try {
# Test the new generated key by performing a search
if ($search_res = $index->search('')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we also check the search returned a result?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I'll add that and update the PR

echo "Successful key test\n";
} else {
echo "Failed search with the new key\n";
}
}
catch (Exception $e) {
echo 'Message: ' .$e->getMessage();
}

?>