Membuat alur kerja menggunakan Python atau Ruby

Membuat alur kerja menggunakan Python atau Ruby.

Contoh kode

C#

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan C# di Panduan memulai alur kerja menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API alur kerja C#.

Untuk mengautentikasi ke Workflows, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

using Google.Api.Gax.ResourceNames;
using Google.Cloud.Workflows.V1;
using Google.LongRunning;

public sealed partial class GeneratedWorkflowsClientSnippets
{
    /// <summary>Snippet for CreateWorkflow</summary>
    /// <remarks>
    /// This snippet has been automatically generated and should be regarded as a code template only.
    /// It will require modifications to work:
    /// - It may require correct/in-range values for request initialization.
    /// - It may require specifying regional endpoints when creating the service client as shown in
    ///   https://cloud.google.com/dotnet/docs/reference/help/client-configuration#endpoint.
    /// </remarks>
    public void CreateWorkflowRequestObject()
    {
        // Create client
        WorkflowsClient workflowsClient = WorkflowsClient.Create();
        // Initialize request argument(s)
        CreateWorkflowRequest request = new CreateWorkflowRequest
        {
            ParentAsLocationName = LocationName.FromProjectLocation("[PROJECT]", "[LOCATION]"),
            Workflow = new Workflow(),
            WorkflowId = "",
        };
        // Make the request
        Operation<Workflow, OperationMetadata> response = workflowsClient.CreateWorkflow(request);

        // Poll until the returned long-running operation is complete
        Operation<Workflow, OperationMetadata> completedResponse = response.PollUntilCompleted();
        // Retrieve the operation result
        Workflow result = completedResponse.Result;

        // Or get the name of the operation
        string operationName = response.Name;
        // This name can be stored, then the long-running operation retrieved later by name
        Operation<Workflow, OperationMetadata> retrievedResponse = workflowsClient.PollOnceCreateWorkflow(operationName);
        // Check if the retrieved long-running operation has completed
        if (retrievedResponse.IsCompleted)
        {
            // If it has completed, then access the result
            Workflow retrievedResult = retrievedResponse.Result;
        }
    }
}

Go

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Go di Panduan memulai alur kerja menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API alur kerja Go.

Untuk mengautentikasi ke Workflows, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.


package main

import (
	"context"

	workflows "cloud.google.com/go/workflows/apiv1"
	workflowspb "cloud.google.com/go/workflows/apiv1/workflowspb"
)

func main() {
	ctx := context.Background()
	// This snippet has been automatically generated and should be regarded as a code template only.
	// It will require modifications to work:
	// - It may require correct/in-range values for request initialization.
	// - It may require specifying regional endpoints when creating the service client as shown in:
	//   https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options
	c, err := workflows.NewClient(ctx)
	if err != nil {
		// TODO: Handle error.
	}
	defer c.Close()

	req := &workflowspb.CreateWorkflowRequest{
		// TODO: Fill request struct fields.
		// See https://pkg.go.dev/cloud.google.com/go/workflows/apiv1/workflowspb#CreateWorkflowRequest.
	}
	op, err := c.CreateWorkflow(ctx, req)
	if err != nil {
		// TODO: Handle error.
	}

	resp, err := op.Wait(ctx)
	if err != nil {
		// TODO: Handle error.
	}
	// TODO: Use resp.
	_ = resp
}

Java

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Java di Panduan memulai alur kerja menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API alur kerja Java.

Untuk mengautentikasi ke Workflows, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

import com.google.cloud.workflows.v1.CreateWorkflowRequest;
import com.google.cloud.workflows.v1.LocationName;
import com.google.cloud.workflows.v1.Workflow;
import com.google.cloud.workflows.v1.WorkflowsClient;

public class SyncCreateWorkflow {

  public static void main(String[] args) throws Exception {
    syncCreateWorkflow();
  }

  public static void syncCreateWorkflow() throws Exception {
    // This snippet has been automatically generated and should be regarded as a code template only.
    // It will require modifications to work:
    // - It may require correct/in-range values for request initialization.
    // - It may require specifying regional endpoints when creating the service client as shown in
    // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
    try (WorkflowsClient workflowsClient = WorkflowsClient.create()) {
      CreateWorkflowRequest request =
          CreateWorkflowRequest.newBuilder()
              .setParent(LocationName.of("[PROJECT]", "[LOCATION]").toString())
              .setWorkflow(Workflow.newBuilder().build())
              .setWorkflowId("workflowId-360387270")
              .build();
      Workflow response = workflowsClient.createWorkflowAsync(request).get();
    }
  }
}

Python

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Python di Panduan memulai alur kerja menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API alur kerja Python.

Untuk mengautentikasi ke Workflows, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

# This snippet has been automatically generated and should be regarded as a
# code template only.
# It will require modifications to work:
# - It may require correct/in-range values for request initialization.
# - It may require specifying regional endpoints when creating the service
#   client as shown in:
#   https://googleapis.dev/python/google-api-core/latest/client_options.html
from google.cloud import workflows_v1

def sample_create_workflow():
    # Create a client
    client = workflows_v1.WorkflowsClient()

    # Initialize request argument(s)
    workflow = workflows_v1.Workflow()
    workflow.source_contents = "source_contents_value"

    request = workflows_v1.CreateWorkflowRequest(
        parent="parent_value",
        workflow=workflow,
        workflow_id="workflow_id_value",
    )

    # Make the request
    operation = client.create_workflow(request=request)

    print("Waiting for operation to complete...")

    response = operation.result()

    # Handle the response
    print(response)

Ruby

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Ruby di Panduan memulai alur kerja menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API alur kerja Ruby.

Untuk mengautentikasi ke Workflows, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

require "google/cloud/workflows/v1"

##
# Snippet for the create_workflow call in the Workflows service
#
# This snippet has been automatically generated and should be regarded as a code
# template only. It will require modifications to work:
# - It may require correct/in-range values for request initialization.
# - It may require specifying regional endpoints when creating the service
# client as shown in https://cloud.google.com/ruby/docs/reference.
#
# This is an auto-generated example demonstrating basic usage of
# Google::Cloud::Workflows::V1::Workflows::Client#create_workflow.
#
def create_workflow
  # Create a client object. The client can be reused for multiple calls.
  client = Google::Cloud::Workflows::V1::Workflows::Client.new

  # Create a request. To set request fields, pass in keyword arguments.
  request = Google::Cloud::Workflows::V1::CreateWorkflowRequest.new

  # Call the create_workflow method.
  result = client.create_workflow request

  # The returned object is of type Gapic::Operation. You can use it to
  # check the status of an operation, cancel it, or wait for results.
  # Here is how to wait for a response.
  result.wait_until_done! timeout: 60
  if result.response?
    p result.response
  else
    puts "No response received."
  end
end

Langkah selanjutnya

Untuk menelusuri dan memfilter contoh kode untuk produk Google Cloud lainnya, lihat browser contoh Google Cloud.