Richiama Cloud Functions

Questa guida rapida illustra come richiamare una funzione che pubblica un messaggio creato a partire dagli argomenti ricevuti dalla funzione utilizzando l'API Cloud Functions.

Prima di iniziare

Prima di eseguire questa guida rapida, assicurati che tu o i tuoi amministratori abbiate completato i seguenti prerequisiti:

  • Assicurati che l'API Cloud Functions sia abilitata nel tuo progetto Google Cloud.

    Vai alla libreria API

  • A seconda dell'ambiente in cui è ospitato il sistema SAP, configura l'autenticazione per richiamare Cloud Functions. Per le istruzioni, consulta Autenticazione per richiamare Cloud Functions. Configura le chiavi client nel seguente modo:

    • Per accedere all'endpoint della Cloud Function, crea una chiave client denominata DEMO-CF.
    • Per richiamare la Cloud Function, crea una chiave client denominata DEMO-CF-INVOKER.
  • Nella console Google Cloud, scrivi una funzione HTTP di 2ª generazione, cf-gen2-hello-with-args, che pubblica un messaggio utilizzando gli argomenti forniti:

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

    Per informazioni su come scrivere le funzioni HTTP, consulta Scrivere funzioni Cloud Functions.

Crea un programma per richiamare Cloud Functions

  1. Nel sistema SAP, crea un programma eseguibile nel tuo spazio dei nomi personalizzato (ad esempio Z o Y) utilizzando la transazione SE38.

    1. Nella GUI di SAP, inserisci il codice transazione SE38.

    2. Nel campo Programma, inserisci un nome per il programma. Ad esempio, ZDEMO_CLOUDFUNC_INVOKER.

    3. Fai clic su Crea.

    4. Specifica gli attributi del programma:

      1. Nel campo Titolo, inserisci un titolo per il programma, ad esempio Invoke Cloud Function using Cloud Function Invoker.

      2. Nel campo Tipo, scegli Executable Program.

      3. Fai clic su Salva.

    5. Salva il programma come oggetto locale.

    6. Nell'Editor ABAP, aggiungi il seguente codice:

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

    Sostituisci quanto segue:

    • DEMO_CF: nome della chiave client utilizzata per accedere all'endpoint della funzione Cloud Function.
    • DEMO_CF_INVOKER: nome della chiave client utilizzata per richiamare la Cloud Function.
  2. Esegui l'applicazione in SE38. In caso di esito positivo, viene visualizzato il seguente output:

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

Passaggi successivi