From 9307fcb7ab810f72337693568e744d578f8181d2 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 5 Jan 2024 15:37:57 -0600 Subject: [PATCH] chore: upgrade recaptcha samples to new client surface (#1948) --- recaptcha/composer.json | 2 +- recaptcha/src/create_key.php | 10 +++++++--- recaptcha/src/delete_key.php | 7 +++++-- recaptcha/src/get_key.php | 9 ++++++--- recaptcha/src/list_keys.php | 10 ++++++---- recaptcha/src/update_key.php | 12 +++++++----- 6 files changed, 32 insertions(+), 18 deletions(-) diff --git a/recaptcha/composer.json b/recaptcha/composer.json index c55aabd226..d12a4a5e4c 100644 --- a/recaptcha/composer.json +++ b/recaptcha/composer.json @@ -1,5 +1,5 @@ { "require": { - "google/cloud-recaptcha-enterprise": "^1.0" + "google/cloud-recaptcha-enterprise": "^1.7" } } diff --git a/recaptcha/src/create_key.php b/recaptcha/src/create_key.php index d36d8fcea5..bfd04eedd3 100644 --- a/recaptcha/src/create_key.php +++ b/recaptcha/src/create_key.php @@ -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 @@ -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: '); diff --git a/recaptcha/src/delete_key.php b/recaptcha/src/delete_key.php index 3be945e085..81a2d0168d 100644 --- a/recaptcha/src/delete_key.php +++ b/recaptcha/src/delete_key.php @@ -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 @@ -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') { diff --git a/recaptcha/src/get_key.php b/recaptcha/src/get_key.php index e1d7ce296f..51b6edf151 100644 --- a/recaptcha/src/get_key.php +++ b/recaptcha/src/get_key.php @@ -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 @@ -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); diff --git a/recaptcha/src/list_keys.php b/recaptcha/src/list_keys.php index fe1ba1ada4..d52efdadc3 100644 --- a/recaptcha/src/list_keys.php +++ b/recaptcha/src/list_keys.php @@ -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 @@ -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); diff --git a/recaptcha/src/update_key.php b/recaptcha/src/update_key.php index 18b1709e1b..62481afcbf 100644 --- a/recaptcha/src/update_key.php +++ b/recaptcha/src/update_key.php @@ -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 @@ -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) {