验证地址

本快速入门介绍了如何创建一个程序,以通过 SAP BTP 版本的 ABAP SDK for Google Cloud,使用 Address Validation API 验证存储在 SAP 数据库中的公司地址。

准备工作

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

  • 确保已在 Google Cloud 项目中启用 Address Validation API。

    转到 API 库

创建 ABAP 类以验证地址

  1. 创建软件包:

    1. 在 ADT 中,转到 Project Explorer。
    2. 右键点击软件包 ZLOCAL,然后选择新建 > ABAP 软件包
    3. 为软件包输入以下详细信息:

      • 名称:输入 ZABAPSDK_TEST
      • 说明:输入 ABAP SDK Test Package
    4. 点击下一步

    5. 选择传输请求对话框中,选中创建新请求复选框。

    6. 输入传输请求的说明。

    7. 点击完成

  2. 创建 ABAP 类以调用 Address Validation API:

    1. 右键点击 ABAP 软件包,然后选择新建 > ABAP 类
    2. 为 ABAP 类输入以下详细信息:

      • 名称:输入 ZGOOG_CL_QS_ADDRESS_VALIDATION
      • 说明:输入 Quick start for Address Validation API
    3. 点击下一步

    4. 选择传输请求,然后点击完成

  3. 在代码编辑器中,将默认代码替换为以下代码段:

    " --------------------------------------------------------------------
    "  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.                                -
    " --------------------------------------------------------------------
    CLASS zcl_qs_validate_address DEFINITION
      PUBLIC FINAL
      CREATE PUBLIC.
    
      PUBLIC SECTION.
        INTERFACES if_oo_adt_classrun.
    ENDCLASS.
    
    CLASS zcl_qs_validate_address IMPLEMENTATION.
      METHOD if_oo_adt_classrun~main.
    
        DATA ls_input             TYPE /goog/cl_addrvaldn_v1=>ty_012.
        DATA lo_address_validator TYPE REF TO /goog/cl_addrvaldn_v1.
    
        TRY.
            " Open HTTP connection
            lo_address_validator = NEW #( iv_key_name = 'DEMO_ADDR_VAL' ).
    
            " Pass the address to be validated
            ls_input-address-region_code = 'US'.
            ls_input-address-locality    = 'Mountain View'.
            APPEND '1600, Amphitheatre, Parkway' TO ls_input-address-address_lines.
    
            " Call the API Method to validate address
            lo_address_validator->validate_address( EXPORTING is_input    = ls_input
                                                    IMPORTING es_output   = DATA(ls_output)
                                                              ev_ret_code = DATA(lv_ret_code)
                                                              ev_err_text = DATA(lv_err_text)
                                                              es_err_resp = DATA(ls_err_resp) ).
    
            IF     lo_address_validator->is_success( lv_ret_code ) = abap_true
               AND ls_output-result-verdict-address_complete       = abap_true.
              out->write( 'Address is complete' ).
            ENDIF.
    
          CATCH /goog/cx_sdk INTO DATA(lo_exception). " TODO: variable is assigned but never used (ABAP cleaner)
            " Handle exception here
        ENDTRY.
      ENDMETHOD.
    ENDCLASS.
    

    DEMO_ADDR_VAL 替换为客户端密钥名称。

  4. 保存并启用更改。

  5. 运行您的应用:

    1. 选择 ABAP 类 ZGOOG_CL_QS_ADDRESS_VALIDATION
    2. 依次点击运行 > 运行方式 > ABAP 应用(控制台)。或者,按 F9。 如果创建成功,将显示以下输出:
      'Address is complete'
      

后续步骤