Code Samples

Generate PDF from HTML

ABAP
1
FUNCTION zgenerate_pdf.
2
*"----------------------------------------------------------------------
3
*"*"Local Interface:
4
*" IMPORTING
5
*" VALUE(IM_RFCDEST) TYPE RFCDEST
6
*" VALUE(IM_API_KEY) TYPE STRING
7
*" VALUE(IM_HTML) TYPE STRING
8
*" VALUE(IM_ENCRYPTION_KEY) TYPE STRING OPTIONAL
9
*" VALUE(IM_LANDSCAPE) TYPE STRING OPTIONAL
10
*" VALUE(IM_FORMAT) TYPE STRING OPTIONAL
11
*" VALUE(IM_MARGIN_TOP) TYPE STRING OPTIONAL
12
*" VALUE(IM_MARGIN_RIGHT) TYPE STRING OPTIONAL
13
*" VALUE(IM_MARGIN_BOTTOM) TYPE STRING OPTIONAL
14
*" VALUE(IM_MARGIN_LEFT) TYPE STRING OPTIONAL
15
*" EXPORTING
16
*" VALUE(EX_SUBRC) TYPE SYSUBRC
17
*" VALUE(EX_MESS) TYPE STRING
18
*" VALUE(EX_PDF) TYPE XSTRING
19
*"----------------------------------------------------------------------
20
*
21
DATA: lo_http_client TYPE REF TO if_http_client,
22
lv_http_rc TYPE i,
23
lv_response TYPE string,
24
lv_json TYPE /ui2/cl_json=>json,
25
lr_data TYPE REF TO data.
26
*
27
FIELD-SYMBOLS: <data> TYPE data,
28
<field> TYPE any.
29
*
30
* Setup IF_HTTP_Client object
31
*
32
CALL METHOD cl_http_client=>create_by_destination
33
EXPORTING
34
destination = im_rfcdest
35
IMPORTING
36
client = lo_http_client
37
EXCEPTIONS
38
argument_not_found = 1
39
plugin_not_active = 2
40
internal_error = 3
41
OTHERS = 4.
42
*
43
IF sy-subrc IS NOT INITIAL.
44
ex_mess = 'Error creating new IF_HTTP_Client object. Process aborted.'.
45
ex_subrc = 4.
46
RETURN.
47
ENDIF.
48
*
49
* Set protocol version
50
*
51
lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_1 ).
52
*
53
* Set request header fields
54
*
55
lo_http_client->request->set_header_field(
56
name = 'Accept'
57
value = 'application/pdf' ).
58
*
59
lo_http_client->request->set_header_field(
60
name = 'Content-Type'
61
value = 'application/x-www-form-urlencoded' ).
62
*
63
lo_http_client->request->set_header_field(
64
name = 'x-api-key'
65
value = im_api_key ).
66
*
67
* Set form fields/attributes
68
*
69
lo_http_client->request->if_http_entity~set_form_field(
70
name = 'inputPayload'
71
value = im_html ).
72
*
73
IF im_encryption_key IS NOT INITIAL.
74
*
75
lo_http_client->request->if_http_entity~set_form_field(
76
name = 'encryptionKey'
77
value = im_encryption_key ).
78
*
79
ENDIF.
80
*
81
IF im_landscape IS NOT INITIAL.
82
*
83
lo_http_client->request->if_http_entity~set_form_field(
84
name = 'landscape'
85
value = im_landscape ).
86
*
87
ENDIF.
88
*
89
IF im_format IS NOT INITIAL.
90
*
91
lo_http_client->request->if_http_entity~set_form_field(
92
name = 'format'
93
value = im_format ).
94
*
95
ENDIF.
96
*
97
IF im_margin_top IS NOT INITIAL.
98
*
99
lo_http_client->request->if_http_entity~set_form_field(
100
name = 'marginTop'
101
value = im_margin_top ).
102
*
103
ENDIF.
104
*
105
IF im_margin_right IS NOT INITIAL.
106
*
107
lo_http_client->request->if_http_entity~set_form_field(
108
name = 'marginRight'
109
value = im_margin_right ).
110
*
111
ENDIF.
112
*
113
IF im_margin_bottom IS NOT INITIAL.
114
*
115
lo_http_client->request->if_http_entity~set_form_field(
116
name = 'marginBottom'
117
value = im_margin_bottom ).
118
*
119
ENDIF.
120
*
121
IF im_margin_left IS NOT INITIAL.
122
*
123
lo_http_client->request->if_http_entity~set_form_field(
124
name = 'marginLeft'
125
value = im_margin_left ).
126
*
127
ENDIF.
128
*
129
lo_http_client->request->set_method( 'POST' ).
130
*
131
lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.
132
*
133
* Send request
134
*
135
CALL METHOD lo_http_client->send
136
EXPORTING
137
timeout = 60 " 15 Seconds
138
EXCEPTIONS
139
http_communication_failure = 1
140
http_invalid_state = 2.
141
*
142
IF sy-subrc IS NOT INITIAL.
143
*
144
ex_mess = 'HTTP Post error. Process aborted'.
145
ex_subrc = 4.
146
*
147
CALL METHOD lo_http_client->close
148
EXCEPTIONS
149
http_invalid_state = 1
150
OTHERS = 2.
151
*
152
FREE lo_http_client.
153
RETURN.
154
*
155
ENDIF.
156
*
157
* Read the Response
158
*
159
CALL METHOD lo_http_client->receive
160
EXCEPTIONS
161
http_communication_failure = 1
162
http_invalid_state = 2
163
http_processing_failed = 3.
164
*
165
lo_http_client->response->get_status( IMPORTING code = lv_http_rc ).
166
lo_http_client->response->get_status( IMPORTING reason = ex_mess ).
167
lv_response = lo_http_client->response->get_cdata( ).
168
*
169
IF lv_http_rc NE '200'.
170
*
171
* Get error message back from Renda
172
*
173
ex_subrc = 4.
174
lv_json = lv_response.
175
lr_data = /ui2/cl_json=>generate( json = lv_json ).
176
*
177
IF lr_data IS BOUND.
178
ASSIGN lr_data->* TO <data>.
179
ASSIGN COMPONENT 'MESSAGE' OF STRUCTURE <data> TO <field>.
180
IF <field> IS ASSIGNED.
181
lr_data = <field>.
182
ASSIGN lr_data->* TO <data>.
183
ex_mess = <data>.
184
ENDIF.
185
ENDIF.
186
*
187
ELSE.
188
*
189
ex_pdf = lo_http_client->response->get_data( ).
190
*
191
ENDIF.
192
*
193
CALL METHOD lo_http_client->close
194
EXCEPTIONS
195
http_invalid_state = 1
196
OTHERS = 2.
197
*
198
FREE lo_http_client.
199
*
200
ENDFUNCTION.
Copied!

Generate QR Code

ABAP
1
FUNCTION zgenerate_qr_code.
2
*"----------------------------------------------------------------------
3
*"*"Local Interface:
4
*" IMPORTING
5
*" VALUE(IM_RFCDEST) TYPE RFCDEST
6
*" VALUE(IM_API_KEY) TYPE STRING
7
*" VALUE(IM_DATA) TYPE STRING
8
*" EXPORTING
9
*" VALUE(EX_SUBRC) TYPE SYSUBRC
10
*" VALUE(EX_MESS) TYPE STRING
11
*" VALUE(EX_QR_CODE) TYPE XSTRING
12
*"----------------------------------------------------------------------
13
*
14
DATA: lo_http_client TYPE REF TO if_http_client,
15
lv_http_rc TYPE i,
16
lv_response TYPE string,
17
lv_json TYPE /ui2/cl_json=>json,
18
lr_data TYPE REF TO data.
19
*
20
FIELD-SYMBOLS: <data> TYPE data,
21
<field> TYPE any.
22
*
23
* Setup IF_HTTP_Client object
24
*
25
CALL METHOD cl_http_client=>create_by_destination
26
EXPORTING
27
destination = im_rfcdest
28
IMPORTING
29
client = lo_http_client
30
EXCEPTIONS
31
argument_not_found = 1
32
plugin_not_active = 2
33
internal_error = 3
34
OTHERS = 4.
35
*
36
IF sy-subrc IS NOT INITIAL.
37
ex_mess = 'Error creating new IF_HTTP_Client object. Process aborted.'.
38
ex_subrc = 4.
39
RETURN.
40
ENDIF.
41
*
42
* Set protocol version
43
*
44
lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_1 ).
45
*
46
* Set request header fields
47
*
48
lo_http_client->request->set_header_field(
49
name = 'Accept'
50
value = '*/*' ).
51
*
52
lo_http_client->request->set_header_field(
53
name = 'Content-Type'
54
value = 'application/x-www-form-urlencoded' ).
55
*
56
lo_http_client->request->set_header_field(
57
name = 'x-api-key'
58
value = im_api_key ).
59
*
60
* Set form fields/attributes
61
*
62
lo_http_client->request->if_http_entity~set_form_field(
63
name = 'data'
64
value = im_data ).
65
*
66
lo_http_client->request->set_method( 'POST' ).
67
*
68
lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.
69
*
70
* Send request
71
*
72
CALL METHOD lo_http_client->send
73
EXPORTING
74
timeout = 60 " 15 Seconds
75
EXCEPTIONS
76
http_communication_failure = 1
77
http_invalid_state = 2.
78
*
79
IF sy-subrc IS NOT INITIAL.
80
*
81
ex_mess = 'HTTP Post error. Process aborted'.
82
ex_subrc = 4.
83
*
84
CALL METHOD lo_http_client->close
85
EXCEPTIONS
86
http_invalid_state = 1
87
OTHERS = 2.
88
*
89
FREE lo_http_client.
90
RETURN.
91
*
92
ENDIF.
93
*
94
* Read the Response
95
*
96
CALL METHOD lo_http_client->receive
97
EXCEPTIONS
98
http_communication_failure = 1
99
http_invalid_state = 2
100
http_processing_failed = 3.
101
*
102
lo_http_client->response->get_status( IMPORTING code = lv_http_rc ).
103
lo_http_client->response->get_status( IMPORTING reason = ex_mess ).
104
lv_response = lo_http_client->response->get_cdata( ).
105
*
106
IF lv_http_rc NE '200'.
107
*
108
* Get error message back from Renda
109
*
110
ex_subrc = 4.
111
lv_json = lv_response.
112
lr_data = /ui2/cl_json=>generate( json = lv_json ).
113
*
114
IF lr_data IS BOUND.
115
ASSIGN lr_data->* TO <data>.
116
ASSIGN COMPONENT 'MESSAGE' OF STRUCTURE <data> TO <field>.
117
IF <field> IS ASSIGNED.
118
lr_data = <field>.
119
ASSIGN lr_data->* TO <data>.
120
ex_mess = <data>.
121
ENDIF.
122
ENDIF.
123
*
124
ELSE.
125
*
126
ex_qr_code = lo_http_client->response->get_data( ).
127
*
128
ENDIF.
129
*
130
CALL METHOD lo_http_client->close
131
EXCEPTIONS
132
http_invalid_state = 1
133
OTHERS = 2.
134
*
135
FREE lo_http_client.
136
*
137
ENDFUNCTION.
Copied!

Onboard user for TOTP

ABAP
1
FUNCTION zonboard_user.
2
*"----------------------------------------------------------------------
3
*"*"Local Interface:
4
*" IMPORTING
5
*" VALUE(IM_RFCDEST) TYPE RFCDEST
6
*" VALUE(IM_API_KEY) TYPE STRING
7
*" VALUE(IM_USER_ID) TYPE STRING
8
*" VALUE(IM_APPLICATION_ID) TYPE STRING
9
*" VALUE(IM_TOKEN_VALIDITY) TYPE STRING OPTIONAL
10
*" VALUE(IM_TOKEN_LENGTH) TYPE STRING OPTIONAL
11
*" EXPORTING
12
*" VALUE(EX_SECRET_KEY) TYPE STRING
13
*" VALUE(EX_OTP_URL) TYPE STRING
14
*" VALUE(EX_QR_CODE) TYPE STRING
15
*" VALUE(EX_TOKEN) TYPE STRING
16
*" VALUE(EX_MESS) TYPE STRING
17
*" VALUE(EX_SUBRC) TYPE SYSUBRC
18
*"----------------------------------------------------------------------
19
TYPES: BEGIN OF ltyp_response,
20
secretkey TYPE string,
21
otpurl TYPE string,
22
qrcode TYPE string,
23
token TYPE string,
24
END OF ltyp_response.
25
26
DATA: lo_http_client TYPE REF TO if_http_client,
27
lv_http_rc TYPE i,
28
lv_response TYPE string,
29
lv_json TYPE /ui2/cl_json=>json,
30
lr_data TYPE REF TO data,
31
ls_response TYPE ltyp_response.
32
*
33
FIELD-SYMBOLS: <data> TYPE data,
34
<field> TYPE any.
35
*
36
* Setup IF_HTTP_Client object
37
*
38
CALL METHOD cl_http_client=>create_by_destination
39
EXPORTING
40
destination = im_rfcdest
41
IMPORTING
42
client = lo_http_client
43
EXCEPTIONS
44
argument_not_found = 1
45
plugin_not_active = 2
46
internal_error = 3
47
OTHERS = 4.
48
*
49
IF sy-subrc IS NOT INITIAL.
50
ex_mess = 'Error creating new IF_HTTP_Client object. Process aborted.'.
51
ex_subrc = 4.
52
RETURN.
53
ENDIF.
54
*
55
* Set protocol version
56
*
57
lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_1 ).
58
*
59
* Set request header fields
60
*
61
lo_http_client->request->set_header_field(
62
name = 'Content-Type'
63
value = 'application/x-www-form-urlencoded' ).
64
*
65
lo_http_client->request->set_header_field(
66
name = 'x-api-key'
67
value = im_api_key ).
68
*
69
* Set form fields/attributes
70
*
71
lo_http_client->request->if_http_entity~set_form_field(
72
name = 'userID'
73
value = im_user_id ).
74
*
75
lo_http_client->request->if_http_entity~set_form_field(
76
name = 'applicationID'
77
value = im_application_id ).
78
*
79
IF im_token_validity IS NOT INITIAL.
80
*
81
lo_http_client->request->if_http_entity~set_form_field(
82
name = 'tokenValidity'
83
value = im_token_validity ).
84
*
85
ENDIF.
86
*
87
IF im_token_length IS NOT INITIAL.
88
*
89
lo_http_client->request->if_http_entity~set_form_field(
90
name = 'tokenLength'
91
value = im_token_length ).
92
*
93
ENDIF.
94
*
95
lo_http_client->request->set_method( 'POST' ).
96
*
97
lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.
98
*
99
* Send request
100
*
101
CALL METHOD lo_http_client->send
102
EXPORTING
103
timeout = 60 " 15 Seconds
104
EXCEPTIONS
105
http_communication_failure = 1
106
http_invalid_state = 2.
107
*
108
IF sy-subrc IS NOT INITIAL.
109
*
110
ex_mess = 'HTTP Post error. Process aborted'.
111
ex_subrc = 4.
112
*
113
CALL METHOD lo_http_client->close
114
EXCEPTIONS
115
http_invalid_state = 1
116
OTHERS = 2.
117
*
118
FREE lo_http_client.
119
RETURN.
120
*
121
ENDIF.
122
*
123
* Read the Response
124
*
125
CALL METHOD lo_http_client->receive
126
EXCEPTIONS
127
http_communication_failure = 1
128
http_invalid_state = 2
129
http_processing_failed = 3.
130
*
131
lo_http_client->response->get_status( IMPORTING code = lv_http_rc ).
132
lo_http_client->response->get_status( IMPORTING reason = ex_mess ).
133
lv_response = lo_http_client->response->get_cdata( ).
134
*
135
IF lv_http_rc NE '200'.
136
*
137
* Get error message back from Renda
138
*
139
ex_subrc = 4.
140
lv_json = lv_response.
141
lr_data = /ui2/cl_json=>generate( json = lv_json ).
142
*
143
IF lr_data IS BOUND.
144
ASSIGN lr_data->* TO <data>.
145
ASSIGN COMPONENT 'MESSAGE' OF STRUCTURE <data> TO <field>.
146
IF <field> IS ASSIGNED.
147
lr_data = <field>.
148
ASSIGN lr_data->* TO <data>.
149
ex_mess = <data>.
150
ENDIF.
151
ENDIF.
152
*
153
ELSE.
154
*
155
/ui2/cl_json=>deserialize( EXPORTING json = lv_response pretty_name = /ui2/cl_json=>pretty_mode-camel_case CHANGING data = ls_response ).
156
ex_secret_key = ls_response-secretkey.
157
ex_otp_url = ls_response-otpurl.
158
ex_qr_code = ls_response-qrcode.
159
ex_token = ls_response-token.
160
*
161
ENDIF.
162
*
163
CALL METHOD lo_http_client->close
164
EXCEPTIONS
165
http_invalid_state = 1
166
OTHERS = 2.
167
*
168
FREE lo_http_client.
169
*
170
ENDFUNCTION.
Copied!

Validate OTP using secret key

ABAP
1
FUNCTION zverify_otp.
2
*"----------------------------------------------------------------------
3
*"*"Local Interface:
4
*" IMPORTING
5
*" VALUE(IM_RFCDEST) TYPE RFCDEST
6
*" VALUE(IM_API_KEY) TYPE STRING
7
*" VALUE(IM_SECRET) TYPE STRING
8
*" VALUE(IM_TOKEN) TYPE STRING
9
*" VALUE(IM_TOKEN_VALIDITY) TYPE STRING OPTIONAL
10
*" EXPORTING
11
*" VALUE(EX_IS_VALID) TYPE FLAG
12
*" VALUE(EX_TOKEN) TYPE STRING
13
*" VALUE(EX_TIME_USED) TYPE STRING
14
*" VALUE(EX_TIME_REMAINING) TYPE STRING
15
*" VALUE(EX_MESS) TYPE STRING
16
*" VALUE(EX_SUBRC) TYPE SYSUBRC
17
*"----------------------------------------------------------------------
18
TYPES: BEGIN OF ltyp_response,
19
isvalid TYPE flag,
20
token TYPE string,
21
timeused TYPE string,
22
timeremaining TYPE string,
23
END OF ltyp_response.
24
25
DATA: lo_http_client TYPE REF TO if_http_client,
26
lv_http_rc TYPE i,
27
lv_response TYPE string,
28
lv_json TYPE /ui2/cl_json=>json,
29
lr_data TYPE REF TO data,
30
ls_response TYPE ltyp_response.
31
*
32
FIELD-SYMBOLS: <data> TYPE data,
33
<field> TYPE any.
34
*
35
* Setup IF_HTTP_Client object
36
*
37
CALL METHOD cl_http_client=>create_by_destination
38
EXPORTING
39
destination = im_rfcdest
40
IMPORTING
41
client = lo_http_client
42
EXCEPTIONS
43
argument_not_found = 1
44
plugin_not_active = 2
45
internal_error = 3
46
OTHERS = 4.
47
*
48
IF sy-subrc IS NOT INITIAL.
49
ex_mess = 'Error creating new IF_HTTP_Client object. Process aborted.'.
50
ex_subrc = 4.
51
RETURN.
52
ENDIF.
53
*
54
* Set protocol version
55
*
56
lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_1 ).
57
*
58
* Set request header fields
59
*
60
lo_http_client->request->set_header_field(
61
name = 'Content-Type'
62
value = 'application/x-www-form-urlencoded' ).
63
*
64
lo_http_client->request->set_header_field(
65
name = 'x-api-key'
66
value = im_api_key ).
67
*
68
* Set form fields/attributes
69
*
70
lo_http_client->request->if_http_entity~set_form_field(
71
name = 'secret'
72
value = im_secret ).
73
*
74
lo_http_client->request->if_http_entity~set_form_field(
75
name = 'token'
76
value = im_token ).
77
*
78
IF im_token_validity IS NOT INITIAL.
79
*
80
lo_http_client->request->if_http_entity~set_form_field(
81
name = 'tokenValidity'
82
value = im_token_validity ).
83
*
84
ENDIF.
85
*
86
lo_http_client->request->set_method( 'POST' ).
87
*
88
lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.
89
*
90
* Send request
91
*
92
CALL METHOD lo_http_client->send
93
EXPORTING
94
timeout = 60 " 15 Seconds
95
EXCEPTIONS
96
http_communication_failure = 1
97
http_invalid_state = 2.
98
*
99
IF sy-subrc IS NOT INITIAL.
100
*
101
ex_mess = 'HTTP Post error. Process aborted'.
102
ex_subrc = 4.
103
*
104
CALL METHOD lo_http_client->close
105
EXCEPTIONS
106
http_invalid_state = 1
107
OTHERS = 2.
108
*
109
FREE lo_http_client.
110
RETURN.
111
*
112
ENDIF.
113
*
114
* Read the Response
115
*
116
CALL METHOD lo_http_client->receive
117
EXCEPTIONS
118
http_communication_failure = 1
119
http_invalid_state = 2
120
http_processing_failed = 3.
121
*
122
lo_http_client->response->get_status( IMPORTING code = lv_http_rc ).
123
lo_http_client->response->get_status( IMPORTING reason = ex_mess ).
124
lv_response = lo_http_client->response->get_cdata( ).
125
*
126
IF lv_http_rc NE '200'.
127
*
128
* Get error message back from Renda
129
*
130
ex_subrc = 4.
131
lv_json = lv_response.
132
lr_data = /ui2/cl_json=>generate( json = lv_json ).
133
*
134
IF lr_data IS BOUND.
135
ASSIGN lr_data->* TO <data>.
136
ASSIGN COMPONENT 'MESSAGE' OF STRUCTURE <data> TO <field>.
137
IF <field> IS ASSIGNED.
138
lr_data = <field>.
139
ASSIGN lr_data->* TO <data>.
140
ex_mess = <data>.
141
ENDIF.
142
ENDIF.
143
*
144
ELSE.
145
*
146
/ui2/cl_json=>deserialize( EXPORTING json = lv_response pretty_name = /ui2/cl_json=>pretty_mode-camel_case CHANGING data = ls_response ).
147
ex_is_valid = ls_response-isvalid.
148
ex_token = ls_response-token.
149
ex_time_used = ls_response-timeused.
150
ex_time_remaining = ls_response-timeremaining.
151
*
152
ENDIF.
153
*
154
CALL METHOD lo_http_client->close
155
EXCEPTIONS
156
http_invalid_state = 1
157
OTHERS = 2.
158
*
159
FREE lo_http_client.
160
*
161
ENDFUNCTION.
Copied!

Retrieve OTP for a secret key

ABAP
zget_otp
1
FUNCTION zget_otp.
2
*"----------------------------------------------------------------------
3
*"*"Local Interface:
4
*" IMPORTING
5
*" VALUE(IM_RFCDEST) TYPE RFCDEST
6
*" VALUE(IM_API_KEY) TYPE STRING
7
*" VALUE(IM_SECRET) TYPE STRING
8
*" VALUE(IM_TOKEN_VALIDITY) TYPE STRING OPTIONAL
9
*" VALUE(IM_TOKEN_LENGTH) TYPE STRING OPTIONAL
10
*" EXPORTING
11
*" VALUE(EX_CURRENT_TOKEN) TYPE STRING
12
*" VALUE(EX_TIME_USED) TYPE STRING
13
*" VALUE(EX_TIME_REMAINING) TYPE STRING
14
*" VALUE(EX_PREVIOUS_TOKEN) TYPE STRING
15
*" VALUE(EX_NEXT_TOKEN) TYPE STRING
16
*" VALUE(EX_MESS) TYPE STRING
17
*" VALUE(EX_SUBRC) TYPE SYSUBRC
18
*"----------------------------------------------------------------------
19
TYPES: BEGIN OF ltyp_response,
20
currenttoken TYPE string,
21
timeused TYPE string,
22
timeremaining TYPE string,
23
previoustoken TYPE string,
24
nexttoken TYPE string,
25
END OF ltyp_response.
26
27
DATA: lo_http_client TYPE REF TO if_http_client,
28
lv_http_rc TYPE i,
29
lv_response TYPE string,
30
lv_json TYPE /ui2/cl_json=>json,
31
lr_data TYPE REF TO data,
32
ls_response TYPE ltyp_response.
33
*
34
FIELD-SYMBOLS: <data> TYPE data,
35
<field> TYPE any.
36
*
37
* Setup IF_HTTP_Client object
38
*
39
CALL METHOD cl_http_client=>create_by_destination
40
EXPORTING
41
destination = im_rfcdest
42
IMPORTING
43
client = lo_http_client
44
EXCEPTIONS
45
argument_not_found = 1
46
plugin_not_active = 2
47
internal_error = 3
48
OTHERS = 4.
49
*
50
IF sy-subrc IS NOT INITIAL.
51
ex_mess = 'Error creating new IF_HTTP_Client object. Process aborted.'.
52
ex_subrc = 4.
53
RETURN.
54
ENDIF.
55
*
56
* Set protocol version
57
*
58
lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_1 ).
59
*
60
* Set request header fields
61
*
62
lo_http_client->request->set_header_field(
63
name = 'Content-Type'
64
value = 'application/x-www-form-urlencoded' ).
65
*
66
lo_http_client->request->set_header_field(
67
name = 'x-api-key'
68
value = im_api_key ).
69
*
70
* Set form fields/attributes
71
*
72
lo_http_client->request->if_http_entity~set_form_field(
73
name = 'secret'
74
value = im_secret ).
75
*
76
IF im_token_validity IS NOT INITIAL.
77
*
78
lo_http_client->request->if_http_entity~set_form_field(
79
name = 'tokenValidity'
80
value = im_token_validity ).
81
*
82
ENDIF.
83
*
84
IF im_token_length IS NOT INITIAL.
85
*
86
lo_http_client->request->if_http_entity~set_form_field(
87
name = 'tokenLength'
88
value = im_token_length ).
89
*
90
ENDIF.
91
*
92
lo_http_client->request->set_method( 'POST' ).
93
*
94
lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.
95
*
96
* Send request
97
*
98
CALL METHOD lo_http_client->send
99
EXPORTING
100
timeout = 60 " 15 Seconds
101
EXCEPTIONS
102
http_communication_failure = 1
103
http_invalid_state = 2.
104
*
105
IF sy-subrc IS NOT INITIAL.
106
*
107
ex_mess = 'HTTP Post error. Process aborted'.
108
ex_subrc = 4.
109
*
110
CALL METHOD lo_http_client->close
111
EXCEPTIONS
112
http_invalid_state = 1
113
OTHERS = 2.
114
*
115
FREE lo_http_client.
116
RETURN.
117
*
118
ENDIF.
119
*
120
* Read the Response
121
*
122
CALL METHOD lo_http_client->receive
123
EXCEPTIONS
124
http_communication_failure = 1
125
http_invalid_state = 2
126
http_processing_failed = 3.
127
*
128
lo_http_client->response->get_status( IMPORTING code = lv_http_rc ).
129
lo_http_client->response->get_status( IMPORTING reason = ex_mess ).
130
lv_response = lo_http_client->response->get_cdata( ).
131
*
132
IF lv_http_rc NE '200'.
133
*
134
* Get error message back from Renda
135
*
136
ex_subrc = 4.
137
lv_json = lv_response.
138
lr_data = /ui2/cl_json=>generate( json = lv_json ).
139
*
140
IF lr_data IS BOUND.
141
ASSIGN lr_data->* TO <data>.
142
ASSIGN COMPONENT 'MESSAGE' OF STRUCTURE <data> TO <field>.
143
IF <field> IS ASSIGNED.
144
lr_data = <field>.
145
ASSIGN lr_data->* TO <data>.
146
ex_mess = <data>.
147
ENDIF.
148
ENDIF.
149
*
150
ELSE.
151
*
152
/ui2/cl_json=>deserialize( EXPORTING json = lv_response pretty_name = /ui2/cl_json=>pretty_mode-camel_case CHANGING data = ls_response ).
153
ex_current_token = ls_response-currenttoken.
154
ex_time_used = ls_response-timeused.
155
ex_time_remaining = ls_response-timeremaining.
156
ex_previous_token = ls_response-previoustoken.
157
ex_next_token = ls_response-nexttoken.
158
*
159
ENDIF.
160
*
161
CALL METHOD lo_http_client->close
162
EXCEPTIONS
163
http_invalid_state = 1
164
OTHERS = 2.
165
*
166
FREE lo_http_client.
167
*
168
ENDFUNCTION.
Copied!

Send SMS

ABAP
1
FUNCTION zsend_sms.
2
*"----------------------------------------------------------------------
3
*"*"Local Interface:
4
*" IMPORTING
5
*" VALUE(IM_RFCDEST) TYPE RFCDEST
6
*" VALUE(IM_API_KEY) TYPE STRING
7
*" VALUE(IM_NUMBER) TYPE STRING
8
*" VALUE(IM_MESSAGE) TYPE STRING
9
*" VALUE(IM_SENDER_NAME) TYPE STRING OPTIONAL
10
*" EXPORTING
11
*" VALUE(EX_SUBRC) TYPE SYSUBRC
12
*" VALUE(EX_MESS) TYPE STRING
13
*"----------------------------------------------------------------------
14
*
15
DATA: lo_http_client TYPE REF TO if_http_client,
16
lv_http_rc TYPE i,
17
lv_response TYPE string,
18
lv_json TYPE /ui2/cl_json=>json,
19
lr_data TYPE REF TO data.
20
*
21
FIELD-SYMBOLS: <data> TYPE data,
22
<field> TYPE any.
23
*
24
* Setup IF_HTTP_Client object
25
*
26
CALL METHOD cl_http_client=>create_by_destination
27
EXPORTING
28
destination = im_rfcdest
29
IMPORTING
30
client = lo_http_client
31
EXCEPTIONS
32
argument_not_found = 1
33
plugin_not_active = 2
34
internal_error = 3
35
OTHERS = 4.
36
*
37
IF sy-subrc IS NOT INITIAL.
38
ex_mess = 'Error creating new IF_HTTP_Client object. Process aborted.'.
39
ex_subrc = 4.
40
RETURN.
41
ENDIF.
42
*
43
* Set protocol version
44
*
45
lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_1 ).
46
*
47
* Set request header fields
48
*
49
lo_http_client->request->set_header_field(
50
name = 'x-api-key'
51
value = im_api_key ).
52
*
53
* Set form fields/attributes
54
*
55
lo_http_client->request->if_http_entity~set_form_field(
56
name = 'number'
57
value = im_number ).
58
*
59
lo_http_client->request->if_http_entity~set_form_field(
60
name = 'message'
61
value = im_message ).
62
*
63
IF im_sender_name IS NOT INITIAL.
64
*
65
lo_http_client->request->if_http_entity~set_form_field(
66
name = 'senderName'
67
value = im_sender_name ).
68
*
69
ENDIF.
70
*
71
lo_http_client->request->set_method( 'POST' ).
72
*
73
lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.
74
*
75
* Send request
76
*
77
CALL METHOD lo_http_client->send
78
EXPORTING
79
timeout = 60 " 15 Seconds
80
EXCEPTIONS
81
http_communication_failure = 1
82
http_invalid_state = 2.
83
*
84
IF sy-subrc IS NOT INITIAL.
85
*
86
ex_mess = 'HTTP Post error. Process aborted'.
87
ex_subrc = 4.
88
*
89
CALL METHOD lo_http_client->close
90
EXCEPTIONS
91
http_invalid_state = 1
92
OTHERS = 2.
93
*
94
FREE lo_http_client.
95
RETURN.
96
*
97
ENDIF.
98
*
99
* Read the Response
100
*
101
CALL METHOD lo_http_client->receive
102
EXCEPTIONS
103
http_communication_failure = 1
104
http_invalid_state = 2
105
http_processing_failed = 3.
106
*
107
lo_http_client->response->get_status( IMPORTING code = lv_http_rc ).
108
lo_http_client->response->get_status( IMPORTING reason = ex_mess ).
109
lv_response = lo_http_client->response->get_cdata( ).
110
*
111
IF lv_http_rc NE '200'.
112
*
113
* Get error message back from Renda
114
*
115
ex_subrc = 4.
116
lv_json = lv_response.
117
lr_data = /ui2/cl_json=>generate( json = lv_json ).
118
*
119
IF lr_data IS BOUND.
120
ASSIGN lr_data->* TO <data>.
121
ASSIGN COMPONENT 'MESSAGE' OF STRUCTURE <data> TO <field>.
122
IF <field> IS ASSIGNED.
123
lr_data = <field>.
124
ASSIGN lr_data->* TO <data>.
125
ex_mess = <data>.
126
ENDIF.
127
ENDIF.
128
*
129
ENDIF.
130
*
131
CALL METHOD lo_http_client->close
132
EXCEPTIONS
133
http_invalid_state = 1
134
OTHERS = 2.
135
*
136
FREE lo_http_client.
137
*
138
ENDFUNCTION.
Copied!
Last modified 8mo ago