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.
Leave a Reply