Library klien Agent Platform Workbench

Halaman ini menunjukkan cara memulai Library Klien Cloud untuk Notebooks API. Library klien mempermudah aksesGoogle Cloud API dari bahasa yang didukung. Meskipun Anda dapat menggunakanGoogle Cloud API secara langsung dengan membuat permintaan mentah ke server, library klien memberikan penyederhanaan yang secara signifikan mengurangi jumlah kode yang perlu ditulis.

Baca lebih lanjut Library Klien Cloud dan Library Klien Google API versi lama di Penjelasan library klien.

Menginstal library klien

C++

Lihat GitHub README untuk mengetahui detail tentang persyaratan library klien ini dan cara menginstal dependensi.

C#

Install-Package Google.Cloud.Notebooks.V1 -Pre

Untuk mengetahui informasi selengkapnya, lihat Menyiapkan Lingkungan Pengembangan C#.

Go

go get cloud.google.com/go/notebooks

Untuk mengetahui informasi selengkapnya, lihat Menyiapkan Lingkungan Pengembangan Go.

Java

Jika Anda menggunakan Maven, tambahkan kode berikut ke file pom.xml Anda: Untuk informasi lebih lanjut tentang BOM, lihat BOM Library Google Cloud Platform.

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>libraries-bom</artifactId>
      <version>26.79.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-notebooks</artifactId>
  </dependency>
</dependencies>

Jika menggunakan Gradle, tambahkan kode berikut ke dependensi Anda:

implementation 'com.google.cloud:google-cloud-notebooks:1.89.0'

Jika Anda menggunakan sbt, tambahkan kode berikut ke dependensi Anda:

libraryDependencies += "com.google.cloud" % "google-cloud-notebooks" % "1.89.0"

Untuk mengetahui informasi selengkapnya, lihat Menyiapkan Lingkungan Pengembangan Java.

Node.js

npm install @google-cloud/notebooks

Untuk mengetahui informasi selengkapnya, lihat Menyiapkan Lingkungan Pengembangan Node.js.

PHP

composer require google/cloud

Untuk mengetahui informasi selengkapnya, lihat Menggunakan PHP di Google Cloud.

Python

Mac/Linux

pip install virtualenv
virtualenv ENVIRONMENT_NAME
source ENVIRONMENT_NAME/bin/activate
ENVIRONMENT_NAME/bin/pip install google-cloud-notebooks

Windows

pip install --upgrade google-cloud-notebooks
pip install virtualenv
virtualenv ENVIRONMENT_NAME
ENVIRONMENT_NAME\Scripts\activate
ENVIRONMENT_NAME\Scripts\pip.exe install google-cloud-notebooks

Untuk mengetahui informasi selengkapnya, lihat Menyiapkan Lingkungan Pengembangan Python.

Ruby

gem install google-cloud-notebooks

Untuk mengetahui informasi selengkapnya, lihat Menyiapkan Lingkungan Pengembangan Ruby.

Menyiapkan autentikasi

Untuk mengautentikasi panggilan ke Google Cloud API, library klien mendukung Kredensial Default Aplikasi (ADC). Library ini mencari kredensial dalam kumpulan lokasi yang ditentukan dan menggunakan kredensial tersebut untuk mengautentikasi permintaan ke API. Dengan ADC, Anda dapat menyediakan kredensial untuk aplikasi di berbagai lingkungan, seperti produksi atau pengembangan lokal, tanpa perlu mengubah kode aplikasi.

Untuk lingkungan produksi, cara menyiapkan ADC bergantung pada layanan dan konteksnya. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan Kredensial Default Aplikasi.

Untuk lingkungan pengembangan lokal, Anda dapat menyiapkan ADC dengan kredensial yang terkait dengan Akun Google Anda:

  1. Instal Google Cloud CLI. Setelah penginstalan, inisialisasi Google Cloud CLI dengan menjalankan perintah berikut:

    gcloud init

    Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.

  2. Jika Anda menggunakan shell lokal, buat kredensial autentikasi lokal untuk akun pengguna Anda:

    gcloud auth application-default login

    Anda tidak perlu melakukan langkah ini jika menggunakan Cloud Shell.

    Jika error autentikasi ditampilkan, dan Anda menggunakan penyedia identitas (IdP) eksternal, konfirmasi bahwa Anda telah login ke gcloud CLI dengan identitas gabungan Anda.

    Layar login akan muncul. Setelah Anda login, kredensial Anda akan disimpan dalam file kredensial lokal yang digunakan oleh ADC.

Menggunakan library klien

Contoh berikut menunjukkan cara menggunakan library klien dalam beberapa bahasa yang tersedia.

C++

// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! [all]
#include "google/cloud/notebooks/v2/notebook_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 notebooks = ::google::cloud::notebooks_v2;
  auto client = notebooks::NotebookServiceClient(
      notebooks::MakeNotebookServiceConnection());

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

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

Node.js

// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

async function main(projectId, location) {
  /**
   * TODO(developer): Uncomment these variables before running the sample.
   */
  // const projectId = 'my-project';
  // const location = 'global';

  // Imports the Google Cloud Some API library
  const {NotebookServiceClient} = require('@google-cloud/notebooks');
  const client = new NotebookServiceClient();
  async function listInstances() {
    const [instances] = await client.listInstances({
      parent: `projects/${projectId}/locations/${location}`,
    });
    for (const instance of instances) {
      console.info(`instance: ${instance.name}`);
    }
  }
  listInstances();
}

main(...process.argv.slice(2));
process.on('unhandledRejection', err => {
  console.error(err.message);
  process.exitCode = 1;
});

Referensi lainnya

C++

Daftar berikut berisi link ke referensi lainnya yang terkait dengan library klien untuk C++:

C#

Daftar berikut berisi link ke referensi lainnya yang terkait dengan library klien untuk C#:

Go

Daftar berikut berisi link ke referensi lainnya yang terkait dengan library klien untuk Go:

Java

Daftar berikut berisi link ke referensi lainnya yang terkait dengan library klien untuk Java:

Node.js

Daftar berikut berisi link ke referensi lainnya yang terkait dengan library klien untuk Node.js:

PHP

Daftar berikut berisi link ke referensi lainnya yang terkait dengan library klien untuk PHP:

Python

Daftar berikut berisi link ke referensi lainnya yang terkait dengan library klien untuk Python:

Ruby

Daftar berikut berisi link ke referensi lainnya yang terkait dengan library klien untuk Ruby: