-
Notifications
You must be signed in to change notification settings - Fork 2
/
zr_qs_gmail_get_messages.prog.abap
127 lines (114 loc) · 5.26 KB
/
zr_qs_gmail_get_messages.prog.abap
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
**********************************************************************
* Copyright 2023 Google LLC *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* https://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, *
* either express or implied. *
* See the License for the specific language governing permissions *
* and limitations under the License. *
**********************************************************************
REPORT zr_qs_gmail_get_messages.
* Data declarations
DATA:
lt_message_parts TYPE /goog/cl_gmail_v1=>ty_t_038,
lt_message_sub_parts TYPE /goog/cl_gmail_v1=>ty_t_038,
lt_bin_data TYPE STANDARD TABLE OF char1024,
lv_p_id TYPE string,
lv_p_user_id TYPE string,
lv_xfile TYPE xstring,
lv_email_text TYPE string,
lv_file_length TYPE i.
FIELD-SYMBOLS:
<lt_message_parts> TYPE data.
TRY.
* Open HTTP Connection
DATA(lo_client) = NEW /goog/cl_gmail_v1( iv_key_name = 'TEST_GMAIL' ).
* Populate relevant parameters
lv_p_id = '<Message ID>'. "Pass the email Message ID here
lv_p_user_id = 'me'. "Short hand to pass your Gmail ID
* Call API method: gmail.users.messages.get
CALL METHOD lo_client->get_messages
EXPORTING
iv_p_id = lv_p_id
iv_p_user_id = lv_p_user_id
IMPORTING
es_output = DATA(ls_output)
ev_ret_code = DATA(lv_ret_code)
ev_err_text = DATA(lv_err_text)
es_err_resp = DATA(ls_err_resp).
IF lo_client->is_success( lv_ret_code ).
* Post processing to extract the response data reference to ABAP Type shipped with the SDK
* Serialize and deserialize response data in ABAP types embedded in the SDK
ASSIGN ls_output-payload-parts->* TO <lt_message_parts>.
DATA(lv_json) = /goog/cl_json_util=>serialize_json( is_data = <lt_message_parts> ).
/goog/cl_json_util=>deserialize_json( EXPORTING iv_json = lv_json
iv_pretty_name = /ui2/cl_json=>pretty_mode-extended
IMPORTING es_data = lt_message_parts ).
* Iterate internal table to browse through the message parts
LOOP AT lt_message_parts ASSIGNING FIELD-SYMBOL(<ls_message_part>).
* If file name is not present, it means that the message part is email body
IF <ls_message_part>-filename IS INITIAL.
* Serialize and deserialize response data in ABAP types embedded in the SDK
ASSIGN <ls_message_part>-parts TO <lt_message_parts>.
lv_json = /goog/cl_json_util=>serialize_json( is_data = <lt_message_parts> ).
/goog/cl_json_util=>deserialize_json( EXPORTING iv_json = lv_json
iv_pretty_name = /ui2/cl_json=>pretty_mode-extended
IMPORTING es_data = lt_message_sub_parts ).
READ TABLE lt_message_sub_parts ASSIGNING FIELD-SYMBOL(<ls_message_sub_part>)
WITH KEY mime_type = 'text/plain'.
IF sy-subrc EQ 0.
* Call function module to decode the Base64 encoded email text to xstring format
CALL FUNCTION 'SCMS_BASE64_DECODE_STR'
EXPORTING
input = <ls_message_sub_part>-body-data
unescape = 'X'
IMPORTING
output = lv_xfile
EXCEPTIONS
failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
* Call function module to covert email text from xstring to binary format
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_xfile
IMPORTING
output_length = lv_file_length
TABLES
binary_tab = lt_bin_data.
* Call function module to covert email text from binary to string format
CALL FUNCTION 'SCMS_BINARY_TO_STRING'
EXPORTING
input_length = lv_file_length
IMPORTING
text_buffer = lv_email_text
TABLES
binary_tab = lt_bin_data.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
* Display email text as output
cl_demo_output=>display( lv_email_text ).
ENDIF.
ELSE.
* Display read Attachment ID as output
WRITE:
'Attachment ID: ',
<ls_message_part>-body-attachment_id.
ENDIF.
ENDLOOP.
ELSE.
MESSAGE lv_err_text TYPE 'E'.
ENDIF.
* Close HTTP Connection
lo_client->close( ).
CATCH /goog/cx_sdk INTO DATA(lo_exception).
MESSAGE lo_exception->get_text( ) TYPE 'E'.
ENDTRY.