From b618bfc5d8a26c6cd7684ee5c5d379dc2b9b47bf Mon Sep 17 00:00:00 2001 From: Arpit Mohan Date: Tue, 17 Sep 2019 12:24:45 +0000 Subject: [PATCH] Adding a very initial version of the RestApiPlugin Requires a bunch of testing for corner cases and error scenarios. --- app/server/appsmith-plugins/pom.xml | 1 + .../appsmith-plugins/postgresPlugin/pom.xml | 4 +- .../appsmith-plugins/restApiPlugin/pom.xml | 85 +++++++++++++++++++ .../com/external/plugins/RestApiPlugin.java | 83 ++++++++++++++++++ .../external/plugins/RestApiPluginTest.java | 20 +++++ 5 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 app/server/appsmith-plugins/restApiPlugin/pom.xml create mode 100644 app/server/appsmith-plugins/restApiPlugin/src/main/java/com/external/plugins/RestApiPlugin.java create mode 100644 app/server/appsmith-plugins/restApiPlugin/src/test/java/com/external/plugins/RestApiPluginTest.java diff --git a/app/server/appsmith-plugins/pom.xml b/app/server/appsmith-plugins/pom.xml index 758a89dd77..6920e23634 100644 --- a/app/server/appsmith-plugins/pom.xml +++ b/app/server/appsmith-plugins/pom.xml @@ -16,6 +16,7 @@ postgresPlugin + restApiPlugin \ No newline at end of file diff --git a/app/server/appsmith-plugins/postgresPlugin/pom.xml b/app/server/appsmith-plugins/postgresPlugin/pom.xml index ef801162d1..1e3e069f8d 100644 --- a/app/server/appsmith-plugins/postgresPlugin/pom.xml +++ b/app/server/appsmith-plugins/postgresPlugin/pom.xml @@ -17,8 +17,8 @@ ${java.version} postgres-plugin com.external.plugins.PostgresPlugin - 0.0.1 - Arpit Mohan + 1.0-SNAPSHOT + tech@appsmith.com diff --git a/app/server/appsmith-plugins/restApiPlugin/pom.xml b/app/server/appsmith-plugins/restApiPlugin/pom.xml new file mode 100644 index 0000000000..36d180dd87 --- /dev/null +++ b/app/server/appsmith-plugins/restApiPlugin/pom.xml @@ -0,0 +1,85 @@ + + + + 4.0.0 + + com.external.plugins + restApiPlugin + 1.0-SNAPSHOT + + restApiPlugin + + + UTF-8 + 11 + ${java.version} + ${java.version} + restapi-plugin + com.external.plugins.RestApiPlugin + 1.0-SNAPSHOT + tech@appsmith.com + + + + + + junit + junit + 4.11 + test + + + + org.pf4j + pf4j-spring + 0.5.0 + + + + com.appsmith + interfaces + 1.0-SNAPSHOT + + + + org.springframework + spring-webflux + 5.1.5.RELEASE + + + + org.projectlombok + lombok + 1.18.8 + provided + + + + + + + + maven-compiler-plugin + 3.8.1 + + + org.apache.maven.plugins + maven-jar-plugin + 3.1.2 + + + + ${plugin.id} + ${plugin.class} + ${plugin.version} + ${plugin.provider} + ${plugin.dependencies} + + + + + + + + diff --git a/app/server/appsmith-plugins/restApiPlugin/src/main/java/com/external/plugins/RestApiPlugin.java b/app/server/appsmith-plugins/restApiPlugin/src/main/java/com/external/plugins/RestApiPlugin.java new file mode 100644 index 0000000000..a0a4327053 --- /dev/null +++ b/app/server/appsmith-plugins/restApiPlugin/src/main/java/com/external/plugins/RestApiPlugin.java @@ -0,0 +1,83 @@ +package com.external.plugins; + +import com.appsmith.external.models.ActionConfiguration; +import com.appsmith.external.models.Param; +import com.appsmith.external.models.ResourceConfiguration; +import com.appsmith.external.plugins.BasePlugin; +import com.appsmith.external.plugins.PluginExecutor; +import lombok.extern.slf4j.Slf4j; +import org.json.JSONObject; +import org.pf4j.Extension; +import org.pf4j.PluginWrapper; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.client.ClientResponse; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class RestApiPlugin extends BasePlugin { + + public RestApiPlugin(PluginWrapper wrapper) { + super(wrapper); + } + + @Slf4j + @Extension + public static class RestApiPluginExecutor implements PluginExecutor { + + @Override + public Flux execute(ResourceConfiguration resourceConfiguration, + ActionConfiguration actionConfiguration, + List params) { + JSONObject requestBody = actionConfiguration.getBody(); + if(requestBody == null) { + requestBody = new JSONObject(); + } + Map propertyMap = params.stream() + .collect(Collectors.toMap(Param::getKey, param -> param)); + + String path = (actionConfiguration.getPath() == null) ? "" : actionConfiguration.getPath(); + String url = resourceConfiguration.getUrl() + path; + HttpMethod httpMethod = actionConfiguration.getHttpMethod(); + if(httpMethod == null) { + return Flux.error(new Exception("HttpMethod must not be null")); + } + + log.debug("Going to make a RestApi call to url: {}, httpMethod: {}", url, httpMethod); + + WebClient webClient = WebClient.builder() + .baseUrl(url) + .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + .build(); + + WebClient.RequestHeadersSpec request = webClient.method(httpMethod) + .body(BodyInserters.fromObject(requestBody)); + + Mono responseMono = request.exchange(); + return responseMono.flatMapMany(response -> { + log.debug("Got response: {}", response); + List contentTypes = response.headers().header(HttpHeaders.CONTENT_TYPE); + Class clazz = String.class; + if (contentTypes != null && contentTypes.size() > 0) { + String contentType = contentTypes.get(0); + boolean isJson = MediaType.APPLICATION_JSON_UTF8_VALUE.toLowerCase() + .equals(contentType.toLowerCase() + .replaceAll("\\s", "")) + || MediaType.APPLICATION_JSON_VALUE.equals(contentType.toLowerCase()); + + if (isJson) { + clazz = JSONObject.class; + } + } + return response.bodyToFlux(clazz); + }); + } + } +} diff --git a/app/server/appsmith-plugins/restApiPlugin/src/test/java/com/external/plugins/RestApiPluginTest.java b/app/server/appsmith-plugins/restApiPlugin/src/test/java/com/external/plugins/RestApiPluginTest.java new file mode 100644 index 0000000000..065ab1dcce --- /dev/null +++ b/app/server/appsmith-plugins/restApiPlugin/src/test/java/com/external/plugins/RestApiPluginTest.java @@ -0,0 +1,20 @@ +package com.external.plugins; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +/** + * Unit test for simple App. + */ +public class RestApiPluginTest +{ + /** + * Rigorous Test :-) + */ + @Test + public void shouldAnswerWithTrue() + { + assertTrue( true ); + } +}