55
66class
77 EG_SHEETS_API
8+ inherit
9+ LOGGABLE
10+
811create
912 make
1013
@@ -41,8 +44,11 @@ feature -- Access
4144 version : STRING_ 8
4245 -- Google Sheets version
4346
44-
4547feature -- Spreedsheets
48+ spreadsheet_id : detachable STRING
49+
50+
51+ feature -- Spreedsheets Operations
4652
4753 create_spreedsheet : detachable STRING
4854 -- POST /spreadsheets
@@ -58,12 +64,104 @@ feature -- Spreedsheets
5864 end
5965 end
6066
67+ get_from_id (a_spreadsheet_id : attached like spreadsheet_id ): detachable like last_response .body
68+ -- POST /spreadsheets/`a_spreadsheet_id'
69+ note
70+ EIS :" name=get.spreedsheets" , " src=https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/get" , " protocol=uri"
71+ require
72+ not a_spreadsheet_id .is_empty
73+ local
74+ l_file : PLAIN_TEXT_FILE
75+ l_qry_params : STRING_TABLE [STRING ]
76+ do
77+
78+ logger .write_information (" get_from_id-> Now getting sheet from id:" + a_spreadsheet_id )
79+ create l_qry_params .make (2 )
80+ l_qry_params .extend (" true" , " includeGridData" ) -- all content
81+ -- l_params.extend ("sheets.properties", "fields") -- properties only
82+
83+ api_get_call (sheets_url (" spreadsheets/" + a_spreadsheet_id , Void ), l_qry_params )
84+ check
85+ attached last_response as l_response and then
86+ attached l_response .body as l_body
87+ then
88+ parse_last_response
89+ if l_response .status = {HTTP_STATUS_CODE }.ok then
90+ Result := l_body
91+
92+ create l_file .make_create_read_write (" /tmp/hitme_sheet_json-get_from_id.json" )
93+ logger .write_information (" get_from_id->Writing body into " + l_file .path .utf_ 8 _name )
94+ l_file .close
95+ l_file .wipe_out
96+ l_file .open_append
97+
98+ l_file .put_string (l_body )
99+ l_file .close
100+ elseif l_response .status = {HTTP_STATUS_CODE }.not_found then
101+ logger .write_error (" get_from_id-> Not found:" + l_response .status .out + " %NBody: " + l_body )
102+ else
103+ logger .write_error (" get_from_id-> Status code invalid:" + l_response .status .out + " %NBody: " + l_body )
104+ end
105+ end
106+ end
107+
108+ append (a_spreadsheet_id : attached like spreadsheet_id ; a_data_line : ARRAY [STRING ]): detachable like last_response .body
109+ note
110+ EIS :" name=append.spreedsheets" , " src=https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append" , " protocol=uri"
111+ require
112+ not a_spreadsheet_id .is_empty
113+ local
114+ l_file : PLAIN_TEXT_FILE
115+ l_range ,
116+ l_path_params_s : STRING
117+ l_qry_params : STRING_TABLE [STRING ]
118+ do
119+ l_range := " "
120+ logger .write_information (" append-> spreadsheed_id:" + a_spreadsheet_id )
121+ -- path params
122+ l_path_params_s := a_spreadsheet_id
123+ l_path_params_s .append (" /values/{" ) -- spreadsheets/{spreadsheetId}/values/{range}:append
124+ l_path_params_s .append (" " ) -- range ex. A1:B2 or namedRanges TRY: last not null index could be: =index(J:J,max(row(J:J)*(J:J<>"")))
125+ l_path_params_s .append (" }:append" )
126+ -- qry params
127+ create l_qry_params .make (2 )
128+ l_qry_params .extend (" RAW" , " valueInputOption" ) -- INPUT_VALUE_OPTION_UNSPECIFIED|RAW|USER_ENTERED https://developers.google.com/sheets/api/reference/rest/v4/ValueInputOption
129+ l_qry_params .extend (" INSERT_ROWS" , " insertDataOption" ) -- OVERWRITE|INSERT_ROWS https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append#InsertDataOption
130+ l_qry_params .extend (" true" , " includeValuesInResponse" ) -- BOOLEAN
131+ l_qry_params .extend (" true" , " responseValueRenderOption" ) -- FORMATTED_VALUE| https://developers.google.com/sheets/api/reference/rest/v4/ValueRenderOption
132+
133+ api_get_call (sheets_url (" spreadsheets/" + l_path_params_s , Void ), l_qry_params )
134+ check
135+ attached last_response as l_response and then
136+ attached l_response .body as l_body
137+ then
138+ parse_last_response
139+ if l_response .status = {HTTP_STATUS_CODE }.ok then
140+ Result := l_body
141+
142+ create l_file .make_create_read_write (" /tmp/hitme_sheet_json-append.json" )
143+ logger .write_information (" get_from_id->Writing body into " + l_file .path .utf_ 8 _name )
144+ l_file .close
145+ l_file .wipe_out
146+ l_file .open_append
147+
148+ l_file .put_string (l_body )
149+ l_file .close
150+ elseif l_response .status = {HTTP_STATUS_CODE }.not_found then
151+ logger .write_error (" get_from_id-> Not found:" + l_response .status .out + " %NBody: " + l_body )
152+ else
153+ logger .write_error (" get_from_id-> Status code invalid:" + l_response .status .out + " %NBody: " + l_body )
154+ end
155+ end
156+ end
157+
61158feature -- Parameters Factory
62159
63160 parameters (a_params : detachable STRING_TABLE [STRING ] ): detachable ARRAY [detachable TUPLE [name : STRING ; value : STRING ]]
161+ -- @JV please add a call example
64162 local
65- l_result : detachable ARRAY [ detachable TUPLE [ name : STRING ; value : STRING ]]
66- l_tuple : detachable TUPLE [ name : STRING ; value : STRING ]
163+ l_result : like parameters
164+ l_tuple : like parameters . item
67165 i : INTEGER
68166 do
69167 if attached a_params then
@@ -83,6 +181,43 @@ feature -- Parameters Factory
83181
84182feature -- Error Report
85183
184+ parse_last_response
185+ require
186+ attached last_response
187+ local
188+ l_json_parser : JSON_PARSER
189+ do
190+ check
191+ attached last_response as l_response
192+ then
193+ if l_response .status = {HTTP_STATUS_CODE }.unauthorized then
194+ logger .write_error (" parse_last_response->Unauthorized status, review your authorization credentials" )
195+ end
196+ if attached l_response .body as l_body then
197+ logger .write_debug (" parse_last_response->body:" + l_body )
198+ create l_json_parser .make_with_string (l_body )
199+ l_json_parser .parse_content
200+ if l_json_parser .is_valid then
201+ if attached {JSON_OBJECT } l_json_parser .parsed_json_value as l_main_jso then
202+ if attached {JSON_OBJECT } l_main_jso .item (" error" ) as l_error_jso then
203+ if attached {JSON_NUMBER } l_error_jso .item (" code" ) as l_jso then
204+ print (" parse_last_response-> error code:" + l_jso .representation + " %N" )
205+ end
206+ if attached {JSON_STRING } l_error_jso .item (" message" ) as l_jso then
207+ print (" parse_last_response-> error message:" + l_jso .unescaped_string_ 8 + " %N" )
208+ end
209+ if attached {JSON_STRING } l_error_jso .item (" status" ) as l_jso then
210+ print (" parse_last_response-> error status:" + l_jso .unescaped_string_ 8 + " %N" )
211+ end
212+ end
213+ end
214+ else
215+ print (" parse_last_response-> Error: Invalid json body content:" + l_body + " %N" )
216+ end
217+ end
218+ end
219+ end
220+
86221 has_error : BOOLEAN
87222 -- Last api call raise an error?
88223 do
@@ -152,6 +287,7 @@ feature {NONE} -- Implementation
152287 api_service : OAUTH_ 20 _SERVICE
153288 config : OAUTH_CONFIG
154289 do
290+ logger .write_debug (" internal_api_call-> a_api_url:" + a_api_url + " method:" + a_method )
155291 -- TODO improve this, so we can check the required scopes before we
156292 -- do an api call.
157293 create config .make_default (" " , " " )
@@ -169,10 +305,10 @@ feature {NONE} -- Implementation
169305 create l_access_token .make_token_secret (access_token , " NOT_NEEDED" )
170306 -- | Todo improve access_token to create a token without a secret.
171307 if attached l_access_token as ll_access_token then
172- print (" %NGot the Access Token!%N " );
308+ logger . write_information (" internal_api_call->Got the Access Token: " + ll_access_token . token );
173309
174310 -- Now let's go and check if the request is signed correcty
175- print (" %NNow we're going to verify our credentials...%N" );
311+ logger . write_information (" internal_api_call->Now we're going to verify our credentials...%N" );
176312 -- Build the request and authorize it with OAuth.
177313 create request .make (a_method , a_api_url )
178314 -- Workaorund to make it work with Google API
0 commit comments