use of com.google.recaptchaenterprise.v1.Key in project java-recaptchaenterprise by googleapis.
the class GetMetrics method getMetrics.
/**
* 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 {
// 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);
// 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);
}
}
use of com.google.recaptchaenterprise.v1.Key in project java-recaptchaenterprise by googleapis.
the class MigrateKey method migrateKey.
/**
* 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 {
// 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);
// key is present.
for (Key key : recaptcha.ListSiteKeys.listSiteKeys(projectId).iterateAll()) {
if (key.equals(response)) {
System.out.printf("Key migrated successfully: %s", recaptchaSiteKey);
}
}
}
}
use of com.google.recaptchaenterprise.v1.Key in project java-recaptchaenterprise by googleapis.
the class UpdateSiteKey method updateSiteKey.
/**
* Update the properties of the given site key present under the project id.
*
* @param projectID: GCloud Project ID.
* @param recaptchaSiteKeyID: Specify the site key.
* @param domainName: Specify the domain name for which the settings should be updated.
*/
public static void updateSiteKey(String projectID, String recaptchaSiteKeyID, String domainName) throws IOException, InterruptedException, ExecutionException, TimeoutException {
// clean up any remaining background resources.
try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
// Set the name and the new settings for the key.
UpdateKeyRequest updateKeyRequest = UpdateKeyRequest.newBuilder().setKey(Key.newBuilder().setDisplayName("any descriptive name for the key").setName(KeyName.of(projectID, recaptchaSiteKeyID).toString()).setWebSettings(WebKeySettings.newBuilder().setAllowAmpTraffic(true).addAllowedDomains(domainName).build()).build()).build();
client.updateKeyCallable().futureCall(updateKeyRequest).get();
// Check if the key has been updated.
GetKeyRequest getKeyRequest = GetKeyRequest.newBuilder().setName(KeyName.of(projectID, recaptchaSiteKeyID).toString()).build();
Key response = client.getKey(getKeyRequest);
// Get the changed property.
boolean allowedAmpTraffic = response.getWebSettings().getAllowAmpTraffic();
if (!allowedAmpTraffic) {
System.out.println("Error! reCAPTCHA Site key property hasn't been updated. Please try again !");
return;
}
System.out.println("reCAPTCHA Site key successfully updated !");
}
}
use of com.google.recaptchaenterprise.v1.Key in project java-recaptchaenterprise by googleapis.
the class RecaptchaEnterpriseServiceClientTest method createKeyTest.
@Test
public void createKeyTest() throws Exception {
Key expectedResponse = Key.newBuilder().setName(KeyName.of("[PROJECT]", "[KEY]").toString()).setDisplayName("displayName1714148973").putAllLabels(new HashMap<String, String>()).setCreateTime(Timestamp.newBuilder().build()).setTestingOptions(TestingOptions.newBuilder().build()).build();
mockRecaptchaEnterpriseService.addResponse(expectedResponse);
CreateKeyRequest request = CreateKeyRequest.newBuilder().setParent(ProjectName.of("[PROJECT]").toString()).setKey(Key.newBuilder().build()).build();
Key actualResponse = client.createKey(request);
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockRecaptchaEnterpriseService.getRequests();
Assert.assertEquals(1, actualRequests.size());
CreateKeyRequest actualRequest = ((CreateKeyRequest) actualRequests.get(0));
Assert.assertEquals(request.getParent(), actualRequest.getParent());
Assert.assertEquals(request.getKey(), actualRequest.getKey());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
use of com.google.recaptchaenterprise.v1.Key in project java-recaptchaenterprise by googleapis.
the class RecaptchaEnterpriseServiceClientTest method getMetricsExceptionTest.
@Test
public void getMetricsExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
mockRecaptchaEnterpriseService.addException(exception);
try {
MetricsName name = MetricsName.of("[PROJECT]", "[KEY]");
client.getMetrics(name);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception.
}
}
Aggregations