Skip to content

Instantly share code, notes, and snippets.

@ryanpbrewster
Last active June 4, 2019 18:37
Show Gist options
  • Save ryanpbrewster/aef2a5c411a074819c8d7b67be80621c to your computer and use it in GitHub Desktop.
Save ryanpbrewster/aef2a5c411a074819c8d7b67be80621c to your computer and use it in GitHub Desktop.
import java.net.URI
import java.util
import com.google.api.gax.core.FixedCredentialsProvider
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider
import com.google.auth.Credentials
import com.google.cloud.firestore.{CollectionReference, FirestoreOptions}
import com.google.common.collect.{ImmutableList, ImmutableMap}
import io.grpc.ManagedChannelBuilder
object FirestoreEmulatorExample {
def main(args: Array[String]): Unit = {
val db = FirestoreOptions.newBuilder()
.setProjectId("foo")
.setChannelProvider(
InstantiatingGrpcChannelProvider.newBuilder().setEndpoint("localhost:8080")
.setChannelConfigurator((input: ManagedChannelBuilder[_]) => {
input.usePlaintext()
input
}).build())
.setCredentialsProvider(FixedCredentialsProvider.create(FakeCreds))
.build()
.getService
val batch = db.batch()
batch.set(db.document("foo/bar"), ImmutableMap.of("string", "asdf", "int", 42))
batch.set(db.document("coll/doc"), ImmutableMap.of("boolean", false, "double", 3.14))
batch.commit().get()
db.listCollections().forEach((t: CollectionReference) => println(t.getPath))
}
}
object FakeCreds extends Credentials {
final val HEADERS: util.Map[String, util.List[String]] = {
ImmutableMap.of("Authorization", ImmutableList.of("Bearer owner"))
}
override def getAuthenticationType: String = ???
override def getRequestMetadata(uri: URI): util.Map[String, util.List[String]] = {
HEADERS
}
override def hasRequestMetadata: Boolean = true
override def hasRequestMetadataOnly: Boolean = true
override def refresh(): Unit = ()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment