Memanggil Cloud Functions

Panduan memulai ini menunjukkan cara memanggil fungsi yang memublikasikan pesan yang dibuat dari argumen yang diterima fungsi tersebut, menggunakan Cloud Functions API.

Sebelum memulai

Sebelum menjalankan panduan memulai ini, pastikan Anda atau administrator Anda telah memenuhi prasyarat berikut:

  • Pastikan Cloud Functions API diaktifkan di project Google Cloud Anda.

    Buka library API

  • Bergantung pada lingkungan tempat sistem SAP Anda dihosting, siapkan autentikasi untuk mengaktifkan Cloud Functions. Untuk mengetahui petunjuknya, lihat Autentikasi untuk memanggil Cloud Functions. Konfigurasikan kunci klien sebagai berikut:

    • Untuk mengakses endpoint Cloud Function, buat kunci klien dengan nama DEMO-CF.
    • Untuk memanggil Cloud Function, buat kunci klien dengan nama DEMO-CF-INVOKER.
  • Di Konsol Google Cloud, tulis fungsi HTTP generasi ke-2, cf-gen2-hello-with-args, yang memublikasikan pesan menggunakan argumen yang diberikan:

        exports.helloWorld = (req, res) => {
        let name = req.body.name || req.query.name;
        let full_name = `${req.body.firstname} ${req.body.lastname}`;
        res.status(200).send(`Hello ${name}! Full Name: ${full_name}`);
      };
    

    Untuk mengetahui informasi tentang cara menulis fungsi HTTP, lihat Menulis Cloud Functions.

Membuat program untuk memanggil Cloud Functions

  1. Dalam sistem SAP, buat program yang dapat dieksekusi di namespace kustom Anda (misalnya, Z atau Y) menggunakan transaksi SE38.

    1. Di SAP GUI, masukkan kode transaksi SE38.

    2. Di kolom Program, masukkan nama program Anda. Contoh, ZDEMO_CLOUDFUNC_INVOKER.

    3. Klik Create.

    4. Tentukan atribut program:

      1. Di kolom Title, masukkan judul program Anda, misalnya, Invoke Cloud Function using Cloud Function Invoker.

      2. Di kolom Type, pilih Executable Program.

      3. Klik Simpan.

    5. Simpan program sebagai Local Object.

    6. Di ABAP Editor, tambahkan kode berikut:

      **********************************************************************
      *  Copyright 2024 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.                                *
      **********************************************************************
      REPORT zr_qs_cfinvoker.
      
      DATA(lv_cf_name)  = CONV string( 'cf-gen2-hello-with-args' ).
      DATA(lv_msg)      = CONV string( '{"firstname": "John", "lastname" : "Doe"}' ).
      
      
      TRY.
          " Create a Client API stub for Cloud Functions
          DATA(lo_cloudfunc_client) = NEW /goog/cl_cloudfunc_v2( iv_key_name   = 'DEMO_CF' ).
          " Create a Client API stub for Cloud Function Invoker.
          " Internally this uses the Cloud Function instance to fetch the cloud function HTTP endpoint
          DATA(lo_cfinvoker_client) = NEW /goog/cl_cloudfunc_invoker( iv_key_name   = 'DEMO_CF_INVOKER' ).
      
          " Send additional query parameters as inputs to the cloud function.
          lo_cfinvoker_client->add_common_qparam( iv_name  = 'name'
                                                  iv_value = 'Johnny' ).
      
          lo_cfinvoker_client->invoke(
            EXPORTING
              iv_cf_name      = lv_cf_name            "Cloud Function Name
              iv_cf_location  = 'us-central1'         "Location where the Cloud Function is hosted
              io_cf_instance  = lo_cloudfunc_client   "Instance of cloud Function Client API Stub
              iv_body         = lv_msg                "Input payload to the Cloud Function
              iv_content_type = 'application/json'
              iv_method       = 'POST'
            IMPORTING
              es_output       = DATA(lv_output)
              ev_ret_code     = DATA(lv_ret_code)
              ev_err_text     = DATA(lv_err_text)
              es_err_resp     = DATA(ls_err_resp)
          ).
      
          IF lo_cfinvoker_client->is_success( iv_code = lv_ret_code ).
            WRITE: / 'HTTP Return Code:', lv_ret_code.
            WRITE: / 'Response:', lv_output. "Output of cloud function
          ELSE.
            WRITE: / 'HTTP Return Code:', lv_ret_code.
            WRITE: / 'Error:', lv_err_text.
          ENDIF.
      
        CATCH /goog/cx_sdk INTO DATA(lo_exp).
          WRITE: / lo_exp->get_text( ).
      ENDTRY.

    Ganti kode berikut:

    • DEMO_CF: nama kunci klien yang digunakan untuk mengakses endpoint Cloud Function.
    • DEMO_CF_INVOKER: nama kunci klien yang digunakan untuk memanggil Cloud Function.
  2. Jalankan aplikasi Anda di SE38. Jika berhasil, output berikut akan ditampilkan:

     HTTP Return Code:        200
     Response: Hello Johnny! Full Name: John Doe
    

Langkah selanjutnya