-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
usage example #290
Comments
Cool!
How did you determine that the error occurs before the request is sent? I was thinking the error was originating from here, but that's the response decoder. Is authentication needed to access this API? |
Nevermind, I was misunderstanding, and it seems we get an error on receive the response 😅
It seems I pointed an wrong baseUri for firestore? I could retrieve document from the firestore emulator 👍 |
it seems i need to learn https://googleapis.github.io/HowToRPC.html 🤓
|
Here's a progress https://github.com/tanishiking/http4s-firestore now I'm receiving RESOURCE_PROJECT_INVALID 🤔 |
Okay, it worked at least on JVM 🎉 //> using scala 3
//> using dep "org.http4s::http4s-ember-client:0.23.23"
//> using dep "io.chrisdavenport::http4s-grpc-google-cloud-firestore-v1:3.15.2+0.0.6"
import cats.syntax.all._
import cats.effect._
import org.http4s._
import org.http4s.client.Client
import org.http4s.ember.client.EmberClientBuilder
import org.http4s.client.middleware.Logger
import com.google.firestore.v1.firestore.Firestore
import com.google.firestore.v1.document.Document
import com.google.firestore.v1.firestore.GetDocumentRequest
import com.google.firestore.v1.firestore.GetDocumentRequest.ConsistencySelector
import com.google.firestore.v1.firestore.CreateDocumentRequest
import com.google.firestore.v1.document.Value
import com.google.firestore.v1.document.Value.ValueTypeOneof
object Main extends IOApp:
override def run(args: List[String]): IO[ExitCode] =
val projectId: String = args(0)
val accessToken: String = args(1)
createClient().use { rawClient =>
val client = Logger[IO](
logHeaders = true,
logBody = true,
redactHeadersWhen = _ => false,
logAction = Some(msg => IO.println(msg))
)(rawClient)
val firestore = Firestore.fromClient(
client,
Uri
.fromString("https://firestore.googleapis.com")
.getOrElse(throw new RuntimeException("invalid firestore uri"))
)
val docId = System.currentTimeMillis().toString
firestore
.createDocument(
CreateDocumentRequest.of(
parent = s"projects/$projectId/databases/(default)/documents",
collectionId = "jokes",
documentId = docId,
document = Some(Document.of(
name = "",
fields = Map(
"joke" -> Value.of(
ValueTypeOneof.StringValue("joke")
)
),
createTime = None,
updateTime = None
)),
mask = None
),
Headers.of(
headers.Authorization(
Credentials.Token(AuthScheme.Bearer, accessToken)
),
headers.`Content-Type`(
new MediaType("application", "grpc")
)
)
)
.flatMap { doc =>
IO.println(doc)
} >> ExitCode.Success.pure[IO]
}
def createClient(): Resource[IO, Client[IO]] =
EmberClientBuilder
.default[IO]
.withHttp2
.build
|
@tanishiking I was looking more closely at the guide you linked. I'm a little confused, I wonder if you're actually invoking the "fallback" instead of the "true" gRPC API 🤔 https://googleapis.github.io/HowToRPC.html#grpc-fallback-experimental |
Unfortunately this also seems incorrect. If you look at the logs for the original request in #290 (comment) you can see it is already including the correct header |
Yeah, I was also thinking about it, but I'm not sure how can we force to use "true" gRPC API 🤔
Yes, that statement turned out to be incorrect. However, we still need to set |
It seems like the fallback protocol is like https://github.com/googleapis/googleapis.github.io/blob/bcb-to-fb/examples/rpc/rust/src/main.rs that RPC using normal HTTP POST let mut res = client.post("https://language.googleapis.com/$rpc/google.cloud.language.v1.LanguageService/AnalyzeEntities")
.header(ContentType(Mime::from_str("application/x-protobuf").unwrap()))
.header(XGoogApiKey(google_api_key.to_owned()))
.body(serialized_bytes)
.send().unwrap(); So, if http4s-grpc doesn't fallback to the HTTP call, we're using gRPC on the above example maybe? (I'm quite new to gRPC and not sure how can we confirm we're communicating over gRPC 🙇)
|
Well, it's complicated, because gRPC itself is defined over HTTP/2 😅 So I'm not entirely sure how the fallback is different from that. I believe the
I'm very suspicious about this
It seems strange if true gRPC is using an experimental URL format. But it would make sense for "gRPC Fallback (Experimental)" to be use an experimental URL format. Sorry, I'm mostly throwing mud at your code without answers of my own 😅 Thanks for your efforts to actually make this stuff work 🙏 We may need to study an official Google library to see how its invoking the gRPCs. |
Hm, that makes sense. Anyway, it seems we need to study from an official Google library as you mentioned 😅
No worries! I really appreciate having someone to talk to about this issue 😄 BTW, I made an another sloppy example that programmatically authenticate GCP using service account key (instead of |
Nice! I have a JVM/JS implementation of this in one of my other projects: I started working on porting this to a new googleapis-http4s-runtime library. I'm refreshing the code and adding Native support. |
Awesome! Are you planning to deprecate the |
Currently that library has a couple other modules in it that I haven't figured out what to do with yet. For example it supports BigQuery APIs that are not available over gRPC. |
Example with googleapis-runtime: //> using scala "2.13.12"
//> using repositories "sonatype-s01:snapshots"
//> using dep "dev.i10416::http4s-googleapis-runtime:0.0-20544fa-SNAPSHOT"
//> using dep "org.http4s::http4s-core:0.23.25"
//> using dep "org.http4s::http4s-ember-client:0.23.25"
//> using dep "org.typelevel::cats-effect:3.5.2"
//> using dep "io.chrisdavenport::http4s-grpc-google-cloud-firestore-v1:3.15.2+0.0.6"
import org.http4s.syntax.all._
import com.google.firestore.v1.firestore.Firestore
import com.google.firestore.v1.document.Document
import com.google.firestore.v1.firestore.GetDocumentRequest
import com.google.firestore.v1.firestore.GetDocumentRequest.ConsistencySelector
import com.google.firestore.v1.firestore.CreateDocumentRequest
import com.google.firestore.v1.document.Value
import com.google.firestore.v1.document.Value.ValueTypeOneof
import cats.effect._
import org.http4s.ember.client.EmberClientBuilder
import org.http4s.googleapis.runtime.auth.ApplicationDefaultCredentials
import com.google.firestore.v1.firestore.ListDocumentsRequest
import org.http4s.Headers
import org.http4s.headers.`Content-Type`
import org.http4s.MediaType
import org.http4s.headers.Authorization
import org.http4s.Credentials
import org.http4s.AuthScheme
object Main extends IOApp {
private val projectId: String = ??? // your GCP project id
private val parentCollection: String = ???
private val collectionId: String = ???
def run(args: List[String]): IO[ExitCode] = EmberClientBuilder
.default[IO]
.withHttp2
.build
.use { client =>
for {
oauth2 <- ApplicationDefaultCredentials(client)
tkn <- oauth2.get
f = Firestore.fromClient(client, uri"https://firestore.googleapis.com")
response <- f.listDocuments(
ListDocumentsRequest(
parent =
s"projects/$projectId/databases/(default)/documents/$parentCollection",
collectionId = collectionId,
pageSize = 10
),
Headers(
Authorization(
Credentials.Token(AuthScheme.Bearer, tkn.token)
),
`Content-Type`(
new MediaType("application", "grpc")
)
)
)
_ <- IO.println(response)
} yield ()
}
.handleErrorWith(IO.println(_))
.as(ExitCode.Success)
}
|
Hi, first of all, thank you for providing this library!
Could someone please share an example of how to use any GCP libraries?
I've been attempting to communicate with Firestore using
http4s-grpc-google-cloud-firestore-v1
, but I'm encountering an issue where it fails with ajava.util.NoSuchElementException
before sending the request to Firestore.context: I'm attempting to develop example app that consumes some GCP application on CloudRun based on ChristopherDavenport/scala-native-ember-example#7
The text was updated successfully, but these errors were encountered: