Repository navigation
Expand file tree
/
Copy pathzrp_read_event_mesh_data.txt
More file actions
189 lines (151 loc) · 8.19 KB
/
Copy pathzrp_read_event_mesh_data.txt
File metadata and controls
189 lines (151 loc) · 8.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
*&---------------------------------------------------------------------*
*& Report ZRP_READ_EVENT_MESH_DATA
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zrp_read_event_mesh_data.
*******************************************************************************************
* Variable Declaratin Section *
*******************************************************************************************
DATA: lo_oa2c_client TYPE REF TO if_oauth2_client.
DATA(lv_dest_name) = 'EVENT_MESH_DEMO'.
DATA(lv_auth_profile) = 'EVENT_MESH_DEMO_PROFILE'.
******************************************************************************************
* Instantiate HTTP Client using rfc destination name *
******************************************************************************************
** Create RFC connect
cl_http_client=>create_by_destination(
EXPORTING destination = lv_dest_name
IMPORTING client = DATA(lo_http_client)
EXCEPTIONS argument_not_found = 1
destination_not_found = 2
destination_no_authority = 3
plugin_not_active = 4
internal_error = 5
OTHERS = 6 ).
lo_http_client->propertytype_logon_popup = if_http_client=>co_disabled.
IF sy-subrc <> 0.
* Raise error and exception handling in case of instation of http_client failed
ENDIF.
*****************************************************************************************
* Set Authentication token (oAuth token) to the connection *
*****************************************************************************************
** Set token for authorization OAuth 2.0
TRY.
cl_oauth2_client=>create( EXPORTING i_profile = CONV #( lv_auth_profile )
RECEIVING ro_oauth2_client = lo_oa2c_client ).
CATCH cx_oa2c.
* Raise error and exception handling in case of client creation failed for OAuth profile
ENDTRY.
TRY.
lo_oa2c_client->set_token( EXPORTING io_http_client = lo_http_client ).
CATCH cx_oa2c.
TRY.
CALL METHOD lo_oa2c_client->execute_cc_flow.
CATCH cx_oa2c.
* Raise error and exception handling in case of token set is failed
ENDTRY.
TRY.
lo_oa2c_client->set_token( EXPORTING io_http_client = lo_http_client ).
CATCH cx_oa2c.
* Raise error and exception handling in case of token set is failed
ENDTRY.
ENDTRY.
DATA(lt_request_headers) = VALUE tihttpnvp( ( name = /apeu/if_constants=>gc_http_req_header-qos
value = /apeu/if_constants=>gc_http_req_hdr_values-value_1 ) ).
***********************************************************************
* Setting up of Request Parameters to Remote call *
***********************************************************************
** Set URI
cl_http_utility=>set_request_uri( EXPORTING request = lo_http_client->request
uri = '/messagingrest/v1/queues/demoqueue/messages/consumption' ).
** Set request headers for the call if passed
LOOP AT lt_request_headers ASSIGNING FIELD-SYMBOL(<ls_headers>).
lo_http_client->request->set_header_field( EXPORTING name = <ls_headers>-name
value = <ls_headers>-value ).
ENDLOOP.
lo_http_client->refresh_cookie( ).
** Set Request Method (GET/POST/PATCH)
lo_http_client->request->set_method( method = 'POST' ).
***********************************************************************
* Trigger the communication *
***********************************************************************
** Send the call
lo_http_client->send( EXCEPTIONS http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
http_invalid_timeout = 4 ).
IF sy-subrc <> 0.
* Raise error and exception handling if call is failed
ENDIF.
********Fire Recieve call to fetch response from (http_client)
lo_http_client->receive( EXCEPTIONS http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3 ).
IF sy-subrc <> 0.
* Raise error and exception handling if response is not received
ENDIF.
DATA: lt_headers TYPE tihttpnvp.
**** Fetch Response Headers
CALL METHOD lo_http_client->response->get_header_fields( CHANGING fields = lt_headers ).
lo_http_client->response->get_status( IMPORTING code = DATA(lv_code) ).
IF lv_code = 404 OR lv_code = 400 OR lv_code = 204.
CLEAR lv_code.
* Raise error and exception handling
ENDIF.
**** Fetch response Payload
DATA(lv_payload) = lo_http_client->response->get_cdata( ).
DATA: lr_data TYPE REF TO data.
** Deserialize payload into a deep structure
/ui2/cl_json=>deserialize( EXPORTING json = lv_payload " JSON string
pretty_name = /ui2/cl_json=>pretty_mode-camel_case " Pretty Print property names
assoc_arrays = abap_true " Deserialize associative array as tables with unique keys
conversion_exits = abap_true " Use DDIC conversion continues on deserialize of values
CHANGING data = lr_data ). " Data to serialize
TRY .
DATA(lv_msg_id) = lt_headers[ name = /apeu/if_constants=>gc_http_req_header-msg_id ]-value.
CATCH cx_sy_itab_line_not_found.
* Raise error and exception handling
ENDTRY.
******************************************************************************************
* Step 5: Parse Payload fetched from Event Mesh * *
******************************************************************************************
* TODO: Write parsing payload based on requirement
******************************************************************************************
* Send akcnowledgement to EM and delete message from queue *
******************************************************************************************
CONCATENATE '/messagingrest/v1/queues/demoqueue/messages/' lv_msg_id '/acknowledgement' INTO DATA(lv_acknowledgement_uri).
** Set URI
cl_http_utility=>set_request_uri( EXPORTING request = lo_http_client->request
uri = lv_acknowledgement_uri ).
lo_http_client->refresh_cookie( ).
** Set Request Method (GET/POST/PATCH)
lo_http_client->request->set_method( method = 'POST' ).
***********************************************************************
* Trigger the communication *
***********************************************************************
** Send the call
lo_http_client->send( EXCEPTIONS http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
http_invalid_timeout = 4 ).
IF sy-subrc <> 0.
* Raise error and exception handling if call is failed
ENDIF.
********Fire Recieve call to fetch response from (http_client)
lo_http_client->receive( EXCEPTIONS http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3 ).
IF sy-subrc <> 0.
* Raise error and exception handling if response is not received
ENDIF.
CLEAR: lv_code.
lo_http_client->response->get_status( IMPORTING code = lv_code ).
** Add log
IF lv_code = 202.
* Success call once acknowledgement is done
ENDIF.
** Close connection
IF lo_http_client IS NOT INITIAL.
lo_http_client->close( ).
ENDIF.