@@ -56,7 +56,7 @@ feature -- Spreedsheets Operations
5656 note
5757 EIS :" name=create.spreedsheets" , " src=https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/create" , " protocol=uri"
5858 do
59- api_post_call (sheets_url (" spreadsheets" , Void ), Void , Void )
59+ api_post_call (sheets_url (" spreadsheets" , Void ), Void , Void , Void )
6060 if
6161 attached last_response as l_response and then
6262 attached l_response .body as l_body
@@ -117,45 +117,52 @@ feature -- Spreedsheets Operations
117117 l_range ,
118118 l_path_params_s : STRING
119119 l_qry_params : STRING_TABLE [STRING ]
120- l_post_data : TUPLE [data :PATH ; content_type : STRING ]
120+ l_post_data : STRING -- TUPLE[data:PATH; content_type: STRING]
121+ url_encoder : URL_ENCODER
121122 do
122123 l_range := " "
123124 logger .write_information (" append-> spreadsheed_id:" + a_spreadsheet_id )
124125 -- path params
125126 l_path_params_s := a_spreadsheet_id
126127 l_path_params_s .append (" /values/" ) -- spreadsheets/{spreadsheetId}/values/{range}:append
127128
128- l_path_params_s .append (" Sheet1!A:A" ) -- range ex. A1:B2 or namedRanges TRY: Sheet1!A:A | last not null index could be: =index(J:J,max(row(J:J)*(J:J<>"")))
129+ -- TODO add url encode to the query parameters.
130+ create url_encoder
131+ l_path_params_s .append (url_encoder .encoded_string (" Sheet1!A1:B5" )) -- range ex. A1:B2 or namedRanges TRY: Sheet1!A:A | last not null index could be: =index(J:J,max(row(J:J)*(J:J<>"")))
129132
130133 l_path_params_s .append (" :append" )
131134 -- qry params
132135 create l_qry_params .make (2 )
133136 l_qry_params .extend (" RAW" , " valueInputOption" ) -- INPUT_VALUE_OPTION_UNSPECIFIED|RAW|USER_ENTERED https://developers.google.com/sheets/api/reference/rest/v4/ValueInputOption
134- l_qry_params .extend (" INSERT_ROWS" , " insertDataOption" ) -- OVERWRITE|INSERT_ROWS https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append#InsertDataOption
135- l_qry_params .extend (" true" , " includeValuesInResponse" ) -- BOOLEAN
136- l_qry_params .extend (" UNFORMATTED_VALUE" , " responseValueRenderOption" ) -- UNFORMATTED_VALUE|FORMULA|FORMATTED_VALUE https://developers.google.com/sheets/api/reference/rest/v4/ValueRenderOption
137- l_qry_params .extend (" SERIAL_NUMBER" , " responseDateTimeRenderOption" ) -- SERIAL_NUMBER|FORMATTED_STRING https://developers.google.com/sheets/api/reference/rest/v4/DateTimeRenderOption
137+ -- l_qry_params.extend ("INSERT_ROWS", "insertDataOption") -- OVERWRITE|INSERT_ROWS https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append#InsertDataOption
138+ -- l_qry_params.extend ("true", "includeValuesInResponse") -- BOOLEAN
139+ -- l_qry_params.extend ("json", "alt") -- BOOLEAN
140+ -- alt=json
141+ -- l_qry_params.extend ("UNFORMATTED_VALUE", "responseValueRenderOption") -- UNFORMATTED_VALUE|FORMULA|FORMATTED_VALUE https://developers.google.com/sheets/api/reference/rest/v4/ValueRenderOption
142+ -- l_qry_params.extend ("SERIAL_NUMBER", "responseDateTimeRenderOption") -- SERIAL_NUMBER|FORMATTED_STRING https://developers.google.com/sheets/api/reference/rest/v4/DateTimeRenderOption
138143
139- l_post_data := impl_append_post_data
140144
141- api_post_call (sheets_url (" spreadsheets/" + l_path_params_s , Void ), l_qry_params , l_post_data )
145+ l_post_data := impl_append_post_data 2
146+
147+ -- Google API append require body parameter instead of upload data.
148+ api_post_call (sheets_url (" spreadsheets/" + l_path_params_s , Void ), l_qry_params , l_post_data , Void )
149+
142150 check
143151 attached last_response as l_response
144152 attached l_response .body as l_body
145- attached data_file as l_data_file
146153 then
147154 parse_last_response
148155 if l_response .status = {HTTP_STATUS_CODE }.ok then
149156 Result := l_body
150157
151- create l_file .make_create_read_write (" /tmp/hitme_sheet_json-append.json" )
152- logger .write_information (" get_from_id->Writing body into " + l_file .path .utf_ 8 _name )
153- l_file .close
154- l_file .wipe_out
155- l_file .open_append
158+ -- create l_file.make_create_read_write ("/tmp/hitme_sheet_json-append.json")
159+ -- logger.write_information ("get_from_id->Writing body into " + l_file.path.utf_8_name)
160+ -- l_file.close
161+ -- l_file.wipe_out
162+ -- l_file.open_append
156163
157- l_file .put_string (l_body )
158- l_file .close
164+ -- l_file.put_string (l_body)
165+ -- l_file.close
159166 elseif l_response .status = {HTTP_STATUS_CODE }.not_found then
160167 logger .write_error (" get_from_id-> Not found:" + l_response .status .out + " %NBody: " + l_body )
161168 else
@@ -270,25 +277,25 @@ feature -- Versions
270277
271278feature {NONE } -- Implementation
272279
273- api_post_call (a_api_url : STRING ; a_params : detachable STRING_TABLE [STRING ]; a_upload_data : detachable TUPLE [data :PATH ; content_type : STRING ])
280+ api_post_call (a_api_url : STRING ; a_params : detachable STRING_TABLE [STRING ]; a_payload : detachable STRING ; a_upload_data : detachable TUPLE [data :PATH ; content_type : STRING ])
274281 -- POST REST API call for `a_api_url'
275282 do
276- internal_api_call (a_api_url , " POST" , a_params , a_upload_data )
283+ internal_api_call (a_api_url , " POST" , a_params , a_payload , a_upload_data )
277284 end
278285
279286 api_delete_call (a_api_url : STRING ; a_params : detachable STRING_TABLE [STRING ])
280287 -- DELETE REST API call for `a_api_url'
281288 do
282- internal_api_call (a_api_url , " DELETE" , a_params , Void )
289+ internal_api_call (a_api_url , " DELETE" , a_params , Void , Void )
283290 end
284291
285292 api_get_call (a_api_url : STRING ; a_params : detachable STRING_TABLE [STRING ])
286293 -- GET REST API call for `a_api_url'
287294 do
288- internal_api_call (a_api_url , " GET" , a_params , Void )
295+ internal_api_call (a_api_url , " GET" , a_params , Void , Void )
289296 end
290297
291- internal_api_call (a_api_url : STRING ; a_method : STRING ; a_params : detachable STRING_TABLE [STRING ]; a_upload_data : detachable TUPLE [filename :PATH ; content_type : STRING ])
298+ internal_api_call (a_api_url : STRING ; a_method : STRING ; a_params : detachable STRING_TABLE [STRING ]; a_payload : detachable STRING ; a_upload_data : detachable TUPLE [filename :PATH ; content_type : STRING ])
292299 note
293300 EIS :" name=access token" , " src=https://developers.google.com/identity/protocols/oauth2" , " protocol=uri"
294301 local
@@ -324,9 +331,16 @@ feature {NONE} -- Implementation
324331 -- Workaorund to make it work with Google API
325332 -- in other case it return HTTP 411 Length Required.
326333 -- Todo check.
327- request .add_header (" Content-length" , " 0" )
328334 upload_data (a_method , request , a_upload_data )
329335 add_parameters (a_method , request , a_params )
336+ -- adding payload
337+ if attached a_payload then
338+ request .add_header (" Content-length" , a_payload .count .out )
339+ request .add_header (" Content-Type" , " application/json; charset=UTF-8" )
340+ request .add_payload (a_payload )
341+ else
342+ request .add_header (" Content-length" , " " )
343+ end
330344
331345 api_service .sign_request (ll_access_token , request )
332346
@@ -499,5 +513,82 @@ feature {NONE} -- Implementation
499513 attached data_file
500514 end
501515
516+ impl_append_post_data 2 : STRING
517+ local
518+ l_res : JSON_OBJECT
519+ l_jsa_main ,
520+ l_jsa_line : JSON_ARRAY
521+ j_array : JSON_ARRAY
522+
523+ -- {
524+ -- "range": string,
525+ -- "majorDimension": enum (Dimension),
526+ -- "values": [
527+ -- array
528+ -- ]
529+ -- }
530+ -- // "values": [
531+ -- // [
532+ -- // "Item",
533+ -- // "Cost"
534+ -- // ],
535+ -- // [
536+ -- // "Wheel",
537+ -- // "$20.50"
538+ -- // ],
539+ -- // [
540+ -- // "Door",
541+ -- // "$15"
542+ -- // ],
543+ -- // [
544+ -- // "Engine",
545+ -- // "$100"
546+ -- // ],
547+ -- // [
548+ -- // "Totals",
549+ -- // "$135.50"
550+ -- // ]
551+ -- // ]
552+
553+ do
554+ create l_res .make_with_capacity (5 )
555+ l_res .put_string (" Sheet1!A1:B5" , " range" )
556+ l_res .put_string (" ROWS" , " majorDimension" ) -- "DIMENSION_UNSPECIFIED", "ROWS", "COLUMNS"
557+
558+ create l_jsa_main .make (10 )
559+
560+ create j_array .make (1 )
561+ create l_jsa_line .make (2 )
562+ l_jsa_line .extend (create {JSON_STRING }.make_from_string (" Item" ))
563+ l_jsa_line .extend (create {JSON_STRING }.make_from_string (" Cost" ))
564+ j_array .add (l_jsa_line )
565+
566+ create l_jsa_line .make (2 )
567+ l_jsa_line .extend (create {JSON_STRING }.make_from_string (" Wheel" ))
568+ l_jsa_line .extend (create {JSON_STRING }.make_from_string (" $20.50" ))
569+ j_array .add (l_jsa_line )
570+
571+ create l_jsa_line .make (2 )
572+ l_jsa_line .extend (create {JSON_STRING }.make_from_string (" Door" ))
573+ l_jsa_line .extend (create {JSON_STRING }.make_from_string (" $15" ))
574+ j_array .add (l_jsa_line )
575+
576+ create l_jsa_line .make (2 )
577+ l_jsa_line .extend (create {JSON_STRING }.make_from_string (" Engine" ))
578+ l_jsa_line .extend (create {JSON_STRING }.make_from_string (" $100" ))
579+ j_array .add (l_jsa_line )
580+
581+ create l_jsa_line .make (2 )
582+ l_jsa_line .extend (create {JSON_STRING }.make_from_string (" Totals" ))
583+ l_jsa_line .extend (create {JSON_STRING }.make_from_string (" $135.50" ))
584+ j_array .add (l_jsa_line )
585+
586+
587+ l_res .put (j_array , " values" )
588+
589+ Result := l_res .representation
590+ logger .write_debug (" impl_append_body-> Result: '" + Result .out + " '" )
591+ end
592+
502593end
503594
0 commit comments