项目作者: lucasvalenteds

项目描述 :
Async/Await support for Firebase Admin SDK
高级语言: Kotlin
项目地址: git://github.com/lucasvalenteds/firebase-admin-coroutines.git


Deprecation notice

A better implementation has been merged into the official kotlin/kotlinx.coroutines repository.

Please, use the official implementation instead.

Firebase Admin Coroutines

Coroutine wrappers for Tasks API and ApiFuture API used in Firebase Admin SDK.

Example

The following code shows how to get an user from Firebase and delete it using two approaches.

The standard approach is to write nested callback functions.

  1. val auth = FirebaseAuth.getInstance()
  2. auth.getUserByEmail(email)
  3. .addOnSuccessListener { user ->
  4. auth.deleteUser(user.uid)
  5. .addOnSuccessListener { println("User deleted") }
  6. .addOnFailureListener { exception -> println(exception) }
  7. }
  8. .addOnFailureListener { exception -> println(exception) }

The other approach is the use of a suspended coroutine to make the code flatter, so we can call the await() method
to wait the first asynchronous operations (retrieve the user) to complete before starting the second one (delete the user).

  1. val auth = FirebaseAuth.getInstance()
  2. try {
  3. val user = auth.getUserByEmail(email).await()
  4. auth.deleteUser(user.uid).await()
  5. println("User deleted")
  6. } catch (exception: FirebaseAuthException) {
  7. println(exception)
  8. }

How to use

Assuming you already have the Firebase Admin SDK as a dependency in your project, you just need to copy and paste the functions into your project.

Firebase SDK version Function
5.x.x or earlier Task<T>.await()
6.x.x or later ApiFuture<T>.await()

About testing

To run the test cases you’ll need to put the private key of a Firebase project in the root path. It also should be named google-services.json. In case you don’t the tests will throw an error due to unauthorized call to the Firebase servers.

If you know how to implement unit tests for the case, please fill an issue and send a pull request.

Contributing

If you found any bug or want to improve the project, please feel free to fork it, fill issues and send pull requests.