Cloud Functions aufrufen

In dieser Kurzanleitung erfahren Sie, wie Sie mit der Cloud Functions API eine Funktion aufrufen, die eine Nachricht veröffentlicht, die aus den Argumenten, die die Funktion empfängt, erstellt wird.

Hinweise

Bevor Sie diese Kurzanleitung ausführen, müssen Sie oder Ihre Administratoren die folgenden Voraussetzungen erfüllt haben:

  • Achten Sie darauf, dass die Cloud Functions API in Ihrem Google Cloud-Projekt aktiviert ist.

    Zur API-Bibliothek

  • Richten Sie je nach Umgebung, in der Ihr SAP-System gehostet wird, die Authentifizierung zum Aufrufen von Cloud Functions ein. Eine Anleitung finden Sie unter Authentifizierung zum Aufrufen von Cloud Functions. Konfigurieren Sie die Clientschlüssel so:

    • Erstellen Sie für den Zugriff auf den Cloud Functions-Endpunkt einen Clientschlüssel mit dem Namen DEMO-CF.
    • Erstellen Sie zum Aufrufen der Cloud Functions-Funktion einen Clientschlüssel mit dem Namen DEMO-CF-INVOKER.
  • Schreiben Sie in der Google Cloud Console die HTTP-Funktion cf-gen2-hello-with-args der 2. Generation, die eine Nachricht mit den bereitgestellten Argumenten veröffentlicht:

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

    Informationen zum Schreiben von HTTP-Funktionen finden Sie unter Cloud Functions-Funktionen schreiben.

Programm zum Aufrufen von Cloud Functions erstellen

  1. Erstellen Sie im SAP-System ein ausführbares Programm in Ihrem benutzerdefinierten Namespace (z. B. Z oder Y) mit der Transaktion SE38.

    1. Geben Sie in der SAP-GUI den Transaktionscode SE38 ein.

    2. Geben Sie im Feld Programm einen Namen für Ihr Programm ein, z. B. ZDEMO_CLOUDFUNC_INVOKER.

    3. Klicken Sie auf Erstellen.

    4. Geben Sie die Programmattribute an:

      1. Geben Sie im Feld Titel den Titel Ihres Programms ein, z. B. Invoke Cloud Function using Cloud Function Invoker.

      2. Wählen Sie im Feld Typ die Option Executable Program aus.

      3. Klicken Sie auf Speichern.

    5. Speichern Sie das Programm als lokales Objekt.

    6. Fügen Sie im ABAP-Editor folgenden Code ein:

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

    Ersetzen Sie Folgendes:

    • DEMO_CF: Name des Clientschlüssels, der für den Zugriff auf den Cloud Functions-Endpunkt verwendet wird.
    • DEMO_CF_INVOKER: Name des Clientschlüssels, mit dem die Cloud Functions-Funktion aufgerufen wird.
  2. Führen Sie die Anwendung in SE38 aus. Wenn der Vorgang erfolgreich war, wird die folgende Ausgabe angezeigt:

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

Nächste Schritte