REPORT z_run_multi_actions_in_sac.
PARAMETERS:
pv_ma_id TYPE string LOWER CASE DEFAULT 'Input multi-actions id here'.
DATA lo_client TYPE REF TO if_http_client.
DATA lo_rest_client TYPE REF TO if_rest_client.
DATA lo_rest_entity TYPE REF TO if_rest_entity.
DATA lo_request TYPE REF TO if_rest_entity.
DATA lv_body TYPE string.
DATA lv_http_status TYPE string.
DATA lv_response TYPE string.
DATA lv_acc_token_to_destination TYPE string.
DATA lv_acc_token_to_sac TYPE string.
DATA lv_csrf TYPE string.
*----------------------------------------get access to destination service---------------------------------
cl_http_client=>create_by_url( EXPORTING url = '<your token url for the btp destination service>' IMPORTING client = lo_client ).
CREATE OBJECT lo_rest_client TYPE cl_rest_http_client
EXPORTING
io_http_client = lo_client.
lo_request = lo_rest_client->create_request_entity( ).
lo_request->set_content_type( iv_media_type = 'application/x-www-form-urlencoded' ).
lv_body = 'grant_type=client_credentials&client_id=<your oauth client id for destination service>&client_secret=<your oauth client secret for destination service>'.
lo_request->set_string_data( lv_body ).
lo_rest_client->post( EXPORTING io_entity = lo_request ).
lv_http_status = lo_rest_client->get_status( ).
lo_rest_entity = lo_rest_client->get_response_entity( ).
lv_response = lo_rest_entity->get_string_data( ).
DATA lr_data TYPE REF TO data.
/ui2/cl_json=>deserialize(
EXPORTING
json = lv_response
pretty_name = /ui2/cl_json=>pretty_mode-user
assoc_arrays = abap_true
CHANGING
data = lr_data ).
IF lr_data IS BOUND.
ASSIGN lr_data->* TO FIELD-SYMBOL(<lfs_data>).
ASSIGN COMPONENT 'access_token' OF STRUCTURE <lfs_data> TO FIELD-SYMBOL(<lfs_results>).
ASSIGN <lfs_results>->* TO FIELD-SYMBOL(<ld_token_value>).
WRITE: 'Get access to destination service successfully'.
ENDIF.
lv_acc_token_to_destination = <ld_token_value>.
*----------------------------------------get access to destination service---------------------------------
cl_http_client=>create_by_url( EXPORTING url = '<your token url for the btp destination service>' IMPORTING client = lo_client ).
CREATE OBJECT lo_rest_client TYPE cl_rest_http_client
EXPORTING
io_http_client = lo_client.
lo_request = lo_rest_client->create_request_entity( ).
lo_request->set_content_type( iv_media_type = 'application/x-www-form-urlencoded' ).
lv_body = 'grant_type=client_credentials&client_id=<your oauth client id for destination service>&client_secret=<your oauth client secret for destination service>'.
lo_request->set_string_data( lv_body ).
lo_rest_client->post( EXPORTING io_entity = lo_request ).
lv_http_status = lo_rest_client->get_status( ).
lo_rest_entity = lo_rest_client->get_response_entity( ).
lv_response = lo_rest_entity->get_string_data( ).
DATA lr_data TYPE REF TO data.
/ui2/cl_json=>deserialize(
EXPORTING
json = lv_response
pretty_name = /ui2/cl_json=>pretty_mode-user
assoc_arrays = abap_true
CHANGING
data = lr_data ).
IF lr_data IS BOUND.
ASSIGN lr_data->* TO FIELD-SYMBOL(<lfs_data>).
ASSIGN COMPONENT 'access_token' OF STRUCTURE <lfs_data> TO FIELD-SYMBOL(<lfs_results>).
ASSIGN <lfs_results>->* TO FIELD-SYMBOL(<ld_token_value>).
WRITE: 'Get access to destination service successfully'.
ENDIF.
lv_acc_token_to_destination = <ld_token_value>.
*----------------------------------------send request get csrf---------------------------------
cl_http_client=>create_by_url( EXPORTING url = '<your SAC url>/api/v1/csrf' IMPORTING client = lo_client ).
CREATE OBJECT lo_rest_client TYPE cl_rest_http_client
EXPORTING
io_http_client = lo_client.
lo_rest_client->set_request_header(
EXPORTING
iv_name = 'X-CSRF-TOKEN'
iv_value = 'FETCH' ).
lo_rest_client->set_request_header(
EXPORTING
iv_name = 'Authorization'
iv_value = lv_acc_token_to_sac ).
lo_rest_client->get( ).
lv_http_status = lo_rest_client->get_status( ).
lo_rest_entity = lo_rest_client->get_response_entity( ).
lv_csrf = lo_rest_entity->get_header_field( 'X-CSRF-TOKEN' ).
DATA lt_headers TYPE tihttpnvp.
DATA ls_header TYPE ihttpnvp.
lt_headers = lo_rest_entity->get_header_fields( ).
DATA lt_cookies TYPE TABLE OF string.
DATA lv_cookie TYPE string.
DATA lv_cookie_remaining TYPE string.
LOOP AT lt_headers INTO ls_header.
IF ls_header-name = 'set-cookie'.
IF ls_header-value CS 'JSESSIONID='.
SPLIT ls_header-value AT ';' INTO lv_cookie lv_cookie_remaining.
lv_cookie = lv_cookie+11.
EXIT.
ENDIF.
ENDIF.
ENDLOOP.
*WRITE: lv_csrf.
DATA lv_decoded_cookie TYPE string.
lv_decoded_cookie = cl_http_utility=>unescape_url(
escaped = lv_cookie ).
WRITE: / 'Get csrf token from SAC public api service'.
*WRITE: lv_csrf.
*WRITE: lv_decoded_cookie.
*----------------------------------------trigger multiactions---------------------------------
DATA lv_url TYPE string.
lv_url = '<your SAC url>/api/v1/multiActions/' && pv_ma_id && '/executions'.
cl_http_client=>create_by_url( EXPORTING url = lv_url IMPORTING client = lo_client ).
CALL METHOD lo_client->request->set_method( if_http_request=>co_request_method_post ).
lo_client->request->set_header_field( name = 'x-csrf-token' value = lv_csrf ).
lo_client->request->set_header_field( name = 'Authorization' value = lv_acc_token_to_sac ).
lo_client->request->set_header_field( name = 'Content-Type' value = 'application/json' ).
lo_client->request->set_cookie( name = 'JSESSIONID'
value = lv_decoded_cookie ).
lv_body = '{"parameterValues": [ ]}'.
lo_client->request->set_cdata( data = lv_body ).
CALL METHOD lo_client->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.
ASSERT sy-subrc = 0.
CALL METHOD lo_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.
lv_response = lo_client->response->get_cdata( ).
WRITE: lv_response.
WRITE: / 'Trigger multi-actions ' && pv_ma_id && ' successfully'.
{
"pmaid": "t.7:FD000504F025851615C5037A42DD56BE",
"pparameter":
{
"parameterId": "TargetVersion",
"value": {
"memberIds": [
"public.Actual"
],
"hierarchyId": ""
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
16 | |
12 | |
9 | |
7 | |
7 | |
6 | |
6 | |
5 | |
5 | |
5 |