Skip to content

Commit

Permalink
chore: upgrade recaptcha samples to new client surface (#1948)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored Jan 5, 2024
1 parent d997ed8 commit 9307fcb
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion recaptcha/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require": {
"google/cloud-recaptcha-enterprise": "^1.0"
"google/cloud-recaptcha-enterprise": "^1.7"
}
}
10 changes: 7 additions & 3 deletions recaptcha/src/create_key.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
namespace Google\Cloud\Samples\Recaptcha;

// [START recaptcha_enterprise_create_site_key]
use Google\Cloud\RecaptchaEnterprise\V1\RecaptchaEnterpriseServiceClient;
use Google\ApiCore\ApiException;
use Google\Cloud\RecaptchaEnterprise\V1\Client\RecaptchaEnterpriseServiceClient;
use Google\Cloud\RecaptchaEnterprise\V1\CreateKeyRequest;
use Google\Cloud\RecaptchaEnterprise\V1\Key;
use Google\Cloud\RecaptchaEnterprise\V1\WebKeySettings;
use Google\Cloud\RecaptchaEnterprise\V1\WebKeySettings\IntegrationType;
use Google\ApiCore\ApiException;

/**
* Create a site key for reCAPTCHA
Expand Down Expand Up @@ -61,7 +62,10 @@ function create_key(string $projectId, string $keyName): void
$key->setWebSettings($settings);

try {
$createdKey = $client->createKey($formattedProject, $key);
$createKeyRequest = (new CreateKeyRequest())
->setParent($formattedProject)
->setKey($key);
$createdKey = $client->createKey($createKeyRequest);
printf('The key: %s is created.' . PHP_EOL, $createdKey->getName());
} catch (ApiException $e) {
print('createKey() call failed with the following error: ');
Expand Down
7 changes: 5 additions & 2 deletions recaptcha/src/delete_key.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

// [START recaptcha_enterprise_delete_site_key]
use Google\ApiCore\ApiException;
use Google\Cloud\RecaptchaEnterprise\V1\RecaptchaEnterpriseServiceClient;
use Google\Cloud\RecaptchaEnterprise\V1\Client\RecaptchaEnterpriseServiceClient;
use Google\Cloud\RecaptchaEnterprise\V1\DeleteKeyRequest;

/**
* Delete an existing reCAPTCHA key from your Google Cloud project
Expand All @@ -39,7 +40,9 @@ function delete_key(string $projectId, string $keyId): void
$formattedKeyName = $client->keyName($projectId, $keyId);

try {
$client->deleteKey($formattedKeyName);
$deleteKeyRequest = (new DeleteKeyRequest())
->setName($formattedKeyName);
$client->deleteKey($deleteKeyRequest);
printf('The key: %s is deleted.' . PHP_EOL, $keyId);
} catch (ApiException $e) {
if ($e->getStatus() === 'NOT_FOUND') {
Expand Down
9 changes: 6 additions & 3 deletions recaptcha/src/get_key.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
namespace Google\Cloud\Samples\Recaptcha;

// [START recaptcha_enterprise_get_site_key]
use Google\Cloud\RecaptchaEnterprise\V1\RecaptchaEnterpriseServiceClient;
use Google\Cloud\RecaptchaEnterprise\V1\WebKeySettings\IntegrationType;
use Google\ApiCore\ApiException;
use Google\Cloud\RecaptchaEnterprise\V1\Client\RecaptchaEnterpriseServiceClient;
use Google\Cloud\RecaptchaEnterprise\V1\GetKeyRequest;
use Google\Cloud\RecaptchaEnterprise\V1\WebKeySettings\IntegrationType;

/**
* Get a reCAPTCHA key from a google cloud project
Expand All @@ -41,7 +42,9 @@ function get_key(string $projectId, string $keyId): void

try {
// Returns a 'Google\Cloud\RecaptchaEnterprise\V1\Key' object
$key = $client->getKey($formattedKeyName);
$getKeyRequest = (new GetKeyRequest())
->setName($formattedKeyName);
$key = $client->getKey($getKeyRequest);
$webSettings = $key->getWebSettings();

print('Key fetched' . PHP_EOL);
Expand Down
10 changes: 6 additions & 4 deletions recaptcha/src/list_keys.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
namespace Google\Cloud\Samples\Recaptcha;

// [START recaptcha_enterprise_list_site_keys]
use Google\Cloud\RecaptchaEnterprise\V1\RecaptchaEnterpriseServiceClient;
use Google\ApiCore\ApiException;
use Google\Cloud\RecaptchaEnterprise\V1\Client\RecaptchaEnterpriseServiceClient;
use Google\Cloud\RecaptchaEnterprise\V1\ListKeysRequest;

/**
* List all the reCAPTCHA keys associate to a Google Cloud project
Expand All @@ -38,9 +39,10 @@ function list_keys(string $projectId): void
$formattedProject = $client->projectName($projectId);

try {
$response = $client->listKeys($formattedProject, [
'pageSize' => 2
]);
$listKeysRequest = (new ListKeysRequest())
->setParent($formattedProject)
->setPageSize(2);
$response = $client->listKeys($listKeysRequest);

print('Keys fetched' . PHP_EOL);

Expand Down
12 changes: 7 additions & 5 deletions recaptcha/src/update_key.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
namespace Google\Cloud\Samples\Recaptcha;

// [START recaptcha_enterprise_update_site_key]
use Google\Cloud\RecaptchaEnterprise\V1\RecaptchaEnterpriseServiceClient;
use Google\ApiCore\ApiException;
use Google\Cloud\RecaptchaEnterprise\V1\Client\RecaptchaEnterpriseServiceClient;
use Google\Cloud\RecaptchaEnterprise\V1\Key;
use Google\Cloud\RecaptchaEnterprise\V1\UpdateKeyRequest;
use Google\Cloud\RecaptchaEnterprise\V1\WebKeySettings;
use Google\Cloud\RecaptchaEnterprise\V1\WebKeySettings\ChallengeSecurityPreference;
use Google\Cloud\RecaptchaEnterprise\V1\WebKeySettings\IntegrationType;
use Google\Protobuf\FieldMask;
use Google\ApiCore\ApiException;

/**
* Update an existing reCAPTCHA site key
Expand Down Expand Up @@ -76,9 +77,10 @@ function update_key(
]);

try {
$updatedKey = $client->updateKey($key, [
'updateMask' => $updateMask
]);
$updateKeyRequest = (new UpdateKeyRequest())
->setKey($key)
->setUpdateMask($updateMask);
$updatedKey = $client->updateKey($updateKeyRequest);

printf('The key: %s is updated.' . PHP_EOL, $updatedKey->getDisplayName());
} catch (ApiException $e) {
Expand Down

0 comments on commit 9307fcb

Please sign in to comment.