Search in sources :

Example 6 with Key

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);
    }
}
Also used : Metrics(com.google.recaptchaenterprise.v1.Metrics) ScoreMetrics(com.google.recaptchaenterprise.v1.ScoreMetrics) ScoreMetrics(com.google.recaptchaenterprise.v1.ScoreMetrics) GetMetricsRequest(com.google.recaptchaenterprise.v1.GetMetricsRequest) RecaptchaEnterpriseServiceClient(com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)

Example 7 with Key

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);
            }
        }
    }
}
Also used : MigrateKeyRequest(com.google.recaptchaenterprise.v1.MigrateKeyRequest) Key(com.google.recaptchaenterprise.v1.Key) RecaptchaEnterpriseServiceClient(com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)

Example 8 with Key

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 !");
    }
}
Also used : UpdateKeyRequest(com.google.recaptchaenterprise.v1.UpdateKeyRequest) GetKeyRequest(com.google.recaptchaenterprise.v1.GetKeyRequest) Key(com.google.recaptchaenterprise.v1.Key) RecaptchaEnterpriseServiceClient(com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)

Example 9 with Key

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()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) HashMap(java.util.HashMap) CreateKeyRequest(com.google.recaptchaenterprise.v1.CreateKeyRequest) Key(com.google.recaptchaenterprise.v1.Key) Test(org.junit.Test)

Example 10 with Key

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.
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) MetricsName(com.google.recaptchaenterprise.v1.MetricsName) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)27 Key (com.google.datastore.v1.Key)17 AbstractMessage (com.google.protobuf.AbstractMessage)12 DatastoreHelper.makeKey (com.google.datastore.v1.client.DatastoreHelper.makeKey)11 Key (com.google.recaptchaenterprise.v1.Key)10 RecaptchaEnterpriseServiceClient (com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)8 Entity (com.google.datastore.v1.Entity)7 DeleteKey (org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.DeleteKey)7 DatastoreV1.isValidKey (org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.isValidKey)7 Mutation (com.google.datastore.v1.Mutation)5 ArrayList (java.util.ArrayList)5 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)4 GetKeyRequest (com.google.recaptchaenterprise.v1.GetKeyRequest)4 Key (com.google.recaptchaenterprise.v1beta1.Key)4 StatusRuntimeException (io.grpc.StatusRuntimeException)4 HashMap (java.util.HashMap)4 Query (com.google.datastore.v1.Query)3 DeleteKeyRequest (com.google.recaptchaenterprise.v1.DeleteKeyRequest)3 GetMetricsRequest (com.google.recaptchaenterprise.v1.GetMetricsRequest)3 Metrics (com.google.recaptchaenterprise.v1.Metrics)3