Category: SAP

  • What is SAP Cloud Application Programming Model

    Preface – This post is part of the SAP CAPM series.

    Introduction

    We have been writing codes in different languages (such as ABAP, CDS, UI5/FIori and OData) at different platform (SAP NetWeaver, SAP Editor, HANA Studio, and SAP Web IDE) to achieve CRUD operation on business data. These all have their own limitation such as source independent development, platform independent development, etc. To remove dependency on multiple platforms and to combine SAP and open-source tools at a single platform, SAP came with Cloud Application Programming Model. In this article we will explore it in detail.

    SAP Cloud Application Programming Model

    SAP Cloud Application Programming Model, also known as SAP CAP is a frameworks of tools, languages and libraries (both open source and SAP tools and technologies) designed efficiently with SAP best practices to help developers to minimize coding efforts, develop reusable peace of codes in form of micro services and to focus on designing and implementing business/enterprise specific logic.

    Architecture of SAP Cloud Application Programming Model

    In this section we will talk about two different architectures. The first one will be the high level architecture provided by SAP. The other one will emphasize more on the flow of development.

    SAP Cloud Application Programming Model

    According to the architecture attached above, we can conclude that in this programming model we will be using:

    1. Web IDE and other development tools such as Visual Studio Code.
    2. To model our data we will be using the annotation power of HANA CDS.
    3. To write business logic we will be using Service SDKs such as Node.js or Java
    4. To deploy our node module, java module, UI5 Module we will have Cloud Foundry as Paas.
    5. To save our business data we will be using HANA or other database
    6. To develop our user Interface we will be using SAP Fiori and other UI tools
    7. The whole tool and framework will be utilizing on SAP Cloud Platform Infrastructure

    Now to understand the architecture in a better way, we can redesign it as below:

    SAP CAP Architecture

    In the architecture shown above we have illustrated the flow of development in terms of CAP development.

     

    Technologies and Tools Involved in SAP Cloud Application Programming Model

    Tool/Technology Purpose Description
    Hana Data Base Store Business Data This can be any database
    Core Data Services Model Data Service It mainly uses HANA CDS Artifacts
    Java/ Node.js Add Business logic In place of ABAP, business logic will be written here
    UI5/Fiori UI Design Like convention Fiori Application, the UI will be developed in same uniform manner
    SAP Cloud Foundry CAP Deployment The CAP application is deployed in to SAP Cloud Foundry as a MTA application

     

    Why and When to choose SAP Cloud Application Programming Model

    Everyone after looking the CAP framework wonders about its implementation in real life business use case. This framework is best suited for the development of applications for the enterprise having Cloud Infrastructure. Since this Application framework can consume both Hana Database as well on premise based OData exposed database, thus it is suitable for all types of enterprises.

    Even implementation of SAP Cloud Platform Integration is best supported by SAP CAP framework.

  • Function Imports in SAP OData

    Preface – This post is part of the ABAP Programs series.

    DATA: ls_parameter TYPE /iwbep/s_mgw_name_value_pair,
          lv_custid    TYPE kunnr,
          lv_flag      TYPE char1,
          lt_custinfo  TYPE TABLE OF ztest_gw_srv,
          ls_custinfo  TYPE ztest_gw_srv,
          ls_entity    TYPE zcl_ztest_gw_srv_mpc=>ts_msg_return.
    
    
        IF iv_action_name = 'demoFuncImport'. " Check what action is being requested
          IF it_parameter IS NOT INITIAL.
    
    * Read Function import parameter value
            READ TABLE it_parameter INTO ls_parameter WITH KEY name = 'CustId'.
            IF sy-subrc = 0.
              lv_custid = ls_parameter-value.
            ENDIF.
    
            READ TABLE it_parameter INTO ls_parameter WITH KEY name = 'Approved_Flag'.
            IF sy-subrc = 0.
              lv_flag = ls_parameter-value.
            ENDIF.
    
            IF  lv_custid IS NOT INITIAL.
              UPDATE ztest_gw_srv SET approved = lv_flag WHERE cust_id = lv_custid.
              IF sy-subrc = 0.
                ls_entity-type   = 'S'.
                ls_entity-message = 'Customer info successfully approved'.
              ELSE.
                ls_entity-type   = 'E'.
                ls_entity-message = 'Error'.
              ENDIF.
    
    
    * Call method copy_data_to_ref and export entity set data
              copy_data_to_ref( EXPORTING is_data = ls_entity
                      CHANGING cr_data = er_data ).
    
            ENDIF.
          ENDIF.
        ENDIF.

     

  • Convert JSON to ABAP Internal Table

    Preface – This post is part of the ABAP Programs series.

     TYPES:
            BEGIN OF t_entry,
              access_token TYPE string,
              token_type   TYPE string,
              expires_in   TYPE n LENGTH 8,
              scope        TYPE string,
              jti          TYPE string,
            END OF t_entry .
          TYPES:
            t_entry_map TYPE SORTED TABLE OF t_entry WITH UNIQUE KEY access_token.
          DATA: m_entries TYPE t_entry.
    
          DATA: lr_instance  TYPE REF TO  /ui5/cl_json_parser.
          CREATE OBJECT lr_instance.
          CALL METHOD me->get_token.
          IF gv_token IS NOT INITIAL.
    
    *        data: itab TYPE TABLE OF string.
    *        data: access_tok type string.
    *        SPLIT gv_token at '"' INTO TABLE itab.
    *        try.
    *            lv_auth2 = itab[ 4 ].
    *          catch cx_sy_itab_line_not_found.
    *        ENDTRY.
    
            /ui2/cl_json=>deserialize(
            EXPORTING json = gv_token pretty_name = /ui2/cl_json=>pretty_mode-camel_case CHANGING data = m_entries ).
            lv_auth2 = m_entries-access_token.
            gv_token = gv_token+17.

     

  • Create Object History in SAP Blockchain

    Preface – This post is part of the Blockchain on SAP Cloud Platform and ABAP Programs series.

    In this article, we will learn how to create a block in Blockchain on the SAP Cloud Platform.

     METHOD create_object_history.
        DATA: lo_http_client TYPE REF TO if_http_client.
        DATA: response TYPE string,
              lv_url   TYPE string,
              lv_auth  TYPE string.
        CONSTANTS : lv_initial_url TYPE string VALUE '<Your_Service>'.
    
        IF iv_object_id IS NOT INITIAL.
    *** Getting Token
          TYPES:
            BEGIN OF t_entry,
              access_token TYPE string,
              token_type   TYPE string,
              expires_in   TYPE n LENGTH 8,
              scope        TYPE string,
              jti          TYPE string,
            END OF t_entry .
          TYPES:
            t_entry_map TYPE SORTED TABLE OF t_entry WITH UNIQUE KEY access_token.
          DATA: m_entries TYPE t_entry.
    
          DATA: lr_instance  TYPE REF TO  /ui5/cl_json_parser.
          CREATE OBJECT lr_instance.
          CALL METHOD me->get_token.
          IF gv_token IS NOT INITIAL.
    
            /ui2/cl_json=>deserialize(
            EXPORTING json = gv_token pretty_name = /ui2/cl_json=>pretty_mode-camel_case CHANGING data = m_entries ).
            DATA lv_auth2 TYPE string.
            lv_auth2 = m_entries-access_token.
    *        gv_token = gv_token+17.
    
            CONCATENATE 'Bearer' lv_auth2 INTO lv_auth SEPARATED BY space.
    
          ENDIF.
          DATA lv_object_id TYPE string.
          lv_object_id = iv_object_id.
          TRANSLATE lv_object_id TO LOWER CASE.
    
          CONCATENATE lv_initial_url lv_object_id INTO lv_url. "Appending Fix URL and the Object ID to get the Request URL
    
          "create HTTP client by url
          CALL METHOD cl_http_client=>create_by_url
            EXPORTING
              url                = lv_url
            IMPORTING
              client             = lo_http_client
            EXCEPTIONS
              argument_not_found = 1
              plugin_not_active  = 2
              internal_error     = 3
              OTHERS             = 4.
    
          "Available API Endpoints
          "https://blockchain-service.cfapps.sap.hana.ondemand.com/blockchain/proofOfHistory/api/v1
          "https://blockchain-service.cfapps.eu10.hana.ondemand.com/blockchain/proofOfHistory/api/v1
          "https://blockchain-service.cfapps.us10.hana.ondemand.com/blockchain/proofOfHistory/api/v1
    
          IF sy-subrc <> 0.
            "error handling
          ENDIF.
    
          "setting request method
          lo_http_client->request->set_method('POST').
    
          "adding headers
          lo_http_client->request->set_header_field( name = 'Content-Type' value = 'application/x-www-form-urlencoded' ).
          lo_http_client->request->set_header_field( name = 'Accept' value = 'application/json' ).
          lo_http_client->request->set_header_field( name = 'Authorization' value = lv_auth ).
    
          "Available Security Schemes for productive API Endpoints
          "OAuth 2.0
    
          CALL METHOD lo_http_client->send
            EXCEPTIONS
              http_communication_failure = 1
              http_invalid_state         = 2
              http_processing_failed     = 3
              http_invalid_timeout       = 4
              OTHERS                     = 5.
    
          IF sy-subrc = 0.
            CALL METHOD lo_http_client->receive
              EXCEPTIONS
                http_communication_failure = 1
                http_invalid_state         = 2
                http_processing_failed     = 3
                OTHERS                     = 5.
          ENDIF.
    
          IF sy-subrc <> 0.
            "error handling
            response = lo_http_client->response->get_cdata( ).
            ev_response = response.
          ELSE.
            response = lo_http_client->response->get_cdata( ).
            IF response IS NOT INITIAL.
              ev_response = response.
            ELSE.
              ev_response = 'Block created successfully'.
            ENDIF.
          ENDIF.
        ENDIF.
      ENDMETHOD.
    

     

  • Get Proof history from the Blockchain Service using SAP ABAP

    Preface – This post is part of the Blockchain on SAP Cloud Platform and ABAP Programs series.

    In this step, we will call the blockchain service again using the token that we have stored in above step. And then we will receive the history of transactions from blockchain.

    METHOD get_proof_history.
        DATA: lo_http_client TYPE REF TO if_http_client.
        DATA: response TYPE string,
              lv_url   TYPE string,
              lv_auth  TYPE string,
              lv_auth2 TYPE string.
        CONSTANTS : lv_initial_url TYPE string VALUE '<Your Service>'.
        IF iv_object_id IS NOT INITIAL.
    *** Getting Token
          TYPES:
            BEGIN OF t_entry,
              access_token TYPE string,
              token_type   TYPE string,
              expires_in   TYPE n LENGTH 8,
              scope        TYPE string,
              jti          TYPE string,
            END OF t_entry .
          TYPES:
            t_entry_map TYPE SORTED TABLE OF t_entry WITH UNIQUE KEY access_token.
          DATA: m_entries TYPE t_entry.
          DATA: lr_instance  TYPE REF TO  /ui5/cl_json_parser.
          CREATE OBJECT lr_instance.
          CALL METHOD me->get_token.
          IF gv_token IS NOT INITIAL.
    *        data: itab TYPE TABLE OF string.
    *        data: access_tok type string.
    *        SPLIT gv_token at '"' INTO TABLE itab.
    *        try.
    *            lv_auth2 = itab[ 4 ].
    *          catch cx_sy_itab_line_not_found.
    *        ENDTRY.
            /ui2/cl_json=>deserialize(
            EXPORTING json = gv_token pretty_name = /ui2/cl_json=>pretty_mode-camel_case CHANGING data = m_entries ).
            lv_auth2 = m_entries-access_token.
            gv_token = gv_token+17.
            CONCATENATE 'Bearer' lv_auth2 INTO lv_auth SEPARATED BY space.
          ENDIF.
          DATA lv_object_id TYPE string.
          lv_object_id = iv_object_id.
          TRANSLATE lv_object_id TO LOWER CASE.
          CONCATENATE lv_initial_url lv_object_id INTO lv_url. "Appending Fix URL and the Object ID to get the Request URL
          "create HTTP client by url
          CALL METHOD cl_http_client=>create_by_url
            EXPORTING
              url                = lv_url
            IMPORTING
              client             = lo_http_client
            EXCEPTIONS
              argument_not_found = 1
              plugin_not_active  = 2
              internal_error     = 3
              OTHERS             = 4.
          "Available API Endpoints
          "https://blockchain-service.cfapps.sap.hana.ondemand.com/blockchain/proofOfHistory/api/v1
          "https://blockchain-service.cfapps.eu10.hana.ondemand.com/blockchain/proofOfHistory/api/v1
          "https://blockchain-service.cfapps.us10.hana.ondemand.com/blockchain/proofOfHistory/api/v1
          IF sy-subrc <> 0.
            "error handling
          ENDIF.
          "setting request method
          lo_http_client->request->set_method('GET').
          "creatung Auth value
    *       lv_auth2 = 'Basic <Your Login Credentials>'.
          "adding headers
    *      lo_http_client->request->set_header_field( name = 'Content-Type' value = 'application/x-www-form-urlencoded' ).
          lo_http_client->request->set_header_field( name = 'Accept' value = 'application/json' ).
          lo_http_client->request->set_header_field( name = 'Authorization' value = lv_auth ).
    *      lo_http_client->request->set_header_field( name = 'APIKey' value = 'zBoCpDtkaT9jexRjtMk0J98Rs8izmQi1' ).
          "Available Security Schemes for productive API Endpoints
          "OAuth 2.0
          CALL METHOD lo_http_client->send
            EXCEPTIONS
              http_communication_failure = 1
              http_invalid_state         = 2
              http_processing_failed     = 3
              http_invalid_timeout       = 4
              OTHERS                     = 5.
          IF sy-subrc = 0.
            CALL METHOD lo_http_client->receive
              EXCEPTIONS
                http_communication_failure = 1
                http_invalid_state         = 2
                http_processing_failed     = 3
                OTHERS                     = 5.
          ENDIF.
          IF sy-subrc = 1.
            "error handling
            ev_response = 'http_communication_failure'.
          ELSEIF sy-subrc = 2.
            ev_response = 'http_invalid_state'.
          ELSEIF sy-subrc = 3.
            ev_response = 'http_processing_failed'.
          ELSEIF sy-subrc = 0.
            response = lo_http_client->response->get_cdata( ).
    *WRITE: 'response: ', response.
            ev_response = response.
          ELSE.
            ev_response = 'Unknown Error'.
          ENDIF.
        ENDIF.
      ENDMETHOD.

     

  • Get Token from SAP Cloud Platform Service

    Preface – This post is part of the ABAP Programs series.

    In this step we will call the service to get the token and store that token in a global variable for later use.

    METHOD get_token.
        DATA: lo_http_client TYPE REF TO if_http_client.
        DATA: response TYPE string,
              lv_url   TYPE string.
        CONSTANTS: lv_initial_url TYPE string VALUE '<Blockchain Service Link>',
                   lv_auth        TYPE string VALUE 'Basic <your login credentials>'.
        "create HTTP client by url
        CALL METHOD cl_http_client=>create_by_url
          EXPORTING
            url                = lv_initial_url
          IMPORTING
            client             = lo_http_client
          EXCEPTIONS
            argument_not_found = 1
            plugin_not_active  = 2
            internal_error     = 3
            OTHERS             = 4.
        "Available API Endpoints
        "https://blockchain-service.cfapps.sap.hana.ondemand.com/blockchain/proofOfHistory/api/v1
        "https://blockchain-service.cfapps.eu10.hana.ondemand.com/blockchain/proofOfHistory/api/v1
        "https://blockchain-service.cfapps.us10.hana.ondemand.com/blockchain/proofOfHistory/api/v1
        IF sy-subrc <> 0.
          "error handling
        ENDIF.
        "setting request method
        lo_http_client->request->set_method('GET').
        "adding headers
        lo_http_client->request->set_header_field( name = 'Authorization' value = lv_auth ).
        "Available Security Schemes for productive API Endpoints
        "OAuth 2.0
        CALL METHOD lo_http_client->send
          EXCEPTIONS
            http_communication_failure = 1
            http_invalid_state         = 2
            http_processing_failed     = 3
            http_invalid_timeout       = 4
            OTHERS                     = 5.
        IF sy-subrc = 0.
          CALL METHOD lo_http_client->receive
            EXCEPTIONS
              http_communication_failure = 1
              http_invalid_state         = 2
              http_processing_failed     = 3
              OTHERS                     = 5.
        ENDIF.
        IF sy-subrc <> 0.
          "error handling
        ENDIF.
        response = lo_http_client->response->get_cdata( ).
       GV_TOKEN = response. "Global Variable
      ENDMETHOD.

     

  • Calculate a hash in ABAP

    Preface – This post is part of the ABAP Programs series.

    In ABAP we can perform the following:

    calculate_hash_for_raw

    METHOD calculate_hash_for_raw.
    
        TRY.
            cl_abap_message_digest=>calculate_hash_for_raw(
              EXPORTING
                if_algorithm = 'SHA256'
                if_data      = iv_data
              IMPORTING
                ef_hashstring = ev_sha256
            ).
          CATCH cx_root.
            " Eh, what're you gonna do?
        ENDTRY.
      ENDMETHOD.

    calculate_hash_for_string

    METHOD calculate_hash_for_string.
    
        TRY.
            cl_abap_message_digest=>calculate_hash_for_char(
              EXPORTING
                if_algorithm = 'SHA256'
                if_data      = iv_data
              IMPORTING
                ef_hashstring = ev_sha256
            ).
          CATCH cx_root.
            " Eh, what're you gonna do?
        ENDTRY.
      ENDMETHOD.

     

  • Navigate between Views in Web Dynpro

    Preface – This post is part of the SAP ABAP Web Dynpro series.

    Introduction

    In our previous articles we have already seen how to create a view in Web Dynpro. In Web Dynpro we can create multiple views under a component. It is also important to embed that view in the window to make it visible. In this article we will explore how to navigate between Views in Web Dynpro. Before jumping directly to the navigation concept, we will start with the concept of plugs in Web Dynpro.

    Navigate between Views in Web Dynpro

    A Web Dynpro application needs following to navigate between two views within a window:

    1. Views: At least two views which will have the navigation concept implemented.
    2. Navigation Links: A Navigation link is the connection between the above views.
    3. Plugs: A plug is the port that exists at both the ends of the Navigation link.

    The above concepts can be understood with the diagram shown below:

    Navigating between Views in Web Dynpro

    Plugs

    A plug is a junction used for accessing or existing a view. Either you will access a view or exist a view; and for both case we have two different plugs. These are:

    Inbound Plug

    This is a plug which is used to enter a view. Every inbound plug must be linked to an outbound plug else it produces errors.

    Outbound Plug

    This is a plug which is used to exit a view. Every outbound plug must be linked to an inbound plug else it produces errors.

    Navigation Link

    A Navigation link is the connection between two views. It needs plugs to execute a navigation.

    Step by step process to create a Navigation between views

    Step 1: Go to SE80, create a web Dynpro component ZTest_PLUGS.

    Step 2: Go to main view, insert a button, provide button text as ‘Press’, and create action method for button ON_PRESS.

    Step 3: Right click on component -> create -> view, provide name as DETAILS_VIEW and enter.

    Step 4: Go to “DETAILS_VIEW” view, insert a button, provide button text as ‘Back’, create action method for button ON_BACK.

    Step 5: Go to main view, outbound plugs tab, add outbound plug as GOTO_DETAILS_VIEW and inbound plugs tab add inbound plug as IN_MAIN.

    Step 6: Go to DETAILS_VIEW view, outbound plugs tab, add outbound plug as GOTO_MAIN and inbound plugs tab add inbound plug as IN_DETAILS.

    Step 7: Expand window, drag and drop MAIN, “DETAILS_VIEW” view into window.

    Step 8: Expand two views, right click on GOTO_ DETAILS_VIEW -> Create Navigation Link, press F4, select destination view as DETAILS_VIEW and enter.

    Step 9: Right click on GOTO_MAIN -> Create Navigation Link, press F4, select destination view as MAIN and enter.

    Step 10: Go to main view actions tab, double click on ON_PRESS method and add below code.

    wd_this->FIRE_GOTO_ DETAILS_VIEW_PLG( );

    Step 11: Go to DETAILS_VIEW view actions tab, double click on ON_BACK method and add below code.

    wd_this->FIRE_GOTO_MAIN_PLG( );

    Step 12: Save and Activate the components.

    Step 13: Run and Test the components.

  • Data Binding in Web Dynpro

    Preface – This post is part of the SAP ABAP Web Dynpro series.

    Introduction

    A Web Dynpro Application is developed using MVC (Model View Controller) Architecture. It means whatever data model is there that requires binding with the View elements (e.g. Input Field, Check box, Table, etc.). This binding can be achieved using view and controller. In this article we will explore data binding in Web Dynpro.

    Data Binding in Web Dynpro

    In Web Dynpro, we have multiple components. In our previous article, we have already discussed that we have something called Context which is a data container which stores data at runtime. A context can have multiple nodes e.g. Table nodes which will store table data. These nodes can have multiple Attributes that are the fields of the table.

    Definition

    Binding of the attributes or nodes of a context with UI elements (e.g. Table, Input Fields, etc.) is known as Data Binding in Web Dynpro.

    Data Binding in Web Dynpro

    Why we need Data Binding

    1. To show Customer Data on the Web Dynpro screen
    2. To auto fill the input fields based on pre-defined selections
    3. To provide search helps for an input element
    4. To maintain/provide conditions on UI based on backend data

    Types of Data Binding

    1. Internal Mapping
      It is a mapping between contexts of single component.
    2. External Mapping
      It is a mapping between multiple contexts using their interface controller.

    Setup and Coding for Data Binding

    In our previous articles we have already shared steps to create a Web Dynpro View and publish it as an application. You can follow steps and code mentioned here to convert that application in an Application which utilizes data binding.

  • Blockchain Implementation in ABAP

    Preface – This post is part of the Blockchain on SAP Cloud Platform series.

    Introduction

    With the introduction of Blockchain in SAP Cloud Platform, the important questions for SAP developers was its implementation in SAP ABAP. All the client information of SAP is mainly on premise based. The major amount of data is already stored in backend system and if we cannot push this data on blockchain then the Blockchain on SAP Cloud Platform is of no use. In this article we will explain the Blockchain Implementation in ABAP.

    Blockchain on SAP Cloud Platform

    In our previous articles we have already explained the blockchain services (mainly Hyperledger, Multichain and Quorum) on SAP Cloud Platform. These instances are connected with SAP Blockchain enablement services, which acts as an interface between these blockchain services and other SAP services.

    Blockchain Implementation in ABAP

    We can easily understand the blockchain implementation in ABAP using the given block diagram:

    Blockchain Implementation in ABAP

    In the diagram above, you can see that the SAP ABAP interacts with Blockchain Enablement services and the HANA Database. This application can be either displayed using Classical ABAP Report or using Fiori Application which will then use OData to interact with SAP ABAP methods.

    To call any service in SAP Cloud platform, we need to do the following:

    1. Get Token from the service using a read call.
    2. Perform CRUD operation on the service using the token received above.

    Let us understand these steps with respect to the blockchain services.

    Get Token from the Blockchain Service

    In this step we will call the service to get the token and store that token in a global variable for later use.

    METHOD get_token.
        DATA: lo_http_client TYPE REF TO if_http_client.
        DATA: response TYPE string,
              lv_url   TYPE string.
        CONSTANTS: lv_initial_url TYPE string VALUE '<Blockchain Service Link>',
                   lv_auth        TYPE string VALUE 'Basic <your login credentials>'.
    
    
        "create HTTP client by url
        CALL METHOD cl_http_client=>create_by_url
          EXPORTING
            url                = lv_initial_url
          IMPORTING
            client             = lo_http_client
          EXCEPTIONS
            argument_not_found = 1
            plugin_not_active  = 2
            internal_error     = 3
            OTHERS             = 4.
    
        "Available API Endpoints
        "https://blockchain-service.cfapps.sap.hana.ondemand.com/blockchain/proofOfHistory/api/v1
        "https://blockchain-service.cfapps.eu10.hana.ondemand.com/blockchain/proofOfHistory/api/v1
        "https://blockchain-service.cfapps.us10.hana.ondemand.com/blockchain/proofOfHistory/api/v1
    
        IF sy-subrc <> 0.
          "error handling
        ENDIF.
    
        "setting request method
        lo_http_client->request->set_method('GET').
    
        "adding headers
        lo_http_client->request->set_header_field( name = 'Authorization' value = lv_auth ).
    
    
        "Available Security Schemes for productive API Endpoints
        "OAuth 2.0
    
        CALL METHOD lo_http_client->send
          EXCEPTIONS
            http_communication_failure = 1
            http_invalid_state         = 2
            http_processing_failed     = 3
            http_invalid_timeout       = 4
            OTHERS                     = 5.
    
        IF sy-subrc = 0.
          CALL METHOD lo_http_client->receive
            EXCEPTIONS
              http_communication_failure = 1
              http_invalid_state         = 2
              http_processing_failed     = 3
              OTHERS                     = 5.
        ENDIF.
    
        IF sy-subrc <> 0.
          "error handling
        ENDIF.
    
        response = lo_http_client->response->get_cdata( ).
    
       GV_TOKEN = response. "Global Variable
      ENDMETHOD.

     

    Get Proof history from the Blockchain Service

    In this step we will call the blockchain service again using the token that we have stored in above step. And then we will receive the history of transactions from blockchain.

    METHOD get_proof_history.
        DATA: lo_http_client TYPE REF TO if_http_client.
        DATA: response TYPE string,
              lv_url   TYPE string,
              lv_auth  TYPE string,
              lv_auth2 TYPE string.
    
        CONSTANTS : lv_initial_url TYPE string VALUE '<Your Service>'.
    
        IF iv_object_id IS NOT INITIAL.
    
    *** Getting Token
          TYPES:
            BEGIN OF t_entry,
              access_token TYPE string,
              token_type   TYPE string,
              expires_in   TYPE n LENGTH 8,
              scope        TYPE string,
              jti          TYPE string,
            END OF t_entry .
          TYPES:
            t_entry_map TYPE SORTED TABLE OF t_entry WITH UNIQUE KEY access_token.
          DATA: m_entries TYPE t_entry.
    
          DATA: lr_instance  TYPE REF TO  /ui5/cl_json_parser.
          CREATE OBJECT lr_instance.
          CALL METHOD me->get_token.
          IF gv_token IS NOT INITIAL.
    
    *        data: itab TYPE TABLE OF string.
    *        data: access_tok type string.
    *        SPLIT gv_token at '"' INTO TABLE itab.
    *        try.
    *            lv_auth2 = itab[ 4 ].
    *          catch cx_sy_itab_line_not_found.
    *        ENDTRY.
    
            /ui2/cl_json=>deserialize(
            EXPORTING json = gv_token pretty_name = /ui2/cl_json=>pretty_mode-camel_case CHANGING data = m_entries ).
            lv_auth2 = m_entries-access_token.
            gv_token = gv_token+17.
    
            CONCATENATE 'Bearer' lv_auth2 INTO lv_auth SEPARATED BY space.
    
          ENDIF.
          DATA lv_object_id TYPE string.
          lv_object_id = iv_object_id.
          TRANSLATE lv_object_id TO LOWER CASE.
    
          CONCATENATE lv_initial_url lv_object_id INTO lv_url. "Appending Fix URL and the Object ID to get the Request URL
    
          "create HTTP client by url
          CALL METHOD cl_http_client=>create_by_url
            EXPORTING
              url                = lv_url
            IMPORTING
              client             = lo_http_client
            EXCEPTIONS
              argument_not_found = 1
              plugin_not_active  = 2
              internal_error     = 3
              OTHERS             = 4.
    
          "Available API Endpoints
          "https://blockchain-service.cfapps.sap.hana.ondemand.com/blockchain/proofOfHistory/api/v1
          "https://blockchain-service.cfapps.eu10.hana.ondemand.com/blockchain/proofOfHistory/api/v1
          "https://blockchain-service.cfapps.us10.hana.ondemand.com/blockchain/proofOfHistory/api/v1
    
          IF sy-subrc <> 0.
            "error handling
          ENDIF.
    
          "setting request method
          lo_http_client->request->set_method('GET').
    
          "creatung Auth value
    *       lv_auth2 = 'Basic <Your Login Credentials>'.
    
          "adding headers
    *      lo_http_client->request->set_header_field( name = 'Content-Type' value = 'application/x-www-form-urlencoded' ).
          lo_http_client->request->set_header_field( name = 'Accept' value = 'application/json' ).
          lo_http_client->request->set_header_field( name = 'Authorization' value = lv_auth ).
    *      lo_http_client->request->set_header_field( name = 'APIKey' value = 'zBoCpDtkaT9jexRjtMk0J98Rs8izmQi1' ).
    
    
          "Available Security Schemes for productive API Endpoints
          "OAuth 2.0
    
          CALL METHOD lo_http_client->send
            EXCEPTIONS
              http_communication_failure = 1
              http_invalid_state         = 2
              http_processing_failed     = 3
              http_invalid_timeout       = 4
              OTHERS                     = 5.
    
          IF sy-subrc = 0.
            CALL METHOD lo_http_client->receive
              EXCEPTIONS
                http_communication_failure = 1
                http_invalid_state         = 2
                http_processing_failed     = 3
                OTHERS                     = 5.
          ENDIF.
    
          IF sy-subrc = 1.
            "error handling
            ev_response = 'http_communication_failure'.
          ELSEIF sy-subrc = 2.
            ev_response = 'http_invalid_state'.
          ELSEIF sy-subrc = 3.
            ev_response = 'http_processing_failed'.
          ELSEIF sy-subrc = 0.
            response = lo_http_client->response->get_cdata( ).
    *WRITE: 'response: ', response.
            ev_response = response.
          ELSE.
            ev_response = 'Unknown Error'.
          ENDIF.
        ENDIF.
      ENDMETHOD.