gpt4 book ai didi

java - 如何从弃用的任务转移到 ApiFuture for firebase admin SDK 5.4 及更高版本

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:44:42 24 4
gpt4 key购买 nike

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

使用 FirebaseAuth(弃用: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;
}

对于 FirebaseDatabase(弃用:TaskaddOnSuccessListeneraddOnFailureListenerupdateChildrenremoveValue) :

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());
}

弃用说明说我必须使用 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)}
*/

/**
* 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>}.
*/

最佳答案

使用 ApiFuture 从 Firebase Admin SDK Java 中使用 FirebaseAuth 验证 token 的代码是:

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
}
});

可以对使用 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

关于java - 如何从弃用的任务转移到 ApiFuture for firebase admin SDK 5.4 及更高版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47412221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com