長時間実行オペレーション

Dialogflow API の一部のメソッドは長時間実行オペレーションを返します。これらのメソッドは非同期で、メソッドがレスポンスを返すときにオペレーションが完了していない場合があります。オペレーションのステータスを確認し、完了するまで待機する、またはオペレーションをキャンセルできます。

オペレーションが完了するまで待機する

オペレーションが完了するまで待機する方法は、次のとおりです。

REST

オペレーションのステータスをポーリングするには、Operations リソースの get メソッドを呼び出します。オペレーションが完了すると、done フィールドが true に設定されます。

リクエストのデータを使用する前に、次のように置き換えます。

  • REGION_ID: リージョン ID
  • PROJECT_ID: 実際の Google Cloud プロジェクト ID
  • OPERATION_ID: オペレーション ID

HTTP メソッドと URL:

GET https://REGION_ID-dialogflow.googleapis.com/v3/projects/PROJECT_ID/locations/REGION_ID/operations/OPERATION_ID

リクエストを送信するには、次のいずれかのオプションを展開します。

次のような JSON レスポンスが返されます。

{
  "name": "projects/PROJECT_ID/locations/REGION_ID/operations/OPERATION_ID",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.dialogflow.v3.SomeOperationType",
    "state": "DONE"
  },
  "done": true,
  ...
}

Java

Dialogflow への認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。


import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.dialogflow.cx.v3.AgentName;
import com.google.cloud.dialogflow.cx.v3.AgentsClient;
import com.google.cloud.dialogflow.cx.v3.AgentsSettings;
import com.google.cloud.dialogflow.cx.v3.ExportAgentRequest;
import com.google.cloud.dialogflow.cx.v3.ExportAgentResponse;
import com.google.protobuf.Struct;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class ExportAgent {

  public static void main(String[] args)
      throws IOException, InterruptedException, ExecutionException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "my-project-id";
    String agentId = "my-agent-id";
    String location = "my-location";

    exportAgent(projectId, agentId, location);
  }

  public static void exportAgent(String projectId, String agentId, String location)
      throws IOException, InterruptedException, ExecutionException {

    // Sets the api endpoint to specified location
    String apiEndpoint = String.format("%s-dialogflow.googleapis.com:443", location);

    AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build();
    // Note: close() needs to be called on the AgentsClient object to clean up resources
    // such as threads. In the example below, try-with-resources is used,
    // which automatically calls close().
    try (AgentsClient agentsClient = AgentsClient.create(agentsSettings)) {
      ExportAgentRequest request =
          ExportAgentRequest.newBuilder()
              .setName(AgentName.of(projectId, location, agentId).toString())
              .build();

      // Returns a future of the operation
      OperationFuture<ExportAgentResponse, Struct> future =
          agentsClient.exportAgentOperationCallable().futureCall(request);

      // get the export agent response after the operation is completed
      ExportAgentResponse response = future.get();
      System.out.println(response);
    }
  }
}

Node.js

Dialogflow への認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。


const {AgentsClient, protos} = require('@google-cloud/dialogflow-cx');

const api_endpoint = `${location}-dialogflow.googleapis.com`;

const client = new AgentsClient({apiEndpoint: api_endpoint});

const exportAgentRequest =
  new protos.google.cloud.dialogflow.cx.v3.ExportAgentRequest();

exportAgentRequest.name = `projects/${projectId}/locations/${location}/agents/${agentId}`;

// exportAgent call returns a promise to a long running operation
const [operation] = await client.exportAgent(exportAgentRequest);

// Waiting for the long running opporation to finish
const [response] = await operation.promise();

// Prints the result of the operation when the operation is done
console.log(response);

Python

Dialogflow への認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。

from google.cloud.dialogflowcx_v3.services.agents.client import AgentsClient
from google.cloud.dialogflowcx_v3.types.agent import ExportAgentRequest

def export_long_running_agent(project_id, agent_id, location):
    api_endpoint = f"{location}-dialogflow.googleapis.com:443"
    client_options = {"api_endpoint": api_endpoint}

    agents_client = AgentsClient(client_options=client_options)

    export_request = ExportAgentRequest()

    export_request.name = (
        f"projects/{project_id}/locations/{location}/agents/{agent_id}"
    )

    # export_agent returns a long running operation
    operation = agents_client.export_agent(request=export_request)

    # Returns the result of the operation when the operation is done
    return operation.result()