TheRajVJain
TheRajVJain

Reputation: 399

GetOperation in google API

I am using dialogflow python package to communicate to my dialogflow agent (v2 version). I call the train_agent function and get the google.api_core.operation.Operation object. I store the name attribute of the operation in DB for later use. How do I get the new status of the same operation?

I tried using from google.longrunning import GetOperation but it says ImportError: cannot import name GetOperation. And the docs say Any API service that returns long-running operations should implement the Operations interface so developers can have a consistent client experience.

Should I implement the interface or something?

Upvotes: 0

Views: 1289

Answers (2)

TheRajVJain
TheRajVJain

Reputation: 399

So I ended up using AgentsClient() object to get access to an operations client.

The final code:

agent_client = dialogflow_v2.AgentsClient()
operations_obj = agent_client.operations_client.get_operations(operations_full_path)
status = operations_obj.done

And other fields can be used as mentioned in the documentation.

Upvotes: 1

gmolau
gmolau

Reputation: 3005

It seems like your are confusing the Google Cloud Python client libraries with the underlying Protobufs and gRPCs of the individual services. GetOperation is a rpc method of the Operation service, defined here. It takes a GetOperationRequest message as input and returns an Operation message. Protobuf messages are meant to be compiled to native classes in various programming languages, in Python they actually end up being descriptors. The descriptors for all messages related to long running operations are shipped with the google-api-core library, which in turn is shipped with every Google Cloud Python client library (e.g. dialogflow). This is why you could technically use the protobuf descriptors directly by importing them from the client library:

from google.longrunning.operations_pb2 import Operation

You would then only have to generate gRPC client and server stubs and you are ready to go:)

In real life you should of course ignore all of this (including the GetOperation method and the Operations interface) and instead use the Google Cloud client libraries, of which there is one for each Google Cloud service in each of the major programming languages and which all come with core modules for common tasks like authentication, billing and long-running operations management. These libraries abstract the Protobuf/gRPC stuff for end users.

In your case you would want to have a look at the google.api_core.operations_v1.OperationsClient, which you can use to manage instances of google.api_core.operation.Operation.

A different question is however if you really need this at all. I have never seen training a Dialogflow agent take more then a few seconds, and once the agent is set up it is also not something that has to be done over and over again. Do you really have to manage this operation manually and even store metadata about it in an external database?

Upvotes: 1

Related Questions