This repository has been archived by the owner on Sep 16, 2023. It is now read-only.
docs(samples): added samples and test for recaptcha key operations #643
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4990e8f
docs(samples): added samples and test for recaptcha key operations
Sita04 15e72af
🦉 Updates from OwlBot
gcf-owl-bot[bot] e1bd4ad
docs(samples): modified return type of ListSiteKey to be re-used in M…
Sita04 0fcaad0
docs(samples): added comment
Sita04 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
samples/snippets/cloud-client/src/main/java/recaptcha/GetMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package recaptcha; | ||
|
||
// [START recaptcha_enterprise_get_metrics_site_key] | ||
|
||
import com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient; | ||
import com.google.recaptchaenterprise.v1.GetMetricsRequest; | ||
import com.google.recaptchaenterprise.v1.Metrics; | ||
import com.google.recaptchaenterprise.v1.MetricsName; | ||
import com.google.recaptchaenterprise.v1.ScoreMetrics; | ||
import java.io.IOException; | ||
|
||
public class GetMetrics { | ||
|
||
public static void main(String[] args) throws IOException { | ||
String projectId = "project-id"; | ||
String recaptchaSiteKey = "recaptcha-site-key"; | ||
|
||
getMetrics(projectId, recaptchaSiteKey); | ||
} | ||
|
||
/** | ||
* Get metrics specific to a recaptcha site key. E.g: score bucket count for a key or number of | ||
* times the checkbox key failed/ passed etc., | ||
* | ||
* @param projectId: Google Cloud Project Id. | ||
* @param recaptchaSiteKey: Specify the site key to get metrics. | ||
*/ | ||
public static void getMetrics(String projectId, String recaptchaSiteKey) throws IOException { | ||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the `client.close()` method on the client to safely | ||
// clean up any remaining background resources. | ||
try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) { | ||
|
||
GetMetricsRequest getMetricsRequest = | ||
GetMetricsRequest.newBuilder() | ||
.setName(MetricsName.of(projectId, recaptchaSiteKey).toString()) | ||
.build(); | ||
|
||
Metrics response = client.getMetrics(getMetricsRequest); | ||
|
||
// Retrieve the metrics you want from the key. | ||
// If the site key is checkbox type: then use response.getChallengeMetricsList() instead of | ||
// response.getScoreMetricsList() | ||
for (ScoreMetrics scoreMetrics : response.getScoreMetricsList()) { | ||
// Each ScoreMetrics is in the granularity of one day. | ||
int scoreBucketCount = scoreMetrics.getOverallMetrics().getScoreBucketsCount(); | ||
System.out.println(scoreBucketCount); | ||
} | ||
System.out.printf("Retrieved the bucket count for score based key: %s", recaptchaSiteKey); | ||
} | ||
} | ||
} | ||
// [END recaptcha_enterprise_get_metrics_site_key] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
samples/snippets/cloud-client/src/main/java/recaptcha/MigrateKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package recaptcha; | ||
|
||
// [START recaptcha_enterprise_migrate_site_key] | ||
|
||
import com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient; | ||
import com.google.recaptchaenterprise.v1.Key; | ||
import com.google.recaptchaenterprise.v1.KeyName; | ||
import com.google.recaptchaenterprise.v1.MigrateKeyRequest; | ||
import java.io.IOException; | ||
|
||
public class MigrateKey { | ||
|
||
public static void main(String[] args) throws IOException { | ||
String projectId = "project-id"; | ||
String recaptchaSiteKey = "recaptcha-site-key"; | ||
|
||
migrateKey(projectId, recaptchaSiteKey); | ||
} | ||
|
||
/** | ||
* Migrate a key from reCAPTCHA (non-Enterprise) to reCAPTCHA Enterprise. If you created the key | ||
* using Admin console: https://www.google.com/recaptcha/admin/site, then use this API to migrate | ||
* to reCAPTCHA Enterprise. For more info, see: | ||
* https://cloud.google.com/recaptcha-enterprise/docs/migrate-recaptcha | ||
* | ||
* @param projectId: Google Cloud Project Id. | ||
* @param recaptchaSiteKey: Specify the site key to migrate. | ||
*/ | ||
public static void migrateKey(String projectId, String recaptchaSiteKey) throws IOException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this one tested? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Migrate key can't be tested through client libraries as the non-enterprise version of the key needs to be created only through the Admin Console. |
||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the `client.close()` method on the client to safely | ||
// clean up any remaining background resources. | ||
try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) { | ||
|
||
// Specify the key name to migrate. | ||
MigrateKeyRequest migrateKeyRequest = | ||
MigrateKeyRequest.newBuilder() | ||
.setName(KeyName.of(projectId, recaptchaSiteKey).toString()) | ||
.build(); | ||
|
||
Key response = client.migrateKey(migrateKeyRequest); | ||
|
||
// To verify if the site key has been migrated, use 'ListSiteKeys' and check if the | ||
// key is present. | ||
for (Key key : recaptcha.ListSiteKeys.listSiteKeys(projectId).iterateAll()) { | ||
if (key.equals(response)) { | ||
System.out.printf("Key migrated successfully: %s", recaptchaSiteKey); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
// [END recaptcha_enterprise_migrate_site_key] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to return the response, keep void. https://github.com/GoogleCloudPlatform/java-docs-samples/blob/main/SAMPLE_FORMAT.md#method-structure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This response is being used in the MigrateKey file. Returning void would mean adding this piece of code to the MigrateKey file (which would be redundant).
Is it okay to treat cases like this as an exception?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should avoid returning any value wherever possible, but otherwise fine