SAP On-premise

Generate PDF from HTML

FUNCTION zgenerate_pdf.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(IM_RFCDEST) TYPE  RFCDEST
*"     VALUE(IM_API_KEY) TYPE  STRING
*"     VALUE(IM_HTML) TYPE  STRING
*"     VALUE(IM_ENCRYPTION_KEY) TYPE  STRING OPTIONAL
*"     VALUE(IM_LANDSCAPE) TYPE  STRING OPTIONAL
*"     VALUE(IM_FORMAT) TYPE  STRING OPTIONAL
*"     VALUE(IM_MARGIN_TOP) TYPE  STRING OPTIONAL
*"     VALUE(IM_MARGIN_RIGHT) TYPE  STRING OPTIONAL
*"     VALUE(IM_MARGIN_BOTTOM) TYPE  STRING OPTIONAL
*"     VALUE(IM_MARGIN_LEFT) TYPE  STRING OPTIONAL
*"  EXPORTING
*"     VALUE(EX_SUBRC) TYPE  SYSUBRC
*"     VALUE(EX_MESS) TYPE  STRING
*"     VALUE(EX_PDF) TYPE  XSTRING
*"----------------------------------------------------------------------
*
  DATA: lo_http_client TYPE REF TO if_http_client,
        lv_http_rc     TYPE i,
        lv_response    TYPE string,
        lv_json        TYPE /ui2/cl_json=>json,
        lr_data        TYPE REF TO data.
*
  FIELD-SYMBOLS: <data>  TYPE data,
                 <field> TYPE any.
*
* Setup IF_HTTP_Client object
*
  CALL METHOD cl_http_client=>create_by_destination
    EXPORTING
      destination        = im_rfcdest
    IMPORTING
      client             = lo_http_client
    EXCEPTIONS
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      OTHERS             = 4.
*
  IF sy-subrc IS NOT INITIAL.
    ex_mess = 'Error creating new IF_HTTP_Client object. Process aborted.'.
    ex_subrc = 4.
    RETURN.
  ENDIF.
*
* Set protocol version
*
  lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_1 ).
*
* Set request header fields
*
  lo_http_client->request->set_header_field(
      name  = 'Accept'
      value = 'application/pdf' ).
*
  lo_http_client->request->set_header_field(
      name  = 'Content-Type'
      value = 'application/x-www-form-urlencoded' ).
*
  lo_http_client->request->set_header_field(
      name  = 'x-api-key'
      value = im_api_key ).
*
* Set form fields/attributes
*
  lo_http_client->request->if_http_entity~set_form_field(
    name   = 'inputPayload'
    value  = im_html ).
*
  IF im_encryption_key IS NOT INITIAL.
*
    lo_http_client->request->if_http_entity~set_form_field(
      name   = 'encryptionKey'
      value  = im_encryption_key ).
*
  ENDIF.
*
  IF im_landscape IS NOT INITIAL.
*
    lo_http_client->request->if_http_entity~set_form_field(
      name   = 'landscape'
      value  = im_landscape ).
*
  ENDIF.
*
  IF im_format IS NOT INITIAL.
*
    lo_http_client->request->if_http_entity~set_form_field(
      name   = 'format'
      value  = im_format ).
*
  ENDIF.
*
  IF im_margin_top IS NOT INITIAL.
*
    lo_http_client->request->if_http_entity~set_form_field(
      name   = 'marginTop'
      value  = im_margin_top ).
*
  ENDIF.
*
  IF im_margin_right IS NOT INITIAL.
*
    lo_http_client->request->if_http_entity~set_form_field(
      name   = 'marginRight'
      value  = im_margin_right ).
*
  ENDIF.
*
  IF im_margin_bottom IS NOT INITIAL.
*
    lo_http_client->request->if_http_entity~set_form_field(
      name   = 'marginBottom'
      value  = im_margin_bottom ).
*
  ENDIF.
*
  IF im_margin_left IS NOT INITIAL.
*
    lo_http_client->request->if_http_entity~set_form_field(
      name   = 'marginLeft'
      value  = im_margin_left ).
*
  ENDIF.
*
  lo_http_client->request->set_method( 'POST' ).
*
  lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.
*
* Send request
*
  CALL METHOD lo_http_client->send
    EXPORTING
      timeout                    = 60   " 15 Seconds
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2.
*
  IF sy-subrc IS NOT INITIAL.
*
    ex_mess = 'HTTP Post error. Process aborted'.
    ex_subrc = 4.
*
    CALL METHOD lo_http_client->close
      EXCEPTIONS
        http_invalid_state = 1
        OTHERS             = 2.
*
    FREE lo_http_client.
    RETURN.
*
  ENDIF.
*
* Read the Response
*
  CALL METHOD lo_http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.
*
  lo_http_client->response->get_status( IMPORTING code   = lv_http_rc ).
  lo_http_client->response->get_status( IMPORTING reason = ex_mess ).
  lv_response = lo_http_client->response->get_cdata( ).
*
  IF lv_http_rc NE '200'.
*
* Get error message back from Renda
*
    ex_subrc = 4.
    lv_json = lv_response.
    lr_data = /ui2/cl_json=>generate( json = lv_json ).
*
    IF lr_data IS BOUND.
      ASSIGN lr_data->* TO <data>.
      ASSIGN COMPONENT 'MESSAGE' OF STRUCTURE <data> TO <field>.
      IF <field> IS ASSIGNED.
        lr_data = <field>.
        ASSIGN lr_data->* TO <data>.
        ex_mess = <data>.
      ENDIF.
    ENDIF.
*
  ELSE.
*
    ex_pdf = lo_http_client->response->get_data( ).
*
  ENDIF.
*
  CALL METHOD lo_http_client->close
    EXCEPTIONS
      http_invalid_state = 1
      OTHERS             = 2.
*
  FREE lo_http_client.
*
ENDFUNCTION.

Generate QR Code

FUNCTION zgenerate_qr_code.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(IM_RFCDEST) TYPE  RFCDEST
*"     VALUE(IM_API_KEY) TYPE  STRING
*"     VALUE(IM_DATA) TYPE  STRING
*"  EXPORTING
*"     VALUE(EX_SUBRC) TYPE  SYSUBRC
*"     VALUE(EX_MESS) TYPE  STRING
*"     VALUE(EX_QR_CODE) TYPE  XSTRING
*"----------------------------------------------------------------------
*
  DATA: lo_http_client TYPE REF TO if_http_client,
        lv_http_rc     TYPE i,
        lv_response    TYPE string,
        lv_json        TYPE /ui2/cl_json=>json,
        lr_data        TYPE REF TO data.
*
  FIELD-SYMBOLS: <data>  TYPE data,
                 <field> TYPE any.
*
* Setup IF_HTTP_Client object
*
  CALL METHOD cl_http_client=>create_by_destination
    EXPORTING
      destination        = im_rfcdest
    IMPORTING
      client             = lo_http_client
    EXCEPTIONS
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      OTHERS             = 4.
*
  IF sy-subrc IS NOT INITIAL.
    ex_mess = 'Error creating new IF_HTTP_Client object. Process aborted.'.
    ex_subrc = 4.
    RETURN.
  ENDIF.
*
* Set protocol version
*
  lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_1 ).
*
* Set request header fields
*
  lo_http_client->request->set_header_field(
      name  = 'Accept'
      value = '*/*' ).
*
  lo_http_client->request->set_header_field(
      name  = 'Content-Type'
      value = 'application/x-www-form-urlencoded' ).
*
  lo_http_client->request->set_header_field(
      name  = 'x-api-key'
      value = im_api_key ).
*
* Set form fields/attributes
*
  lo_http_client->request->if_http_entity~set_form_field(
    name   = 'data'
    value  = im_data ).
*
  lo_http_client->request->set_method( 'POST' ).
*
  lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.
*
* Send request
*
  CALL METHOD lo_http_client->send
    EXPORTING
      timeout                    = 60   " 15 Seconds
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2.
*
  IF sy-subrc IS NOT INITIAL.
*
    ex_mess = 'HTTP Post error. Process aborted'.
    ex_subrc = 4.
*
    CALL METHOD lo_http_client->close
      EXCEPTIONS
        http_invalid_state = 1
        OTHERS             = 2.
*
    FREE lo_http_client.
    RETURN.
*
  ENDIF.
*
* Read the Response
*
  CALL METHOD lo_http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.
*
  lo_http_client->response->get_status( IMPORTING code   = lv_http_rc ).
  lo_http_client->response->get_status( IMPORTING reason = ex_mess ).
  lv_response = lo_http_client->response->get_cdata( ).
*
  IF lv_http_rc NE '200'.
*
* Get error message back from Renda
*
    ex_subrc = 4.
    lv_json = lv_response.
    lr_data = /ui2/cl_json=>generate( json = lv_json ).
*
    IF lr_data IS BOUND.
      ASSIGN lr_data->* TO <data>.
      ASSIGN COMPONENT 'MESSAGE' OF STRUCTURE <data> TO <field>.
      IF <field> IS ASSIGNED.
        lr_data = <field>.
        ASSIGN lr_data->* TO <data>.
        ex_mess = <data>.
      ENDIF.
    ENDIF.
*
  ELSE.
*
    ex_qr_code = lo_http_client->response->get_data( ).
*
  ENDIF.
*
  CALL METHOD lo_http_client->close
    EXCEPTIONS
      http_invalid_state = 1
      OTHERS             = 2.
*
  FREE lo_http_client.
*
ENDFUNCTION.

Onboard user for TOTP

FUNCTION zonboard_user.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(IM_RFCDEST) TYPE  RFCDEST
*"     VALUE(IM_API_KEY) TYPE  STRING
*"     VALUE(IM_USER_ID) TYPE  STRING
*"     VALUE(IM_APPLICATION_ID) TYPE  STRING
*"     VALUE(IM_TOKEN_VALIDITY) TYPE  STRING OPTIONAL
*"     VALUE(IM_TOKEN_LENGTH) TYPE  STRING OPTIONAL
*"  EXPORTING
*"     VALUE(EX_SECRET_KEY) TYPE  STRING
*"     VALUE(EX_OTP_URL) TYPE  STRING
*"     VALUE(EX_QR_CODE) TYPE  STRING
*"     VALUE(EX_TOKEN) TYPE  STRING
*"     VALUE(EX_MESS) TYPE  STRING
*"     VALUE(EX_SUBRC) TYPE  SYSUBRC
*"----------------------------------------------------------------------
  TYPES: BEGIN OF ltyp_response,
           secretkey TYPE string,
           otpurl    TYPE string,
           qrcode    TYPE string,
           token     TYPE string,
         END OF ltyp_response.

  DATA: lo_http_client TYPE REF TO if_http_client,
        lv_http_rc     TYPE i,
        lv_response    TYPE string,
        lv_json        TYPE /ui2/cl_json=>json,
        lr_data        TYPE REF TO data,
        ls_response    TYPE ltyp_response.
*
  FIELD-SYMBOLS: <data>  TYPE data,
                 <field> TYPE any.
*
* Setup IF_HTTP_Client object
*
  CALL METHOD cl_http_client=>create_by_destination
    EXPORTING
      destination        = im_rfcdest
    IMPORTING
      client             = lo_http_client
    EXCEPTIONS
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      OTHERS             = 4.
*
  IF sy-subrc IS NOT INITIAL.
    ex_mess = 'Error creating new IF_HTTP_Client object. Process aborted.'.
    ex_subrc = 4.
    RETURN.
  ENDIF.
*
* Set protocol version
*
  lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_1 ).
*
* Set request header fields
*
  lo_http_client->request->set_header_field(
      name  = 'Content-Type'
      value = 'application/x-www-form-urlencoded' ).
*
  lo_http_client->request->set_header_field(
      name  = 'x-api-key'
      value = im_api_key ).
*
* Set form fields/attributes
*
  lo_http_client->request->if_http_entity~set_form_field(
    name   = 'userID'
    value  = im_user_id ).
*
  lo_http_client->request->if_http_entity~set_form_field(
    name   = 'applicationID'
    value  = im_application_id ).
*
  IF im_token_validity IS NOT INITIAL.
*
    lo_http_client->request->if_http_entity~set_form_field(
      name   = 'tokenValidity'
      value  = im_token_validity ).
*
  ENDIF.
*
  IF im_token_length IS NOT INITIAL.
*
    lo_http_client->request->if_http_entity~set_form_field(
      name   = 'tokenLength'
      value  = im_token_length ).
*
  ENDIF.
*
  lo_http_client->request->set_method( 'POST' ).
*
  lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.
*
* Send request
*
  CALL METHOD lo_http_client->send
    EXPORTING
      timeout                    = 60   " 15 Seconds
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2.
*
  IF sy-subrc IS NOT INITIAL.
*
    ex_mess = 'HTTP Post error. Process aborted'.
    ex_subrc = 4.
*
    CALL METHOD lo_http_client->close
      EXCEPTIONS
        http_invalid_state = 1
        OTHERS             = 2.
*
    FREE lo_http_client.
    RETURN.
*
  ENDIF.
*
* Read the Response
*
  CALL METHOD lo_http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.
*
  lo_http_client->response->get_status( IMPORTING code   = lv_http_rc ).
  lo_http_client->response->get_status( IMPORTING reason = ex_mess ).
  lv_response = lo_http_client->response->get_cdata( ).
*
  IF lv_http_rc NE '200'.
*
* Get error message back from Renda
*
    ex_subrc = 4.
    lv_json = lv_response.
    lr_data = /ui2/cl_json=>generate( json = lv_json ).
*
    IF lr_data IS BOUND.
      ASSIGN lr_data->* TO <data>.
      ASSIGN COMPONENT 'MESSAGE' OF STRUCTURE <data> TO <field>.
      IF <field> IS ASSIGNED.
        lr_data = <field>.
        ASSIGN lr_data->* TO <data>.
        ex_mess = <data>.
      ENDIF.
    ENDIF.
*
  ELSE.
*
    /ui2/cl_json=>deserialize( EXPORTING json = lv_response pretty_name = /ui2/cl_json=>pretty_mode-camel_case CHANGING data = ls_response ).
    ex_secret_key = ls_response-secretkey.
    ex_otp_url = ls_response-otpurl.
    ex_qr_code = ls_response-qrcode.
    ex_token = ls_response-token.
*
  ENDIF.
*
  CALL METHOD lo_http_client->close
    EXCEPTIONS
      http_invalid_state = 1
      OTHERS             = 2.
*
  FREE lo_http_client.
*
ENDFUNCTION.

Validate OTP using secret key

FUNCTION zverify_otp.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(IM_RFCDEST) TYPE  RFCDEST
*"     VALUE(IM_API_KEY) TYPE  STRING
*"     VALUE(IM_SECRET) TYPE  STRING
*"     VALUE(IM_TOKEN) TYPE  STRING
*"     VALUE(IM_TOKEN_VALIDITY) TYPE  STRING OPTIONAL
*"  EXPORTING
*"     VALUE(EX_IS_VALID) TYPE  FLAG
*"     VALUE(EX_TOKEN) TYPE  STRING
*"     VALUE(EX_TIME_USED) TYPE  STRING
*"     VALUE(EX_TIME_REMAINING) TYPE  STRING
*"     VALUE(EX_MESS) TYPE  STRING
*"     VALUE(EX_SUBRC) TYPE  SYSUBRC
*"----------------------------------------------------------------------
  TYPES: BEGIN OF ltyp_response,
           isvalid       TYPE flag,
           token         TYPE string,
           timeused      TYPE string,
           timeremaining TYPE string,
         END OF ltyp_response.

  DATA: lo_http_client TYPE REF TO if_http_client,
        lv_http_rc     TYPE i,
        lv_response    TYPE string,
        lv_json        TYPE /ui2/cl_json=>json,
        lr_data        TYPE REF TO data,
        ls_response    TYPE ltyp_response.
*
  FIELD-SYMBOLS: <data>  TYPE data,
                 <field> TYPE any.
*
* Setup IF_HTTP_Client object
*
  CALL METHOD cl_http_client=>create_by_destination
    EXPORTING
      destination        = im_rfcdest
    IMPORTING
      client             = lo_http_client
    EXCEPTIONS
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      OTHERS             = 4.
*
  IF sy-subrc IS NOT INITIAL.
    ex_mess = 'Error creating new IF_HTTP_Client object. Process aborted.'.
    ex_subrc = 4.
    RETURN.
  ENDIF.
*
* Set protocol version
*
  lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_1 ).
*
* Set request header fields
*
  lo_http_client->request->set_header_field(
      name  = 'Content-Type'
      value = 'application/x-www-form-urlencoded' ).
*
  lo_http_client->request->set_header_field(
      name  = 'x-api-key'
      value = im_api_key ).
*
* Set form fields/attributes
*
  lo_http_client->request->if_http_entity~set_form_field(
    name   = 'secret'
    value  = im_secret ).
*
  lo_http_client->request->if_http_entity~set_form_field(
    name   = 'token'
    value  = im_token ).
*
  IF im_token_validity IS NOT INITIAL.
*
    lo_http_client->request->if_http_entity~set_form_field(
      name   = 'tokenValidity'
      value  = im_token_validity ).
*
  ENDIF.
*
  lo_http_client->request->set_method( 'POST' ).
*
  lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.
*
* Send request
*
  CALL METHOD lo_http_client->send
    EXPORTING
      timeout                    = 60   " 15 Seconds
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2.
*
  IF sy-subrc IS NOT INITIAL.
*
    ex_mess = 'HTTP Post error. Process aborted'.
    ex_subrc = 4.
*
    CALL METHOD lo_http_client->close
      EXCEPTIONS
        http_invalid_state = 1
        OTHERS             = 2.
*
    FREE lo_http_client.
    RETURN.
*
  ENDIF.
*
* Read the Response
*
  CALL METHOD lo_http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.
*
  lo_http_client->response->get_status( IMPORTING code   = lv_http_rc ).
  lo_http_client->response->get_status( IMPORTING reason = ex_mess ).
  lv_response = lo_http_client->response->get_cdata( ).
*
  IF lv_http_rc NE '200'.
*
* Get error message back from Renda
*
    ex_subrc = 4.
    lv_json = lv_response.
    lr_data = /ui2/cl_json=>generate( json = lv_json ).
*
    IF lr_data IS BOUND.
      ASSIGN lr_data->* TO <data>.
      ASSIGN COMPONENT 'MESSAGE' OF STRUCTURE <data> TO <field>.
      IF <field> IS ASSIGNED.
        lr_data = <field>.
        ASSIGN lr_data->* TO <data>.
        ex_mess = <data>.
      ENDIF.
    ENDIF.
*
  ELSE.
*
    /ui2/cl_json=>deserialize( EXPORTING json = lv_response pretty_name = /ui2/cl_json=>pretty_mode-camel_case CHANGING data = ls_response ).
    ex_is_valid = ls_response-isvalid.
    ex_token = ls_response-token.
    ex_time_used = ls_response-timeused.
    ex_time_remaining = ls_response-timeremaining.
*
  ENDIF.
*
  CALL METHOD lo_http_client->close
    EXCEPTIONS
      http_invalid_state = 1
      OTHERS             = 2.
*
  FREE lo_http_client.
*
ENDFUNCTION.

Retrieve OTP for a secret key

zget_otp
FUNCTION zget_otp.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(IM_RFCDEST) TYPE  RFCDEST
*"     VALUE(IM_API_KEY) TYPE  STRING
*"     VALUE(IM_SECRET) TYPE  STRING
*"     VALUE(IM_TOKEN_VALIDITY) TYPE  STRING OPTIONAL
*"     VALUE(IM_TOKEN_LENGTH) TYPE  STRING OPTIONAL
*"  EXPORTING
*"     VALUE(EX_CURRENT_TOKEN) TYPE  STRING
*"     VALUE(EX_TIME_USED) TYPE  STRING
*"     VALUE(EX_TIME_REMAINING) TYPE  STRING
*"     VALUE(EX_PREVIOUS_TOKEN) TYPE  STRING
*"     VALUE(EX_NEXT_TOKEN) TYPE  STRING
*"     VALUE(EX_MESS) TYPE  STRING
*"     VALUE(EX_SUBRC) TYPE  SYSUBRC
*"----------------------------------------------------------------------
  TYPES: BEGIN OF ltyp_response,
           currenttoken  TYPE string,
           timeused      TYPE string,
           timeremaining TYPE string,
           previoustoken TYPE string,
           nexttoken     TYPE string,
         END OF ltyp_response.

  DATA: lo_http_client TYPE REF TO if_http_client,
        lv_http_rc     TYPE i,
        lv_response    TYPE string,
        lv_json        TYPE /ui2/cl_json=>json,
        lr_data        TYPE REF TO data,
        ls_response    TYPE ltyp_response.
*
  FIELD-SYMBOLS: <data>  TYPE data,
                 <field> TYPE any.
*
* Setup IF_HTTP_Client object
*
  CALL METHOD cl_http_client=>create_by_destination
    EXPORTING
      destination        = im_rfcdest
    IMPORTING
      client             = lo_http_client
    EXCEPTIONS
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      OTHERS             = 4.
*
  IF sy-subrc IS NOT INITIAL.
    ex_mess = 'Error creating new IF_HTTP_Client object. Process aborted.'.
    ex_subrc = 4.
    RETURN.
  ENDIF.
*
* Set protocol version
*
  lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_1 ).
*
* Set request header fields
*
  lo_http_client->request->set_header_field(
      name  = 'Content-Type'
      value = 'application/x-www-form-urlencoded' ).
*
  lo_http_client->request->set_header_field(
      name  = 'x-api-key'
      value = im_api_key ).
*
* Set form fields/attributes
*
  lo_http_client->request->if_http_entity~set_form_field(
    name   = 'secret'
    value  = im_secret ).
*
  IF im_token_validity IS NOT INITIAL.
*
    lo_http_client->request->if_http_entity~set_form_field(
      name   = 'tokenValidity'
      value  = im_token_validity ).
*
  ENDIF.
*
  IF im_token_length IS NOT INITIAL.
*
    lo_http_client->request->if_http_entity~set_form_field(
      name   = 'tokenLength'
      value  = im_token_length ).
*
  ENDIF.
*
  lo_http_client->request->set_method( 'POST' ).
*
  lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.
*
* Send request
*
  CALL METHOD lo_http_client->send
    EXPORTING
      timeout                    = 60   " 15 Seconds
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2.
*
  IF sy-subrc IS NOT INITIAL.
*
    ex_mess = 'HTTP Post error. Process aborted'.
    ex_subrc = 4.
*
    CALL METHOD lo_http_client->close
      EXCEPTIONS
        http_invalid_state = 1
        OTHERS             = 2.
*
    FREE lo_http_client.
    RETURN.
*
  ENDIF.
*
* Read the Response
*
  CALL METHOD lo_http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.
*
  lo_http_client->response->get_status( IMPORTING code   = lv_http_rc ).
  lo_http_client->response->get_status( IMPORTING reason = ex_mess ).
  lv_response = lo_http_client->response->get_cdata( ).
*
  IF lv_http_rc NE '200'.
*
* Get error message back from Renda
*
    ex_subrc = 4.
    lv_json = lv_response.
    lr_data = /ui2/cl_json=>generate( json = lv_json ).
*
    IF lr_data IS BOUND.
      ASSIGN lr_data->* TO <data>.
      ASSIGN COMPONENT 'MESSAGE' OF STRUCTURE <data> TO <field>.
      IF <field> IS ASSIGNED.
        lr_data = <field>.
        ASSIGN lr_data->* TO <data>.
        ex_mess = <data>.
      ENDIF.
    ENDIF.
*
  ELSE.
*
    /ui2/cl_json=>deserialize( EXPORTING json = lv_response pretty_name = /ui2/cl_json=>pretty_mode-camel_case CHANGING data = ls_response ).
    ex_current_token = ls_response-currenttoken.
    ex_time_used = ls_response-timeused.
    ex_time_remaining = ls_response-timeremaining.
    ex_previous_token = ls_response-previoustoken.
    ex_next_token = ls_response-nexttoken.
*
  ENDIF.
*
  CALL METHOD lo_http_client->close
    EXCEPTIONS
      http_invalid_state = 1
      OTHERS             = 2.
*
  FREE lo_http_client.
*
ENDFUNCTION.

Send SMS

FUNCTION zsend_sms.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(IM_RFCDEST) TYPE  RFCDEST
*"     VALUE(IM_API_KEY) TYPE  STRING
*"     VALUE(IM_NUMBER) TYPE  STRING
*"     VALUE(IM_MESSAGE) TYPE  STRING
*"     VALUE(IM_SENDER_NAME) TYPE  STRING OPTIONAL
*"  EXPORTING
*"     VALUE(EX_SUBRC) TYPE  SYSUBRC
*"     VALUE(EX_MESS) TYPE  STRING
*"----------------------------------------------------------------------
*
  DATA: lo_http_client TYPE REF TO if_http_client,
        lv_http_rc     TYPE i,
        lv_response    TYPE string,
        lv_json        TYPE /ui2/cl_json=>json,
        lr_data        TYPE REF TO data.
*
  FIELD-SYMBOLS: <data>  TYPE data,
                 <field> TYPE any.
*
* Setup IF_HTTP_Client object
*
  CALL METHOD cl_http_client=>create_by_destination
    EXPORTING
      destination        = im_rfcdest
    IMPORTING
      client             = lo_http_client
    EXCEPTIONS
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      OTHERS             = 4.
*
  IF sy-subrc IS NOT INITIAL.
    ex_mess = 'Error creating new IF_HTTP_Client object. Process aborted.'.
    ex_subrc = 4.
    RETURN.
  ENDIF.
*
* Set protocol version
*
  lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_1 ).
*
* Set request header fields
*
  lo_http_client->request->set_header_field(
      name  = 'x-api-key'
      value = im_api_key ).
*
* Set form fields/attributes
*
  lo_http_client->request->if_http_entity~set_form_field(
    name   = 'number'
    value  = im_number ).
*
  lo_http_client->request->if_http_entity~set_form_field(
    name   = 'message'
    value  = im_message ).
*
  IF im_sender_name IS NOT INITIAL.
*
    lo_http_client->request->if_http_entity~set_form_field(
      name   = 'senderName'
      value  = im_sender_name ).
*
  ENDIF.
*
  lo_http_client->request->set_method( 'POST' ).
*
  lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.
*
* Send request
*
  CALL METHOD lo_http_client->send
    EXPORTING
      timeout                    = 60   " 15 Seconds
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2.
*
  IF sy-subrc IS NOT INITIAL.
*
    ex_mess = 'HTTP Post error. Process aborted'.
    ex_subrc = 4.
*
    CALL METHOD lo_http_client->close
      EXCEPTIONS
        http_invalid_state = 1
        OTHERS             = 2.
*
    FREE lo_http_client.
    RETURN.
*
  ENDIF.
*
* Read the Response
*
  CALL METHOD lo_http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.
*
  lo_http_client->response->get_status( IMPORTING code   = lv_http_rc ).
  lo_http_client->response->get_status( IMPORTING reason = ex_mess ).
  lv_response = lo_http_client->response->get_cdata( ).
*
  IF lv_http_rc NE '200'.
*
* Get error message back from Renda
*
    ex_subrc = 4.
    lv_json = lv_response.
    lr_data = /ui2/cl_json=>generate( json = lv_json ).
*
    IF lr_data IS BOUND.
      ASSIGN lr_data->* TO <data>.
      ASSIGN COMPONENT 'MESSAGE' OF STRUCTURE <data> TO <field>.
      IF <field> IS ASSIGNED.
        lr_data = <field>.
        ASSIGN lr_data->* TO <data>.
        ex_mess = <data>.
      ENDIF.
    ENDIF.
*
  ENDIF.
*
  CALL METHOD lo_http_client->close
    EXCEPTIONS
      http_invalid_state = 1
      OTHERS             = 2.
*
  FREE lo_http_client.
*
ENDFUNCTION.

Last updated