叫用 Cloud Run 函式

本快速入門說明如何使用 Cloud Run 函式 API 叫用函式,藉此發布從函式收到的引數所建構的訊息。

事前準備

執行本快速入門前,請確認您或管理員已完成下列先決條件:

  • 請確認您已在 Google Cloud 專案中啟用 Cloud Run 函式 API。

    前往 API 程式庫

  • 視 SAP 系統託管環境而定,設定叫用 Cloud Run 函式的驗證機制。如需操作說明,請參閱「驗證以叫用 Cloud Run functions」。請按照下列方式設定用戶端金鑰:

    • 如要存取 Cloud Run 函式端點,請建立名為 DEMO-CF 的用戶端金鑰。
    • 如要叫用 Cloud Run 函式,請建立名為 DEMO-CF-INVOKER 的用戶端金鑰。
  • 在 Google Cloud 主控台中,編寫第 2 代 HTTP 函式 cf-gen2-hello-with-args,使用提供的引數發布訊息:

        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}`);
      };
    

    如要瞭解如何編寫 HTTP 函式,請參閱「編寫 Cloud Run 函式」。

建立可叫用 Cloud Run 函式的程式

  1. 在 SAP 系統中,使用交易 SE38 在自訂命名空間 (例如 ZY) 中建立可執行的程式。

    1. 在 SAP GUI 中輸入交易代碼 SE38

    2. 在「Program」欄位中,輸入計畫名稱。例如:ZDEMO_CLOUDFUNC_INVOKER

    3. 按一下 [建立]。

    4. 指定節目屬性:

      1. 在「Title」欄位中,輸入節目名稱,例如 Invoke Cloud Function using Cloud Function Invoker

      2. 在「Type」欄位中,選擇「Executable Program」。

      3. 按一下 [儲存]

    5. 將程式儲存為本機物件

    6. ABAP 編輯器中,新增下列程式碼:

      **********************************************************************
      *  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.

    更改下列內容:

    • DEMO_CF:用於存取 Cloud Run 函式端點的用戶端金鑰名稱。
    • DEMO_CF_INVOKER:用於叫用 Cloud Run 函式的用戶端金鑰名稱。
  2. SE38 中執行應用程式。如果成功,系統會顯示下列輸出內容:

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

後續步驟