Obtenir le domaine

Affichez les détails d'un domaine de Game Servers.

Pages de documentation incluant cet exemple de code

Pour afficher l'exemple de code utilisé en contexte, consultez la documentation suivante :

Exemple de code

Go

Pour savoir comment installer et utiliser la bibliothèque cliente pour Game Servers, consultez la section Bibliothèques clientes Game Servers.


import (
	"context"
	"fmt"
	"io"

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

// getRealm retrieves info on a realm.
func getRealm(w io.Writer, projectID, location, realmID string) error {
	// projectID := "my-project"
	// location := "global"
	// realmID := "myrealm"
	ctx := context.Background()
	client, err := gaming.NewRealmsClient(ctx)
	if err != nil {
		return fmt.Errorf("NewRealmsClient: %v", err)
	}
	defer client.Close()

	req := &gamingpb.GetRealmRequest{
		Name: fmt.Sprintf("projects/%s/locations/%s/realms/%s", projectID, location, realmID),
	}

	resp, err := client.GetRealm(ctx, req)
	if err != nil {
		return fmt.Errorf("GetRealm: %v", err)
	}

	fmt.Fprintf(w, "Realm retrieved: %v", resp.Name)
	return nil
}

Java

Pour savoir comment installer et utiliser la bibliothèque cliente pour Game Servers, consultez la section Bibliothèques clientes Game Servers.


import com.google.cloud.gaming.v1.Realm;
import com.google.cloud.gaming.v1.RealmsServiceClient;
import java.io.IOException;

public class GetRealm {
  public static void getRealm(String projectId, String regionId, String realmId)
      throws IOException {
    // String projectId = "your-project-id";
    // String regionId = "us-central1-f";
    // String realmId = "your-realm-id";
    // 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 realmName =
          String.format("projects/%s/locations/%s/realms/%s", projectId, regionId, realmId);

      Realm allocationPolicy = client.getRealm(realmName);

      System.out.println("Realm found: " + allocationPolicy.getName());
    }
  }
}

Node.js

Pour savoir comment installer et utiliser la bibliothèque cliente pour Game Servers, consultez la section Bibliothèques clientes Game Servers.

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

const client = new RealmsServiceClient();

async function getRealm() {
  /**
   * 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 realmId = 'Unique identifier of the realm';
  const request = {
    // Realm name is the full resource name including project ID and location
    name: client.realmPath(projectId, location, realmId),
  };

  const [realm] = await client.getRealm(request);
  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()}`);
}

getRealm();

Python

Pour savoir comment installer et utiliser la bibliothèque cliente pour Game Servers, consultez la section Bibliothèques clientes Game Servers.

def get_realm(project_id, location, realm_id):
    """Gets a realm."""

    client = gaming.RealmsServiceClient()

    request = realms.GetRealmRequest(
        name=f"projects/{project_id}/locations/{location}/realms/{realm_id}",
    )

    response = client.get_realm(request)
    print(f"Get realm response:\n{response}")
    return response

Étape suivante

Pour rechercher et filtrer des exemples de code pour d'autres produits Google Cloud, consultez l'exemple de navigateur Google Cloud.