对于Firebase admin SDK 5.4及更高版本,如何从不赞成使用的任务迁移到ApiFuture

Al-*_*far 3 java java-8 firebase firebase-admin

我只是想从我的新firebase-admin SDK的Java代码中解决弃用说明,该代码是在版本5.3.1中编写的,但是在将版本升级到5.5.0之后,出现了弃用说明,这是我的示例码:

使用FirebaseAuth(上deprecatation: TaskaddOnSuccessListeneraddOnFailureListener):

private CompletableFuture<FirebaseToken> getDecryptedTokenCompletableFuture(String firebaseTokenString) {
        CompletableFuture<FirebaseToken> tokenFuture = new CompletableFuture<>();
        Task<FirebaseToken> tokenTask = FirebaseAuth.getInstance(firebaseApp).verifyIdToken(firebaseTokenString);
        tokenTask.addOnSuccessListener(tokenFuture::complete);
        tokenTask.addOnFailureListener(exception -> tokenFuture.completeExceptionally(new AuthorizationException("Failed to verify token", exception)));
        return tokenFuture;
    }
Run Code Online (Sandbox Code Playgroud)

而对于FirebaseDatabase(上deprecatation: ,Task,,addOnSuccessListener 和):addOnFailureListenerupdateChildrenremoveValue

public static <T> CompletableFuture<T> toCompletableFuture(Task<T> task) {
    CompletableFuture<T> future = new CompletableFuture<>();
    task.addOnCompleteListener(result -> {
        future.complete(result.getResult());
    }).addOnFailureListener(future::completeExceptionally);
    return future;
}

/**
 * @param updatedParams if null it will removed child
 * @param path          path to update
 * @return void when complete
 */
public CompletableFuture<Void> updateObjectData(Map<String, Object> updatedParams, String path) {
    if (updatedParams == null) {
        return removeObjectData(path);
    }
    logger.debug("Update ObjectData in firebase of ref ({}) with data: {}", path, updatedParams.toString());
    DatabaseReference child = this.getUserDataReference().child(path);
    return toCompletableFuture(child.updateChildren(updatedParams));
}

/**
 * @param path path to of node to remove
 * @return void when complete
 */
public CompletableFuture<Void> removeObjectData(String path) {
    logger.debug("Remove ObjectData in firebase of ref ({})", path);
    DatabaseReference child = this.getUserDataReference().child(path);
    return toCompletableFuture(child.removeValue());
}
Run Code Online (Sandbox Code Playgroud)

我必须使用弃用说明ApiFuture作为发行说明中所说的内容:https : //firebase.google.com/support/release-notes/admin/java

以及内部来源,例如:

  /**
   * Similar to {@link #updateChildrenAsync(Map)} but returns a Task.
   *
   * @param update The paths to update and their new values
   * @return The {@link Task} for this operation.
   * @deprecated Use {@link #updateChildrenAsync(Map)}
   */
Run Code Online (Sandbox Code Playgroud)

/**
 * Represents an asynchronous operation.
 *
 * @param <T> the type of the result of the operation
 * @deprecated {@code Task} has been deprecated in favor of
 *     <a href="https://googleapis.github.io/api-common-java/1.1.0/apidocs/com/google/api/core/ApiFuture.html">{@code ApiFuture}</a>.
 *     For every method x() that returns a {@code Task<T>}, you should be able to find a
 *     corresponding xAsync() method that returns an {@code ApiFuture<T>}.
 */
Run Code Online (Sandbox Code Playgroud)

Eny*_*ado 5

使用ApiFuture从Firebase Admin SDK Java通过FirebaseAuth验证令牌的代码为:

ApiFutures.addCallback(FirebaseAuth.getInstance().verifyIdTokenAsync(token),
  new ApiFutureCallback<FirebaseToken>() {
      @Override
      public void onFailure(Throwable t) {
        // TODO handle failure
      }
      @Override
      public void onSuccess(FirebaseToken decodedToken) {
        // TODO handle success
      }
});
Run Code Online (Sandbox Code Playgroud)

类似的方法可以用于使用FirebaseDatabase的代码。

Hiranya Jayathilaka写了一篇非常详细的文章,解释了如何从Task迁移到ApiFuture及其背后的原理:https ://medium.com/google-cloud/firebase-asynchronous-operations-with-admin-java-sdk-82ca9b4f6022

该代码适用于版本5.4.0-5.8.0,是撰写本文时的最新版本。发行说明可在以下位置找到:https : //firebase.google.com/support/release-notes/admin/java