Workflows API 的 Cloud 客户端库使用入门

演示了如何开始使用适用于 Workflows 的 Cloud 客户端库。

深入探索

如需查看包含此代码示例的详细文档,请参阅以下内容:

代码示例

C++

在尝试此示例之前,请按照 Workflows 快速入门:使用客户端库中的 C++ 设置说明进行操作。

如需向 Workflows 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

#include "google/cloud/workflows/v1/workflows_client.h"
#include "google/cloud/location.h"
#include <iostream>

int main(int argc, char* argv[]) try {
  if (argc != 3) {
    std::cerr << "Usage: " << argv[0] << " project-id location-id\n";
    return 1;
  }

  auto const location = google::cloud::Location(argv[1], argv[2]);

  namespace workflows = ::google::cloud::workflows_v1;
  auto client =
      workflows::WorkflowsClient(workflows::MakeWorkflowsConnection());

  for (auto w : client.ListWorkflows(location.FullName())) {
    if (!w) throw std::move(w).status();
    std::cout << w->DebugString() << "\n";
  }

  return 0;
} catch (google::cloud::Status const& status) {
  std::cerr << "google::cloud::Status thrown: " << status << "\n";
  return 1;
}

Node.js

const {WorkflowsClient} = require('@google-cloud/workflows');
const client = new WorkflowsClient();

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'my-project';
// const location = 'us-central1';

async function listWorkflows(projectId, location) {
  const [workflows] = await client.listWorkflows({
    parent: client.locationPath(projectId, location),
  });
  for (const workflow of workflows) {
    console.info(`name: ${workflow.name}`);
  }
}

listWorkflows(projectId, location).catch(err => {
  console.error(err.message);
  process.exitCode = 1;
});

Node.js

import {WorkflowsClient} from '@google-cloud/workflows';
const client = new WorkflowsClient();

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'my-project';
// const location = 'us-central1';

async function listWorkflows(projectId: string, location: string) {
  const [workflows] = await client.listWorkflows({
    parent: client.locationPath(projectId, location),
  });
  for (const workflow of workflows) {
    console.info(`name: ${workflow.name}`);
  }
}

listWorkflows(projectId, location).catch((err: Error) => {
  console.error(err.message);
  process.exitCode = 1;
});

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器