如何从已弃用的任务转移到ApiFuture for firebase admin SDK 5.4及更高版本 [英] How to move from deprecated Task to ApiFuture for firebase admin SDK 5.4 and above
问题描述
我只是试图从新的firebase-admin SDK的Java代码中解决弃用说明,代码是用5.3.1版编写的,但是在将版本升级到5.5.0之后,出现了弃用说明,这里是一个我的代码示例:
I'm just trying to solve the deprecation notes from my Java code of new firebase-admin SDK, the code is written in version 5.3.1 but after upgrading the version into 5.5.0 the deprecation notes appeared, here is a sample of my code:
使用 FirebaseAuth (弃用:任务
, addOnSuccessListener
和 addOnFailureListener
):
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 (弃用:任务
, addOnSuccessListener
, addOnFailureListener
, updateChildren
和 removeValue
):
And for FirebaseDatabase (deprecatation on: Task
, addOnSuccessListener
, addOnFailureListener
, updateChildren
and removeValue
) :
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());
}
弃用说明sayign我必须使用 ApiFuture
正如发布说明所述: https:// firebase .google.com / support / release-notes / admin / java
The deprecation note sayign I have to use ApiFuture
as what the release notes saying: 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令牌的代码如下:
The code to verify a Token with FirebaseAuth from Firebase Admin SDK Java using ApiFuture, would be:
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的代码。
Similar approach can be used for your code that uses FirebaseDatabase.
Hiranya Jayathilaka撰写了一篇非常详细的文章,解释了如何从Task迁移到ApiFuture及其背后的理性: https://medium.com/google-cloud/firebase-asynchronous-operations-with-admin-java-sdk-82ca9b4f6022
Hiranya Jayathilaka wrote a very detailed article explaining how to migrate from Task to ApiFuture and the rational behind it: 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
This code works for versions 5.4.0 - 5.8.0, the most recent at the time of this writing. The release notes are available here: https://firebase.google.com/support/release-notes/admin/java
这篇关于如何从已弃用的任务转移到ApiFuture for firebase admin SDK 5.4及更高版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!