Skip to content

Commit 715a7b6

Browse files
committed
added_response_parsing
1 parent 3758f6f commit 715a7b6

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

sheets/src/eg_sheets_api.e

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ feature -- Access
4141
version: STRING_8
4242
-- Google Sheets version
4343

44+
spreadsheet_id: detachable STRING
45+
4446

4547
feature -- Spreedsheets
4648

@@ -54,6 +56,22 @@ feature -- Spreedsheets
5456
attached last_response as l_response and then
5557
attached l_response.body as l_body
5658
then
59+
parse_last_response
60+
Result := l_body
61+
end
62+
end
63+
64+
get_from_id (a_spreadsheet_id: attached like spreadsheet_id): detachable like last_response.body
65+
-- https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/get
66+
require
67+
not a_spreadsheet_id.is_empty
68+
do
69+
api_post_call (sheets_url ("spreadsheets/" + a_spreadsheet_id, Void ), Void, Void)
70+
check
71+
attached last_response as l_response and then
72+
attached l_response.body as l_body
73+
then
74+
parse_last_response
5775
Result := l_body
5876
end
5977
end
@@ -83,6 +101,39 @@ feature -- Parameters Factory
83101

84102
feature -- Error Report
85103

104+
parse_last_response
105+
require
106+
attached last_response
107+
local
108+
l_json_parser: JSON_PARSER
109+
do
110+
check
111+
attached last_response as l_response
112+
then
113+
if attached l_response.body as l_body then
114+
create l_json_parser.make_with_string (l_body)
115+
l_json_parser.parse_content
116+
if l_json_parser.is_valid then
117+
if attached {JSON_OBJECT} l_json_parser.parsed_json_value as l_main_jso then
118+
if attached {JSON_OBJECT} l_main_jso.item ("error") as l_error_jso then
119+
if attached {JSON_NUMBER} l_error_jso.item ("code") as l_jso then
120+
print ("parse_last_response-> error code:" + l_jso.representation)
121+
end
122+
if attached {JSON_STRING} l_error_jso.item ("message") as l_jso then
123+
print ("parse_last_response-> error message:" + l_jso.unescaped_string_8)
124+
end
125+
if attached {JSON_STRING} l_error_jso.item ("status") as l_jso then
126+
print ("parse_last_response-> error status:" + l_jso.unescaped_string_8)
127+
end
128+
end
129+
end
130+
else
131+
print ("parse_last_response-> Error: Invalid json body content:" + l_body + "%N")
132+
end
133+
end
134+
end
135+
end
136+
86137
has_error: BOOLEAN
87138
-- Last api call raise an error?
88139
do

sheets/test/test_sheets_api.e

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ feature -- {NONE}
2020
set_from_json_credentials_file_path (create {PATH}.make_from_string ("/home/pg/tmp/eg-sheets/eg-sheets_credentials_eg-suite-desktop-api.json"))
2121
retrieve_access_token
2222
test_create_sheet
23+
-- test_get_sheet ("1v1N4nRa6mmLcP9rUuyQPiCnLuUcBQFDEC7E0CDg3ASI")
2324
end
2425

2526

@@ -60,6 +61,39 @@ feature -- Tests
6061
end
6162
end
6263

64+
test_get_sheet (an_id: attached like {EG_SHEETS_API}.spreadsheet_id)
65+
local
66+
l_esapi: EG_SHEETS_API
67+
do
68+
create l_esapi.make (last_token.token)
69+
if attached l_esapi.get_from_id (an_id) as l_spreedsheet_get_result then
70+
if l_esapi.has_error then
71+
-- debug ("test_create_sheet")
72+
print ("test_create_sheet-> Error %N" )
73+
print ("test_create_sheet-> Error: msg:" + l_esapi.error_message)
74+
print ("test_create_sheet-> See codes here: https://developers.google.com/maps-booking/reference/rest-api-v3/status_codes")
75+
print ("%N")
76+
-- end
77+
check
78+
cannot_create_the_spreedsheet: False
79+
end
80+
else
81+
check Json_Field_spreadsheetId: l_spreedsheet_get_result.has_substring ("spreadsheetId") end
82+
check Json_Field_properties: l_spreedsheet_get_result.has_substring ("properties") end
83+
check Json_Field_sheets: l_spreedsheet_get_result.has_substring ("sheets") end
84+
check Json_Field_spreadsheetUrl: l_spreedsheet_get_result.has_substring ("spreadsheetUrl") end
85+
-- developerMetadata and namedRanges are optional.
86+
-- debug ("test_create_sheet")
87+
print ("test_get_sheet-> success. Result:%N")
88+
print (l_spreedsheet_get_result + "%N")
89+
-- end
90+
end
91+
else
92+
-- Bad scope. no connection, etc
93+
check Unexptected_Behavior: False end
94+
end
95+
end
96+
6397
test_create_sheet_json
6498
local
6599
l_esapi: EG_SHEETS_JSON

0 commit comments

Comments
 (0)