@Override public ListenableFuture<TransactionResult> transactionAsync() { final ListenableFuture<Response> httpResponse; try { final BeginTransactionRequest.Builder request = BeginTransactionRequest.newBuilder(); final ProtoHttpContent payload = new ProtoHttpContent(request.build()); httpResponse = ListenableFutureAdapter.asGuavaFuture(prepareRequest("beginTransaction", payload).execute()); } catch (final Exception e) { return Futures.immediateFailedFuture(new DatastoreException(e)); } return Futures.transformAsync(httpResponse, response -> { if (!isSuccessful(response.getStatusCode())) { throw new DatastoreException(response.getStatusCode(), response.getResponseBody()); } final BeginTransactionResponse transaction = BeginTransactionResponse.parseFrom(streamResponse(response)); return Futures.immediateFuture(TransactionResult.build(transaction)); }); }
@Override public ListenableFuture<RollbackResult> rollbackAsync(final ListenableFuture<TransactionResult> txn) { final ListenableFuture<Response> httpResponse = Futures.transformAsync(txn, result -> { final ByteString transaction = result.getTransaction(); if (transaction == null) { throw new DatastoreException("Invalid transaction."); } final RollbackRequest.Builder request = RollbackRequest.newBuilder(); final ProtoHttpContent payload = new ProtoHttpContent(request.build()); return ListenableFutureAdapter.asGuavaFuture(prepareRequest("rollback", payload).execute()); }); return Futures.transformAsync(httpResponse, response -> { if (!isSuccessful(response.getStatusCode())) { throw new DatastoreException(response.getStatusCode(), response.getResponseBody()); } final RollbackResponse rollback = RollbackResponse.parseFrom(streamResponse(response)); return Futures.immediateFuture(RollbackResult.build(rollback)); }); }
@Override public ListenableFuture<AllocateIdsResult> executeAsync(final AllocateIds statement) { final ListenableFuture<Response> httpResponse; try { final AllocateIdsRequest.Builder request = AllocateIdsRequest.newBuilder() .addAllKeys(statement.getPb(config.getNamespace())); final ProtoHttpContent payload = new ProtoHttpContent(request.build()); httpResponse = ListenableFutureAdapter.asGuavaFuture(prepareRequest("allocateIds", payload).execute()); } catch (final Exception e) { return Futures.immediateFailedFuture(new DatastoreException(e)); } return Futures.transformAsync(httpResponse, response -> { if (!isSuccessful(response.getStatusCode())) { throw new DatastoreException(response.getStatusCode(), response.getResponseBody()); } final AllocateIdsResponse allocate = AllocateIdsResponse.parseFrom(streamResponse(response)); return Futures.immediateFuture(AllocateIdsResult.build(allocate)); }); }
@Override public ListenableFuture<QueryResult> executeAsync(final List<KeyQuery> statements, final ListenableFuture<TransactionResult> txn) { final ListenableFuture<Response> httpResponse = Futures.transformAsync(txn, result -> { final List<com.google.datastore.v1.Key> keys = statements .stream().map(s -> s.getKey().getPb(config.getNamespace())).collect(Collectors.toList()); final LookupRequest.Builder request = LookupRequest.newBuilder().addAllKeys(keys); final ByteString transaction = result.getTransaction(); if (transaction != null) { request.setReadOptions(ReadOptions.newBuilder().setTransaction(transaction)); } final ProtoHttpContent payload = new ProtoHttpContent(request.build()); return ListenableFutureAdapter.asGuavaFuture(prepareRequest("lookup", payload).execute()); }); return Futures.transformAsync(httpResponse, response -> { if (!isSuccessful(response.getStatusCode())) { throw new DatastoreException(response.getStatusCode(), response.getResponseBody()); } final LookupResponse query = LookupResponse.parseFrom(streamResponse(response)); return Futures.immediateFuture(QueryResult.build(query)); }); }
private ListenableFuture<MutationResult> executeAsyncMutations(final List<Mutation> mutations, final ListenableFuture<TransactionResult> txn) { final ListenableFuture<Response> httpResponse = Futures.transformAsync(txn, result -> { final CommitRequest.Builder request = CommitRequest.newBuilder(); if (mutations != null) { request.addAllMutations(mutations); } final ByteString transaction = result.getTransaction(); if (transaction != null) { request.setTransaction(transaction); } else { request.setMode(CommitRequest.Mode.NON_TRANSACTIONAL); } final ProtoHttpContent payload = new ProtoHttpContent(request.build()); return ListenableFutureAdapter.asGuavaFuture(prepareRequest("commit", payload).execute()); }); return Futures.transformAsync(httpResponse, response -> { if (!isSuccessful(response.getStatusCode())) { throw new DatastoreException(response.getStatusCode(), response.getResponseBody()); } final CommitResponse commit = CommitResponse.parseFrom(streamResponse(response)); return Futures.immediateFuture(MutationResult.build(commit)); }); }
@Override public ListenableFuture<QueryResult> executeAsync(final Query statement, final ListenableFuture<TransactionResult> txn) { final ListenableFuture<Response> httpResponse = Futures.transformAsync(txn, result -> { final String namespace = config.getNamespace(); final RunQueryRequest.Builder request = RunQueryRequest.newBuilder() .setQuery(statement.getPb(namespace != null ? namespace : "")); if (namespace != null) { request.setPartitionId(PartitionId.newBuilder().setNamespaceId(namespace)); } final ByteString transaction = result.getTransaction(); if (transaction != null) { request.setReadOptions(ReadOptions.newBuilder().setTransaction(transaction)); } final ProtoHttpContent payload = new ProtoHttpContent(request.build()); return ListenableFutureAdapter.asGuavaFuture(prepareRequest("runQuery", payload).execute()); }); return Futures.transformAsync(httpResponse, response -> { if (!isSuccessful(response.getStatusCode())) { throw new DatastoreException(response.getStatusCode(), response.getResponseBody()); } final RunQueryResponse query = RunQueryResponse.parseFrom(streamResponse(response)); return Futures.immediateFuture(QueryResult.build(query)); }); }
private AsyncHttpClient.BoundRequestBuilder prepareRequest(final String method, final ProtoHttpContent payload) throws IOException { final AsyncHttpClient.BoundRequestBuilder builder = client.preparePost(prefixUri + method); builder.addHeader("Authorization", "Bearer " + accessToken); builder.addHeader("Content-Type", "application/x-protobuf"); builder.addHeader("User-Agent", USER_AGENT); builder.addHeader("Accept-Encoding", "gzip"); builder.setContentLength((int) payload.getLength()); builder.setBody(payload.getMessage().toByteArray()); return builder; }
/** * Attempts to allocate quota for the specified consumer. It should be called before the * operation is executed. * * This method requires the `servicemanagement.services.quota` permission on the specified * service. For more information, see [Google Cloud IAM](https://cloud.google.com/iam). * * **NOTE:** the client code **must** fail-open if the server returns one of the following * quota errors: - `PROJECT_STATUS_UNAVAILABLE` - `SERVICE_STATUS_UNAVAILABLE` - * `BILLING_STATUS_UNAVAILABLE` - `QUOTA_SYSTEM_UNAVAILABLE` * * The server may inject above errors to prohibit any hard dependency on the quota system. * * Create a request for the method "services.allocateQuota". * * This request holds the parameters needed by the the servicecontrol server. After setting * any optional parameters, call the {@link AllocateQuota#execute()} method to invoke the * remote operation. <p> {@link AllocateQuota#initialize(com.google.api.client.googleapis.services.AbstractGoogleClientRequest)} * must be called to initialize this instance immediately after invoking the constructor. * </p> * * @param serviceName Name of the service as specified in the service configuration. For * example, `"pubsub.googleapis.com"`. See google.api.Service for the definition of a service * name. * @param content the {@link com.google.api.servicecontrol.v1.AllocateQuotaRequest} * @since 1.13 */ protected AllocateQuota(java.lang.String serviceName, com.google.api.servicecontrol.v1.AllocateQuotaRequest content) { super(ServiceControl.this, "POST", REST_PATH, new ProtoHttpContent(content), com.google.api.servicecontrol.v1.AllocateQuotaResponse.class); this.serviceName = com.google.api.client.util.Preconditions .checkNotNull(serviceName, "Required parameter serviceName must be specified."); }
/** * Checks an operation with Google Service Control to decide whether the given operation * should proceed. It should be called before the operation is executed. * * If feasible, the client should cache the check results and reuse them for 60 seconds. In * case of server errors, the client can rely on the cached results for longer time. * * NOTE: the `CheckRequest` has the size limit of 64KB. * * This method requires the `servicemanagement.services.check` permission on the specified * service. For more information, see [Google Cloud IAM](https://cloud.google.com/iam). * * Create a request for the method "services.check". * * This request holds the parameters needed by the the servicecontrol server. After setting * any optional parameters, call the {@link Check#execute()} method to invoke the remote * operation. <p> {@link Check#initialize(com.google.api.client.googleapis.services.AbstractGoogleClientRequest)} * must be called to initialize this instance immediately after invoking the constructor. * </p> * * @param serviceName The service name as specified in its service configuration. For example, * `"pubsub.googleapis.com"`. See google.api.Service for the definition of a service name. * @param content the {@link com.google.api.servicecontrol.v1.CheckRequest} * @since 1.13 */ protected Check(java.lang.String serviceName, com.google.api.servicecontrol.v1.CheckRequest content) { super(ServiceControl.this, "POST", REST_PATH, new ProtoHttpContent(content), com.google.api.servicecontrol.v1.CheckResponse.class); this.serviceName = com.google.api.client.util.Preconditions .checkNotNull(serviceName, "Required parameter serviceName must be specified."); }
/** * Reports operation results to Google Service Control, such as logs and metrics. It should be * called after an operation is completed. * * If feasible, the client should aggregate reporting data for up to 5 seconds to reduce API * traffic. Limiting aggregation to 5 seconds is to reduce data loss during client crashes. * Clients should carefully choose the aggregation time window to avoid data loss risk more * than 0.01% for business and compliance reasons. * * NOTE: the `ReportRequest` has the size limit of 1MB. * * This method requires the `servicemanagement.services.report` permission on the specified * service. For more information, see [Google Cloud IAM](https://cloud.google.com/iam). * * Create a request for the method "services.report". * * This request holds the parameters needed by the the servicecontrol server. After setting * any optional parameters, call the {@link Report#execute()} method to invoke the remote * operation. <p> {@link Report#initialize(com.google.api.client.googleapis.services.AbstractGoogleClientRequest)} * must be called to initialize this instance immediately after invoking the constructor. * </p> * * @param serviceName The service name as specified in its service configuration. For example, * `"pubsub.googleapis.com"`. See google.api.Service for the definition of a service name. * @param content the {@link com.google.api.servicecontrol.v1.ReportRequest} * @since 1.13 */ protected Report(java.lang.String serviceName, com.google.api.servicecontrol.v1.ReportRequest content) { super(ServiceControl.this, "POST", REST_PATH, new ProtoHttpContent(content), com.google.api.servicecontrol.v1.ReportResponse.class); this.serviceName = com.google.api.client.util.Preconditions .checkNotNull(serviceName, "Required parameter serviceName must be specified."); }