diff --git a/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/GoogleSheetsMethodStrategy.java b/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/GoogleSheetsMethodStrategy.java index 6908f4d006..87cbf3e203 100644 --- a/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/GoogleSheetsMethodStrategy.java +++ b/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/GoogleSheetsMethodStrategy.java @@ -65,6 +65,8 @@ public class GoogleSheetsMethodStrategy { return new FileInfoMethod(objectMapper); case MethodIdentifiers.TRIGGER_COLUMNS_SELECTOR: return new GetStructureMethod(objectMapper); + case MethodIdentifiers.TRIGGER_SHEET_DATA: + return new RowsGetMethod(objectMapper); default: throw Exceptions.propagate(new AppsmithPluginException( AppsmithPluginError.PLUGIN_EXECUTE_ARGUMENT_ERROR, diff --git a/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/MethodConfig.java b/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/MethodConfig.java index e3edd66e78..92807f509c 100644 --- a/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/MethodConfig.java +++ b/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/MethodConfig.java @@ -25,6 +25,7 @@ import static com.appsmith.external.helpers.PluginUtils.getTrimmedStringDataValu import static com.appsmith.external.helpers.PluginUtils.getValueSafelyFromFormDataAsString; import static com.appsmith.external.helpers.PluginUtils.parseWhereClause; import static com.appsmith.external.helpers.PluginUtils.validDataConfigurationPresentInFormData; +import static com.external.constants.FieldName.QUERY_FORMAT; import static com.external.constants.FieldName.SHEET_NAME; import static com.external.constants.FieldName.SHEET_URL; import static com.external.constants.FieldName.TABLE_HEADER_INDEX; @@ -100,6 +101,8 @@ public class MethodConfig { public MethodConfig(TriggerRequestDTO triggerRequestDTO) { final Map parameters = triggerRequestDTO.getParameters(); switch (parameters.size()) { + case 4: + this.queryFormat = getValueSafelyFromFormDataAsString(parameters, QUERY_FORMAT); case 3: case 2: this.tableHeaderIndex = getValueSafelyFromFormDataAsString(parameters, TABLE_HEADER_INDEX); diff --git a/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/MethodIdentifiers.java b/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/MethodIdentifiers.java index 81bfd607b6..6291d81bde 100644 --- a/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/MethodIdentifiers.java +++ b/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/MethodIdentifiers.java @@ -20,4 +20,5 @@ public class MethodIdentifiers { public static final String TRIGGER_SPREADSHEET_SELECTOR = "SPREADSHEET_SELECTOR"; public static final String TRIGGER_SHEET_SELECTOR = "SHEET_SELECTOR"; public static final String TRIGGER_COLUMNS_SELECTOR = "COLUMNS_SELECTOR"; + public static final String TRIGGER_SHEET_DATA = "SHEET_DATA"; } diff --git a/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/RowsGetMethod.java b/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/RowsGetMethod.java index 49fdc3e19b..bf007ac06b 100644 --- a/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/RowsGetMethod.java +++ b/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/RowsGetMethod.java @@ -29,7 +29,7 @@ import java.util.regex.Pattern; * API reference: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get */ @Slf4j -public class RowsGetMethod implements ExecutionMethod, TemplateMethod { +public class RowsGetMethod implements ExecutionMethod, TemplateMethod, TriggerMethod { ObjectMapper objectMapper; FilterDataService filterDataService; @@ -193,6 +193,22 @@ public class RowsGetMethod implements ExecutionMethod, TemplateMethod { return preFilteringResponse; } + @Override + public boolean validateTriggerMethodRequest(MethodConfig methodConfig) { + return this.validateExecutionMethodRequest(methodConfig); + } + + @Override + public WebClient.RequestHeadersSpec getTriggerClient(WebClient webClient, MethodConfig methodConfig) { + return this.getExecutionClient(webClient, methodConfig); + } + + @Override + public JsonNode transformTriggerResponse( + JsonNode response, MethodConfig methodConfig, Set userAuthorizedSheetIds) { + return this.transformExecutionResponse(response, methodConfig, userAuthorizedSheetIds); + } + private Set sanitizeHeaders(ArrayNode headers, int valueSize) { final Set headerSet = new LinkedHashSet<>(); int headerSize = headers.size(); diff --git a/app/server/appsmith-plugins/googleSheetsPlugin/src/test/java/com/external/config/RowsGetMethodTest.java b/app/server/appsmith-plugins/googleSheetsPlugin/src/test/java/com/external/config/RowsGetMethodTest.java index 716afcd65a..58542eec50 100644 --- a/app/server/appsmith-plugins/googleSheetsPlugin/src/test/java/com/external/config/RowsGetMethodTest.java +++ b/app/server/appsmith-plugins/googleSheetsPlugin/src/test/java/com/external/config/RowsGetMethodTest.java @@ -1,6 +1,7 @@ package com.external.config; import com.appsmith.external.exceptions.pluginExceptions.AppsmithPluginException; +import com.appsmith.external.models.TriggerRequestDTO; import com.external.constants.ErrorMessages; import com.external.constants.FieldName; import com.fasterxml.jackson.core.JsonProcessingException; @@ -8,10 +9,12 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.Test; +import java.util.HashMap; import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; public class RowsGetMethodTest { @@ -168,4 +171,76 @@ public class RowsGetMethodTest { assertEquals(3, result.size()); assertEquals(0, result.get(0).get(FieldName.ROW_INDEX).asInt()); } + + @Test + public void testValidateExecutionMethodRequest_noSpreadsheetId_returnsException() throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + RowsGetMethod rowsGetMethod = new RowsGetMethod(objectMapper); + TriggerRequestDTO triggerRequest = new TriggerRequestDTO(); + Map params = new HashMap(); + + triggerRequest.setParameters(params); + + MethodConfig methodConfig = new MethodConfig(triggerRequest); + final AppsmithPluginException exception = assertThrows( + AppsmithPluginException.class, () -> rowsGetMethod.validateExecutionMethodRequest(methodConfig)); + + assertEquals("Missing required field 'Spreadsheet Url'", exception.getMessage()); + } + + @Test + public void testValidateExecutionMethodRequest_noSheetName_returnsException() throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + RowsGetMethod rowsGetMethod = new RowsGetMethod(objectMapper); + TriggerRequestDTO triggerRequest = new TriggerRequestDTO(); + Map params = new HashMap(); + params.put("sheetUrl", "https://docs.google.com/spreadsheets/d/spreadsheetId/"); + + triggerRequest.setParameters(params); + + MethodConfig methodConfig = new MethodConfig(triggerRequest); + final AppsmithPluginException exception = assertThrows( + AppsmithPluginException.class, () -> rowsGetMethod.validateExecutionMethodRequest(methodConfig)); + + assertEquals("Missing required field 'Spreadsheet Name'", exception.getMessage()); + } + + @Test + public void testValidateExecutionMethodRequest_noTableHeaderIndex_returnsException() + throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + RowsGetMethod rowsGetMethod = new RowsGetMethod(objectMapper); + TriggerRequestDTO triggerRequest = new TriggerRequestDTO(); + Map params = new HashMap(); + params.put("sheetUrl", "https://docs.google.com/spreadsheets/d/spreadsheetId/"); + params.put("queryFormat", "ROWS"); + params.put("sheetName", "sample_sheet_name"); + + triggerRequest.setParameters(params); + + MethodConfig methodConfig = new MethodConfig(triggerRequest); + final AppsmithPluginException exception = assertThrows( + AppsmithPluginException.class, () -> rowsGetMethod.validateExecutionMethodRequest(methodConfig)); + + assertEquals( + "Unexpected value for table header index. Please use a number starting from 1", exception.getMessage()); + } + + @Test + public void testValidateExecutionMethodRequest_allParamsPresent_returnsTrue() throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + RowsGetMethod rowsGetMethod = new RowsGetMethod(objectMapper); + TriggerRequestDTO triggerRequest = new TriggerRequestDTO(); + Map params = new HashMap(); + params.put("sheetUrl", "https://docs.google.com/spreadsheets/d/spreadsheetId/"); + params.put("queryFormat", "ROWS"); + params.put("sheetName", "sample_sheet_name"); + params.put("tableHeaderIndex", 1); + + triggerRequest.setParameters(params); + + MethodConfig methodConfig = new MethodConfig(triggerRequest); + Boolean actualResult = rowsGetMethod.validateExecutionMethodRequest(methodConfig); + assertEquals(true, actualResult); + } }