Search in sources :

Example 36 with Key

use of com.google.recaptchaenterprise.v1.Key in project java-recaptchaenterprise by googleapis.

the class DeleteSiteKey method deleteSiteKey.

/**
 * Delete the given reCAPTCHA site key present under the project ID.
 *
 * @param projectID: GCloud Project ID.
 * @param recaptchaSiteKey: Specify the site key to be deleted.
 */
public static void deleteSiteKey(String projectID, String recaptchaSiteKey) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // clean up any remaining background resources.
    try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
        // Set the project ID and reCAPTCHA site key.
        DeleteKeyRequest deleteKeyRequest = DeleteKeyRequest.newBuilder().setName(KeyName.of(projectID, recaptchaSiteKey).toString()).build();
        client.deleteKeyCallable().futureCall(deleteKeyRequest).get(5, TimeUnit.SECONDS);
        System.out.println("reCAPTCHA Site key successfully deleted !");
    }
}
Also used : DeleteKeyRequest(com.google.recaptchaenterprise.v1.DeleteKeyRequest) RecaptchaEnterpriseServiceClient(com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)

Example 37 with Key

use of com.google.recaptchaenterprise.v1.Key in project java-recaptchaenterprise by googleapis.

the class GetSiteKey method getSiteKey.

/**
 * Get the reCAPTCHA site key present under the project ID.
 *
 * @param projectID: GCloud Project ID.
 * @param recaptchaSiteKey: Specify the site key to get the details.
 */
public static void getSiteKey(String projectID, String recaptchaSiteKey) throws IOException, InterruptedException, ExecutionException, TimeoutException {
    // clean up any remaining background resources.
    try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
        // Construct the "GetSiteKey" request.
        GetKeyRequest getKeyRequest = GetKeyRequest.newBuilder().setName(KeyName.of(projectID, recaptchaSiteKey).toString()).build();
        // Wait for the operation to complete.
        ApiFuture<Key> futureCall = client.getKeyCallable().futureCall(getKeyRequest);
        Key key = futureCall.get(5, TimeUnit.SECONDS);
        System.out.println("Successfully obtained the key !" + key.getName());
    }
}
Also used : GetKeyRequest(com.google.recaptchaenterprise.v1.GetKeyRequest) Key(com.google.recaptchaenterprise.v1.Key) RecaptchaEnterpriseServiceClient(com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)

Example 38 with Key

use of com.google.recaptchaenterprise.v1.Key in project java-recaptchaenterprise by googleapis.

the class ListSiteKeys method listSiteKeys.

/**
 * List all keys present under the given project ID.
 *
 * @param projectID : GCloud Project ID.
 */
public static ListKeysPagedResponse listSiteKeys(String projectID) throws IOException {
    // clean up any remaining background resources.
    try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
        // Set the project ID to list the keys present in it.
        ListKeysRequest listKeysRequest = ListKeysRequest.newBuilder().setParent(ProjectName.of(projectID).toString()).build();
        ListKeysPagedResponse response = client.listKeys(listKeysRequest);
        System.out.println("Listing reCAPTCHA site keys: ");
        for (Key key : response.iterateAll()) {
            System.out.println(key.getName());
        }
        return response;
    }
}
Also used : ListKeysPagedResponse(com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient.ListKeysPagedResponse) ListKeysRequest(com.google.recaptchaenterprise.v1.ListKeysRequest) Key(com.google.recaptchaenterprise.v1.Key) RecaptchaEnterpriseServiceClient(com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)

Example 39 with Key

use of com.google.recaptchaenterprise.v1.Key in project java-recaptchaenterprise by googleapis.

the class RecaptchaEnterpriseServiceClientTest method deleteKeyTest.

@Test
public void deleteKeyTest() throws Exception {
    Empty expectedResponse = Empty.newBuilder().build();
    mockRecaptchaEnterpriseService.addResponse(expectedResponse);
    DeleteKeyRequest request = DeleteKeyRequest.newBuilder().setName(KeyName.of("[PROJECT]", "[KEY]").toString()).build();
    client.deleteKey(request);
    List<AbstractMessage> actualRequests = mockRecaptchaEnterpriseService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    DeleteKeyRequest actualRequest = ((DeleteKeyRequest) actualRequests.get(0));
    Assert.assertEquals(request.getName(), actualRequest.getName());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : Empty(com.google.protobuf.Empty) AbstractMessage(com.google.protobuf.AbstractMessage) DeleteKeyRequest(com.google.recaptchaenterprise.v1.DeleteKeyRequest) Test(org.junit.Test)

Example 40 with Key

use of com.google.recaptchaenterprise.v1.Key in project java-recaptchaenterprise by googleapis.

the class RecaptchaEnterpriseServiceClientTest method deleteKeyExceptionTest.

@Test
public void deleteKeyExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockRecaptchaEnterpriseService.addException(exception);
    try {
        DeleteKeyRequest request = DeleteKeyRequest.newBuilder().setName(KeyName.of("[PROJECT]", "[KEY]").toString()).build();
        client.deleteKey(request);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) DeleteKeyRequest(com.google.recaptchaenterprise.v1.DeleteKeyRequest) 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