调用 Cloud Run functions

本快速入门介绍如何使用 Cloud Run functions API 调用一个函数,该函数发布根据函数收到的参数构造的消息。

准备工作

在运行本快速入门之前,请确保您或您的管理员已满足以下前提条件:

  • 确保已在您的 Google Cloud 项目中启用 Cloud Run functions API。

    转到 API 库

  • 根据 SAP 系统的托管环境,设置用于调用 Cloud Run functions 的身份验证。如需了解相关说明,请参阅进行身份验证以调用 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 functions

创建程序以调用 Cloud Run functions

  1. 在 SAP 系统中,使用事务 SE38 在自定义命名空间(例如 ZY)中创建可执行程序。

    1. 在 SAP GUI 中,输入事务代码 SE38

    2. 程序字段中,输入程序名称,例如 ZDEMO_CLOUDFUNC_INVOKER

    3. 点击创建

    4. 指定程序属性:

      1. 标题字段中,输入程序标题,例如 Invoke Cloud Function using Cloud Function Invoker

      2. 类型字段中,选择 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
    

后续步骤