mirror of
https://github.com/ToolJet/ToolJet
synced 2026-05-23 08:58:26 +00:00
[Google sheets] Show proper error messages
This commit is contained in:
parent
3c98d53aa7
commit
3b87d0e26c
2 changed files with 45 additions and 18 deletions
|
|
@ -42,7 +42,6 @@ class DataQueriesController < ApplicationController
|
|||
|
||||
query_service = QueryService.new @data_query, params[:options], @current_user
|
||||
result = query_service.process
|
||||
|
||||
render json: result
|
||||
render json: result, status: result[:code] || 200
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
class GooglesheetsQueryService
|
||||
attr_accessor :query, :ource, :options, :source_options, :current_user
|
||||
attr_accessor :query, :source, :options, :source_options, :current_user
|
||||
|
||||
def initialize(data_query, options, source_options, current_user)
|
||||
@query = data_query
|
||||
|
|
@ -12,28 +12,56 @@ class GooglesheetsQueryService
|
|||
def process
|
||||
operation = query.options['operation']
|
||||
access_token = source_options['access_token']
|
||||
error = false
|
||||
|
||||
if operation === 'read'
|
||||
result = read_data(access_token)
|
||||
|
||||
if result.code === 401
|
||||
refresh_access_token
|
||||
result = read_data(access_token)
|
||||
end
|
||||
|
||||
if result.code === 200
|
||||
|
||||
headers = result['values'][0]
|
||||
values = result['values'][1..]
|
||||
|
||||
data = []
|
||||
values.each do |value|
|
||||
row = {}
|
||||
headers.each_with_index do |header, index|
|
||||
row[header] = value[index]
|
||||
end
|
||||
parsed_values << row
|
||||
end
|
||||
|
||||
else
|
||||
error = true
|
||||
data = result["error"]
|
||||
end
|
||||
end
|
||||
|
||||
if error
|
||||
{ status: 'error', code: 500, message: data["message"], data: data }
|
||||
else
|
||||
{ status: 'success', data: data }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def read_data(access_token)
|
||||
spreadsheet_id = query.options['spreadsheet_id']
|
||||
sheet = query.options['sheet']
|
||||
|
||||
result = HTTParty.get("https://sheets.googleapis.com/v4/spreadsheets/#{spreadsheet_id}/values/#{sheet}!A1:V101",
|
||||
headers: { 'Content-Type':
|
||||
'application/json', "Authorization": "Bearer #{access_token}" })
|
||||
headers: { 'Content-Type':
|
||||
'application/json', "Authorization": "Bearer #{access_token}" })
|
||||
|
||||
headers = result['values'][0]
|
||||
values = result['values'][1..]
|
||||
|
||||
parsed_values = []
|
||||
values.each do |value|
|
||||
row = {}
|
||||
headers.each_with_index do |header, index|
|
||||
row[header] = value[index]
|
||||
end
|
||||
parsed_values << row
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
{ status: 'success', data: parsed_values }
|
||||
end
|
||||
def refresh_access_token
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue