Adding a very initial version of the RestApiPlugin
Requires a bunch of testing for corner cases and error scenarios.
This commit is contained in:
parent
5d20f170ee
commit
b618bfc5d8
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
<modules>
|
||||
<module>postgresPlugin</module>
|
||||
<module>restApiPlugin</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
|
@ -17,8 +17,8 @@
|
|||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
<plugin.id>postgres-plugin</plugin.id>
|
||||
<plugin.class>com.external.plugins.PostgresPlugin</plugin.class>
|
||||
<plugin.version>0.0.1</plugin.version>
|
||||
<plugin.provider>Arpit Mohan</plugin.provider>
|
||||
<plugin.version>1.0-SNAPSHOT</plugin.version>
|
||||
<plugin.provider>tech@appsmith.com</plugin.provider>
|
||||
<plugin.dependencies />
|
||||
</properties>
|
||||
|
||||
|
|
|
|||
85
app/server/appsmith-plugins/restApiPlugin/pom.xml
Normal file
85
app/server/appsmith-plugins/restApiPlugin/pom.xml
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.external.plugins</groupId>
|
||||
<artifactId>restApiPlugin</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<name>restApiPlugin</name>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>11</java.version>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
<plugin.id>restapi-plugin</plugin.id>
|
||||
<plugin.class>com.external.plugins.RestApiPlugin</plugin.class>
|
||||
<plugin.version>1.0-SNAPSHOT</plugin.version>
|
||||
<plugin.provider>tech@appsmith.com</plugin.provider>
|
||||
<plugin.dependencies />
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.11</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.pf4j</groupId>
|
||||
<artifactId>pf4j-spring</artifactId>
|
||||
<version>0.5.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.appsmith</groupId>
|
||||
<artifactId>interfaces</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webflux</artifactId>
|
||||
<version>5.1.5.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.8</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.1.2</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifestEntries>
|
||||
<Plugin-Id>${plugin.id}</Plugin-Id>
|
||||
<Plugin-Class>${plugin.class}</Plugin-Class>
|
||||
<Plugin-Version>${plugin.version}</Plugin-Version>
|
||||
<Plugin-Provider>${plugin.provider}</Plugin-Provider>
|
||||
<Plugin-Dependencies>${plugin.dependencies}</Plugin-Dependencies>
|
||||
</manifestEntries>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
83
app/server/appsmith-plugins/restApiPlugin/src/main/java/com/external/plugins/RestApiPlugin.java
vendored
Normal file
83
app/server/appsmith-plugins/restApiPlugin/src/main/java/com/external/plugins/RestApiPlugin.java
vendored
Normal file
|
|
@ -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<Object> execute(ResourceConfiguration resourceConfiguration,
|
||||
ActionConfiguration actionConfiguration,
|
||||
List<Param> params) {
|
||||
JSONObject requestBody = actionConfiguration.getBody();
|
||||
if(requestBody == null) {
|
||||
requestBody = new JSONObject();
|
||||
}
|
||||
Map<String, Param> 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<ClientResponse> responseMono = request.exchange();
|
||||
return responseMono.flatMapMany(response -> {
|
||||
log.debug("Got response: {}", response);
|
||||
List<String> 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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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 );
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user