Cloud Functions を呼び出す

このクイックスタートでは、Cloud Functions API を使用して、関数が受け取った引数から構築されたメッセージを公開する関数を呼び出す方法について説明します。

始める前に

このクイックスタートを実行する前に、自身または管理者によって次の事前準備が完了していることを確認してください。

  • Google Cloud プロジェクトで Cloud Functions API が有効になっていることを確認します。

    API ライブラリに移動

  • SAP システムがホストされている環境に応じて、Cloud Functions の呼び出しの認証を設定します。手順については、Cloud Functions を呼び出すための認証をご覧ください。クライアント キーを次のように構成します。

    • Cloud Functions 関数のエンドポイントにアクセスするには、DEMO-CF という名前のクライアント キーを作成します。
    • Cloud Functions の関数を呼び出すには、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 Functions の関数を作成するをご覧ください。

Cloud Functions の関数を呼び出すプログラムを作成する

  1. SAP システムで、トランザクション SE38 を使用して、カスタム名前空間(ZY など)に実行可能プログラムを作成します。

    1. SAP GUI で、トランザクション コード SE38 を入力します。

    2. [プログラム] フィールドに、プログラムの名前を入力します(例: ZDEMO_CLOUDFUNC_INVOKER)。

    3. [作成] をクリックします。

    4. プログラムの属性を指定します。

      1. [Title] フィールドに、プログラムのタイトル(例: Invoke Cloud Function using Cloud Function Invoker)を入力します。

      2. [Type] フィールドで Executable Program を選択します。

      3. [Save] をクリックします。

    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 Functions 関数エンドポイントへのアクセスに使用されるクライアント キーの名前。
    • DEMO_CF_INVOKER: Cloud Functions の関数を呼び出すために使用されるクライアント キーの名前。
  2. SE38 でアプリを実行します。成功すると、次の出力が表示されます。

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

次のステップ