Listar realms

Liste os Realms para Game Servers.

Páginas de documentação que incluem esta amostra de código

Para visualizar o exemplo de código usado em contexto, consulte a seguinte documentação:

Exemplo de código

Go

Para saber como instalar e usar a biblioteca de cliente para Game Servers, consulte Bibliotecas de cliente do Game Servers.


import (
	"context"
	"fmt"
	"io"

	gaming "cloud.google.com/go/gaming/apiv1"
	"google.golang.org/api/iterator"
	gamingpb "google.golang.org/genproto/googleapis/cloud/gaming/v1"
)

// listRealms lists the realms in a location.
func listRealms(w io.Writer, projectID, location string) error {
	// projectID := "my-project"
	// location := "global"
	ctx := context.Background()
	client, err := gaming.NewRealmsClient(ctx)
	if err != nil {
		return fmt.Errorf("NewRealmsClient: %v", err)
	}
	defer client.Close()

	req := &gamingpb.ListRealmsRequest{
		Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, location),
	}

	it := client.ListRealms(ctx, req)
	for {
		resp, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("Next: %v", err)
		}

		fmt.Fprintf(w, "Realm listed: %v\n", resp.Name)
	}

	return nil
}

Java

Para saber como instalar e usar a biblioteca de cliente para Game Servers, consulte Bibliotecas de cliente do Game Servers.


import com.google.cloud.gaming.v1.ListRealmsRequest;
import com.google.cloud.gaming.v1.Realm;
import com.google.cloud.gaming.v1.RealmsServiceClient;
import com.google.cloud.gaming.v1.RealmsServiceClient.ListRealmsPagedResponse;
import com.google.common.base.Strings;
import java.io.IOException;

public class ListRealms {
  public static void listRealms(String projectId, String regionId) throws IOException {
    // String projectId = "your-project-id";
    // String regionId = "us-central1-f";
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (RealmsServiceClient client = RealmsServiceClient.create()) {
      String parent = String.format("projects/%s/locations/%s", projectId, regionId);

      ListRealmsPagedResponse response = client.listRealms(parent);
      for (Realm realm : response.iterateAll()) {
        System.out.println("Realm found: " + realm.getName());
      }

      while (!Strings.isNullOrEmpty(response.getNextPageToken())) {
        ListRealmsRequest request =
            ListRealmsRequest.newBuilder()
                .setParent(parent)
                .setPageToken(response.getNextPageToken())
                .build();
        response = client.listRealms(request);
        for (Realm realm : response.iterateAll()) {
          System.out.println("Realm found: " + realm.getName());
        }
      }
    }
  }
}

Node.js

Para saber como instalar e usar a biblioteca de cliente para Game Servers, consulte Bibliotecas de cliente do Game Servers.

const {RealmsServiceClient} = require('@google-cloud/game-servers');

const client = new RealmsServiceClient();

async function listRealms() {
  /**
   * TODO(developer): Uncomment these variables before running the sample.
   */
  // const projectId = 'Your Google Cloud Project ID';
  // const location = 'A Compute Engine region, e.g. "us-central1"';
  const request = {
    parent: `projects/${projectId}/locations/${location}`,
  };

  const [results] = await client.listRealms(request);
  for (const realm of results) {
    console.log(`Realm name: ${realm.name}`);
    console.log(`Realm description: ${realm.description}`);
    console.log(`Realm time zone: ${realm.timeZone}`);

    const createTime = realm.createTime;
    const createDate = new Date(createTime.seconds * 1000);
    console.log(`Realm created on: ${createDate.toLocaleDateString()}\n`);
  }
}

listRealms();

Python

Para saber como instalar e usar a biblioteca de cliente para Game Servers, consulte Bibliotecas de cliente do Game Servers.



def list_realms(project_id, location):
    """Lists the existing realms."""

    client = gaming.RealmsServiceClient()

    response = client.list_realms(
        parent=f"projects/{project_id}/locations/{location}"
    )

    for realm in response.realms:
        print(f"Name: {realm.name}")

    return response.realms

A seguir

Para pesquisar e filtrar amostras de código de outros produtos do Google Cloud, consulte Navegador de amostra do Google Cloud.