GCP.jl icon indicating copy to clipboard operation
GCP.jl copied to clipboard

Building APIs with ProtoBuf definitions

Open trickster opened this issue 5 years ago • 2 comments

I am experimenting with API generation with .proto definitions like this. Would you be interested in experimenting with these.

https://github.com/googleapis/googleapis.github.io/blob/master/examples/rpc/swift/SETUP.sh

I used ProtoBuf.jl to generate Client Stubs. Julia example below.

using HTTP

include("language_service_pb.jl")


# construct data
doc = Document()
doc._type = Document_Type.PLAIN_TEXT
doc.language = "en"

ent = AnalyzeEntitiesRequest()
ent.document = doc


iob = IOBuffer()
writeproto(iob, ent)
write_proto_bytes = take!(iob)

headers = Dict(
    "X-Goog-Api-Key" => "**********",
    "User-Agent" => "myapp/0.1",
    "Content-Type" => "application/x-protobuf",
)

 # we get the bytes
data_response_bytes = HTTP.request(
    "POST",
    "https://language.googleapis.com/\$rpc/google.cloud.language.v1.LanguageService/AnalyzeEntities",
    headers,
    write_proto_bytes,
).body
entreponse = AnalyzeEntitiesResponse()



# create into IOBuffer from ByteArray
response_buffer = IOBuffer(data_response_bytes)
readproto(IOBuffer(data_response_bytes), entreponse)

"""
entreponse
AnalyzeEntitiesResponse(Entity[Entity("rain", 7, #undef, 0.6791929f0, EntityMention[EntityMention(TextSpan("rain", -1), 2, #undef)], #undef), Entity("plain", 7, #undef, 0.17278467f0, EntityMention[EntityMention(TextSpan("plain", -1), 2, #undef)], #undef), Entity("Spain", 2, #undef, 0.14802246f0, EntityMention[EntityMention(TextSpan("Spain", -1), 1, #undef)], #undef)], "en")
"""

trickster avatar Jan 28 '20 05:01 trickster

GCP communication over grpc would be preferable and more performant than REST. At the time of writing, most GCP apis seemed to only support REST. Was not aware of the example you provided. Really great example.

At the moment I don't have the time to pursue a grpc approach. If time permits I would be interested in seeing if an all grpc is possible. Thank you for the question and example code.

rana avatar Feb 20 '20 07:02 rana

There are two ways to do this. Google APIs support GRPC/HTTP2 and GRPC/HTTP1

  • . It means that we can use https://github.com/googleapis/googleapis.github.io/blob/master/examples/rpc/swift/SETUP.sh approach to create gRPC client stubs for every API in this list https://github.com/googleapis/googleapis/tree/master/google. Use these with HTTP.jl for data transport
  • We can also use gRPC.jl instead of HTTP.jl

My example repo https://github.com/sivakon/grpc-julia-gcp-example

For example, They are releasing bigquery storage API which is faster way of transferring data. Have a look these https://cloud.google.com/bigquery/docs/reference/storage https://cloud.google.com/bigquery/docs/reference/storage/rpc

All the proto defintions are available at https://github.com/googleapis/googleapis/tree/master/google/cloud/bigquery/storage

Unfortunately I can only find BigQuery Storage gRPC and not BigQuery gRPC. But you are correct, the discovery APIs are most comprehensive as of now.

trickster avatar Feb 20 '20 07:02 trickster