Skip to content

Commit 9307fcb

Browse files
authored
chore: upgrade recaptcha samples to new client surface (#1948)
1 parent d997ed8 commit 9307fcb

6 files changed

Lines changed: 32 additions & 18 deletions

File tree

recaptcha/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"require": {
3-
"google/cloud-recaptcha-enterprise": "^1.0"
3+
"google/cloud-recaptcha-enterprise": "^1.7"
44
}
55
}

recaptcha/src/create_key.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424
namespace Google\Cloud\Samples\Recaptcha;
2525

2626
// [START recaptcha_enterprise_create_site_key]
27-
use Google\Cloud\RecaptchaEnterprise\V1\RecaptchaEnterpriseServiceClient;
27+
use Google\ApiCore\ApiException;
28+
use Google\Cloud\RecaptchaEnterprise\V1\Client\RecaptchaEnterpriseServiceClient;
29+
use Google\Cloud\RecaptchaEnterprise\V1\CreateKeyRequest;
2830
use Google\Cloud\RecaptchaEnterprise\V1\Key;
2931
use Google\Cloud\RecaptchaEnterprise\V1\WebKeySettings;
3032
use Google\Cloud\RecaptchaEnterprise\V1\WebKeySettings\IntegrationType;
31-
use Google\ApiCore\ApiException;
3233

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

6364
try {
64-
$createdKey = $client->createKey($formattedProject, $key);
65+
$createKeyRequest = (new CreateKeyRequest())
66+
->setParent($formattedProject)
67+
->setKey($key);
68+
$createdKey = $client->createKey($createKeyRequest);
6569
printf('The key: %s is created.' . PHP_EOL, $createdKey->getName());
6670
} catch (ApiException $e) {
6771
print('createKey() call failed with the following error: ');

recaptcha/src/delete_key.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525

2626
// [START recaptcha_enterprise_delete_site_key]
2727
use Google\ApiCore\ApiException;
28-
use Google\Cloud\RecaptchaEnterprise\V1\RecaptchaEnterpriseServiceClient;
28+
use Google\Cloud\RecaptchaEnterprise\V1\Client\RecaptchaEnterpriseServiceClient;
29+
use Google\Cloud\RecaptchaEnterprise\V1\DeleteKeyRequest;
2930

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

4142
try {
42-
$client->deleteKey($formattedKeyName);
43+
$deleteKeyRequest = (new DeleteKeyRequest())
44+
->setName($formattedKeyName);
45+
$client->deleteKey($deleteKeyRequest);
4346
printf('The key: %s is deleted.' . PHP_EOL, $keyId);
4447
} catch (ApiException $e) {
4548
if ($e->getStatus() === 'NOT_FOUND') {

recaptcha/src/get_key.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
namespace Google\Cloud\Samples\Recaptcha;
2525

2626
// [START recaptcha_enterprise_get_site_key]
27-
use Google\Cloud\RecaptchaEnterprise\V1\RecaptchaEnterpriseServiceClient;
28-
use Google\Cloud\RecaptchaEnterprise\V1\WebKeySettings\IntegrationType;
2927
use Google\ApiCore\ApiException;
28+
use Google\Cloud\RecaptchaEnterprise\V1\Client\RecaptchaEnterpriseServiceClient;
29+
use Google\Cloud\RecaptchaEnterprise\V1\GetKeyRequest;
30+
use Google\Cloud\RecaptchaEnterprise\V1\WebKeySettings\IntegrationType;
3031

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

4243
try {
4344
// Returns a 'Google\Cloud\RecaptchaEnterprise\V1\Key' object
44-
$key = $client->getKey($formattedKeyName);
45+
$getKeyRequest = (new GetKeyRequest())
46+
->setName($formattedKeyName);
47+
$key = $client->getKey($getKeyRequest);
4548
$webSettings = $key->getWebSettings();
4649

4750
print('Key fetched' . PHP_EOL);

recaptcha/src/list_keys.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
namespace Google\Cloud\Samples\Recaptcha;
2525

2626
// [START recaptcha_enterprise_list_site_keys]
27-
use Google\Cloud\RecaptchaEnterprise\V1\RecaptchaEnterpriseServiceClient;
2827
use Google\ApiCore\ApiException;
28+
use Google\Cloud\RecaptchaEnterprise\V1\Client\RecaptchaEnterpriseServiceClient;
29+
use Google\Cloud\RecaptchaEnterprise\V1\ListKeysRequest;
2930

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

4041
try {
41-
$response = $client->listKeys($formattedProject, [
42-
'pageSize' => 2
43-
]);
42+
$listKeysRequest = (new ListKeysRequest())
43+
->setParent($formattedProject)
44+
->setPageSize(2);
45+
$response = $client->listKeys($listKeysRequest);
4446

4547
print('Keys fetched' . PHP_EOL);
4648

recaptcha/src/update_key.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@
2424
namespace Google\Cloud\Samples\Recaptcha;
2525

2626
// [START recaptcha_enterprise_update_site_key]
27-
use Google\Cloud\RecaptchaEnterprise\V1\RecaptchaEnterpriseServiceClient;
27+
use Google\ApiCore\ApiException;
28+
use Google\Cloud\RecaptchaEnterprise\V1\Client\RecaptchaEnterpriseServiceClient;
2829
use Google\Cloud\RecaptchaEnterprise\V1\Key;
30+
use Google\Cloud\RecaptchaEnterprise\V1\UpdateKeyRequest;
2931
use Google\Cloud\RecaptchaEnterprise\V1\WebKeySettings;
3032
use Google\Cloud\RecaptchaEnterprise\V1\WebKeySettings\ChallengeSecurityPreference;
3133
use Google\Cloud\RecaptchaEnterprise\V1\WebKeySettings\IntegrationType;
3234
use Google\Protobuf\FieldMask;
33-
use Google\ApiCore\ApiException;
3435

3536
/**
3637
* Update an existing reCAPTCHA site key
@@ -76,9 +77,10 @@ function update_key(
7677
]);
7778

7879
try {
79-
$updatedKey = $client->updateKey($key, [
80-
'updateMask' => $updateMask
81-
]);
80+
$updateKeyRequest = (new UpdateKeyRequest())
81+
->setKey($key)
82+
->setUpdateMask($updateMask);
83+
$updatedKey = $client->updateKey($updateKeyRequest);
8284

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

0 commit comments

Comments
 (0)