Skip to content

Commit

Permalink
refactor: migrate package plugin.core.http to dynamic properties
Browse files Browse the repository at this point in the history
migrate Download task
migrate Request task
migrate Trigger task
migrate HttpConfiguration class
  • Loading branch information
mgabelle committed Jan 13, 2025
1 parent e4698b0 commit a934414
Show file tree
Hide file tree
Showing 17 changed files with 193 additions and 207 deletions.
31 changes: 15 additions & 16 deletions core/src/main/java/io/kestra/core/http/client/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.time.Duration;
import java.util.List;
import java.util.function.Consumer;
import javax.net.ssl.SSLContext;
Expand Down Expand Up @@ -86,23 +87,21 @@ private CloseableHttpClient createClient() throws IllegalVariableEvaluationExcep

// Timeout
if (this.configuration.getTimeout() != null) {
if (this.configuration.getTimeout().getConnectTimeout() != null) {
connectionConfig.setConnectTimeout(Timeout.of(this.configuration.getTimeout().getConnectTimeout()));
}
var connectTiemout = runContext.render(this.configuration.getTimeout().getConnectTimeout()).as(Duration.class);
connectTiemout.ifPresent(duration -> connectionConfig.setConnectTimeout(Timeout.of(duration)));

if (this.configuration.getTimeout().getReadIdleTimeout() != null) {
connectionConfig.setSocketTimeout(Timeout.of(this.configuration.getTimeout().getReadIdleTimeout()));
}
var readIdleTiemout = runContext.render(this.configuration.getTimeout().getReadIdleTimeout()).as(Duration.class);
readIdleTiemout.ifPresent(duration -> connectionConfig.setSocketTimeout(Timeout.of(duration)));
}

// proxy
if (this.configuration.getProxy() != null) {
SocketAddress proxyAddr = new InetSocketAddress(
runContext.render(configuration.getProxy().getAddress()),
configuration.getProxy().getPort()
runContext.render(configuration.getProxy().getAddress()).as(String.class).orElse(null),
runContext.render(configuration.getProxy().getPort()).as(Integer.class).orElse(null)
);

Proxy proxy = new Proxy(configuration.getProxy().getType(), proxyAddr);
Proxy proxy = new Proxy(runContext.render(configuration.getProxy().getType()).as(Proxy.Type.class).orElse(null), proxyAddr);

builder.setProxySelector(new ProxySelector() {
@Override
Expand All @@ -121,12 +120,12 @@ public List<Proxy> select(URI uri) {

credentialsStore.setCredentials(
new AuthScope(
runContext.render(this.configuration.getProxy().getAddress()),
this.configuration.getProxy().getPort()
runContext.render(this.configuration.getProxy().getAddress()).as(String.class).orElse(null),
runContext.render(this.configuration.getProxy().getPort()).as(Integer.class).orElse(null)
),
new UsernamePasswordCredentials(
runContext.render(this.configuration.getProxy().getUsername()),
runContext.render(this.configuration.getProxy().getPassword()).toCharArray()
runContext.render(this.configuration.getProxy().getUsername()).as(String.class).orElseThrow(),
runContext.render(this.configuration.getProxy().getPassword()).as(String.class).orElseThrow().toCharArray()
)
);
}
Expand All @@ -141,15 +140,15 @@ public List<Proxy> select(URI uri) {

// auth
if (this.configuration.getAuth() != null) {
this.configuration.getAuth().configure(builder);
this.configuration.getAuth().configure(builder, runContext);
}

// root options
if (!this.configuration.getFollowRedirects()) {
if (!runContext.render(this.configuration.getFollowRedirects()).as(Boolean.class).orElseThrow()) {
builder.disableRedirectHandling();
}

if (!this.configuration.getAllowFailed()) {
if (!runContext.render(this.configuration.getAllowFailed()).as(Boolean.class).orElseThrow()) {
builder.addResponseInterceptorLast(new FailedResponseInterceptor());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.kestra.core.exceptions.IllegalVariableEvaluationException;
import io.kestra.core.models.property.Property;
import io.kestra.core.runners.RunContext;
import lombok.experimental.SuperBuilder;
import org.apache.hc.client5.http.auth.AuthScheme;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", visible = true, include = JsonTypeInfo.As.EXISTING_PROPERTY)
Expand All @@ -13,9 +15,9 @@
})
@SuperBuilder(toBuilder = true)
public abstract class AbstractAuthConfiguration {
abstract public AuthType getType();
public abstract Property<AuthType> getType();

abstract public void configure(HttpClientBuilder builder);
public abstract void configure(HttpClientBuilder builder, RunContext runContext) throws IllegalVariableEvaluationException;

public enum AuthType {
BASIC,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package io.kestra.core.http.client.configurations;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.exceptions.IllegalVariableEvaluationException;
import io.kestra.core.models.property.Property;
import io.kestra.core.runners.RunContext;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;
import lombok.experimental.SuperBuilder;
import org.apache.hc.client5.http.auth.AuthScheme;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.impl.auth.BasicScheme;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.message.BasicHeader;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;

@Getter
Expand All @@ -24,20 +22,21 @@ public class BasicAuthConfiguration extends AbstractAuthConfiguration {
@NotNull
@JsonInclude
@Builder.Default
protected AuthType type = AuthType.BASIC;
protected Property<AuthType> type = Property.of(AuthType.BASIC);

@Schema(title = "The username for HTTP basic authentication.")
@PluginProperty(dynamic = true)
private final String username;
private final Property<String> username;

@Schema(title = "The password for HTTP basic authentication.")
@PluginProperty(dynamic = true)
private final String password;
private final Property<String> password;

@Override
public void configure(HttpClientBuilder builder) {
public void configure(HttpClientBuilder builder, RunContext runContext) throws IllegalVariableEvaluationException {
byte[] encoded = Base64.getEncoder()
.encode((this.getUsername() + ":" + this.getPassword()).getBytes(StandardCharsets.UTF_8));
.encode((runContext.render(this.getUsername()).as(String.class).orElse(null)
+ ":"
+ runContext.render(this.getPassword()).as(String.class).orElse(null)
).getBytes(StandardCharsets.UTF_8));

builder.addRequestInterceptorFirst((request, entity, context) -> request
.setHeader(new BasicHeader(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.kestra.core.http.client.configurations;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.exceptions.IllegalVariableEvaluationException;
import io.kestra.core.models.property.Property;
import io.kestra.core.runners.RunContext;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
Expand All @@ -17,18 +19,18 @@ public class BearerAuthConfiguration extends AbstractAuthConfiguration {
@NotNull
@JsonInclude
@Builder.Default
protected AuthType type = AuthType.BEARER;
protected Property<AuthType> type = Property.of(AuthType.BEARER);

@Schema(title = "The token for bearer token authentication.")
@PluginProperty(dynamic = true)
private final String token;
private final Property<String> token;

@Override
public void configure(HttpClientBuilder builder) {
public void configure(HttpClientBuilder builder, RunContext runContext) throws IllegalVariableEvaluationException {
var renderedToken = runContext.render(this.token).as(String.class).orElse(null);
builder.addRequestInterceptorFirst((request, entity, context) -> request
.setHeader(new BasicHeader(
HttpHeaders.AUTHORIZATION,
"Bearer " + this.token
"Bearer " + renderedToken
)));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.kestra.core.http.client.configurations;

import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.models.property.Property;
import io.micronaut.http.client.HttpClientConfiguration;
import io.micronaut.logging.LogLevel;
import io.swagger.v3.oas.annotations.media.Schema;
Expand Down Expand Up @@ -32,18 +33,15 @@ public class HttpConfiguration {

@Schema(title = "Whether redirects should be followed automatically.")
@Builder.Default
@PluginProperty
private Boolean followRedirects = true;
private Property<Boolean> followRedirects = Property.of(true);

@Schema(title = "If true, allow a failed response code (response code >= 400)")
@Builder.Default
@PluginProperty
private Boolean allowFailed = false;
private Property<Boolean> allowFailed = Property.of(false);

@Schema(title = "The default charset for the request.")
@Builder.Default
@PluginProperty
private final Charset defaultCharset = StandardCharsets.UTF_8;
private final Property<Charset> defaultCharset = Property.of(StandardCharsets.UTF_8);

@Schema(title = "The enabled log.")
@PluginProperty
Expand All @@ -59,12 +57,11 @@ public enum LoggingType {
// Deprecated properties

@Schema(title = "The time allowed to establish a connection to the server before failing.")
@PluginProperty
@Deprecated
private final Duration connectTimeout;
private final Property<Duration> connectTimeout;

@Deprecated
public void setConnectTimeout(Duration connectTimeout) {
public void setConnectTimeout(Property<Duration> connectTimeout) {
if (this.timeout == null) {
this.timeout = TimeoutConfiguration.builder()
.build();
Expand All @@ -77,12 +74,11 @@ public void setConnectTimeout(Duration connectTimeout) {

@Schema(title = "The maximum time allowed for reading data from the server before failing.")
@Builder.Default
@PluginProperty
@Deprecated
private final Duration readTimeout = Duration.ofSeconds(HttpClientConfiguration.DEFAULT_READ_TIMEOUT_SECONDS);
private final Property<Duration> readTimeout = Property.of(Duration.ofSeconds(HttpClientConfiguration.DEFAULT_READ_TIMEOUT_SECONDS));

@Deprecated
public void setReadTimeout(Duration readTimeout) {
public void setReadTimeout(Property<Duration> readTimeout) {
if (this.timeout == null) {
this.timeout = TimeoutConfiguration.builder()
.build();
Expand All @@ -95,12 +91,11 @@ public void setReadTimeout(Duration readTimeout) {

@Schema(title = "The type of proxy to use.")
@Builder.Default
@PluginProperty
@Deprecated
private final Proxy.Type proxyType = Proxy.Type.DIRECT;
private final Property<Proxy.Type> proxyType = Property.of(Proxy.Type.DIRECT);

@Deprecated
public void setProxyType(Proxy.Type proxyType) {
public void setProxyType(Property<Proxy.Type> proxyType) {
if (this.proxy == null) {
this.proxy = ProxyConfiguration.builder()
.build();
Expand All @@ -112,12 +107,11 @@ public void setProxyType(Proxy.Type proxyType) {
}

@Schema(title = "The address of the proxy server.")
@PluginProperty(dynamic = true)
@Deprecated
private final String proxyAddress;
private final Property<String> proxyAddress;

@Deprecated
public void setProxyAddress(String proxyAddress) {
public void setProxyAddress(Property<String> proxyAddress) {
if (this.proxy == null) {
this.proxy = ProxyConfiguration.builder()
.build();
Expand All @@ -129,12 +123,11 @@ public void setProxyAddress(String proxyAddress) {
}

@Schema(title = "The port of the proxy server.")
@PluginProperty
@Deprecated
private final Integer proxyPort;
private final Property<Integer> proxyPort;

@Deprecated
public void setProxyPort(Integer proxyPort) {
public void setProxyPort(Property<Integer> proxyPort) {
if (this.proxy == null) {
this.proxy = ProxyConfiguration.builder()
.build();
Expand All @@ -146,12 +139,11 @@ public void setProxyPort(Integer proxyPort) {
}

@Schema(title = "The username for proxy authentication.")
@PluginProperty(dynamic = true)
@Deprecated
private final String proxyUsername;
private final Property<String> proxyUsername;

@Deprecated
public void setProxyUsername(String proxyUsername) {
public void setProxyUsername(Property<String> proxyUsername) {
if (this.proxy == null) {
this.proxy = ProxyConfiguration.builder()
.build();
Expand All @@ -163,12 +155,11 @@ public void setProxyUsername(String proxyUsername) {
}

@Schema(title = "The password for proxy authentication.")
@PluginProperty(dynamic = true)
@Deprecated
private final String proxyPassword;
private final Property<String> proxyPassword;

@Deprecated
public void setProxyPassword(String proxyPassword) {
public void setProxyPassword(Property<String> proxyPassword) {
if (this.proxy == null) {
this.proxy = ProxyConfiguration.builder()
.build();
Expand All @@ -180,12 +171,11 @@ public void setProxyPassword(String proxyPassword) {
}

@Schema(title = "The username for HTTP basic authentication.")
@PluginProperty(dynamic = true)
@Deprecated
private final String basicAuthUser;
private final Property<String> basicAuthUser;

@Deprecated
public void setBasicAuthUser(String basicAuthUser) {
public void setBasicAuthUser(Property<String> basicAuthUser) {
if (this.auth == null || !(this.auth instanceof BasicAuthConfiguration)) {
this.auth = BasicAuthConfiguration.builder()
.build();
Expand All @@ -197,12 +187,11 @@ public void setBasicAuthUser(String basicAuthUser) {
}

@Schema(title = "The password for HTTP basic authentication.")
@PluginProperty(dynamic = true)
@Deprecated
private final String basicAuthPassword;
private final Property<String> basicAuthPassword;

@Deprecated
private void setBasicAuthPassword(String basicAuthPassword) {
private void setBasicAuthPassword(Property<String> basicAuthPassword) {
if (this.auth == null || !(this.auth instanceof BasicAuthConfiguration)) {
this.auth = BasicAuthConfiguration.builder()
.build();
Expand Down Expand Up @@ -243,20 +232,17 @@ private void setLogLevel(LogLevel logLevel) {

@Schema(title = "The time allowed for a read connection to remain idle before closing it.")
@Builder.Default
@PluginProperty
@Deprecated
private final Duration readIdleTimeout = Duration.of(HttpClientConfiguration.DEFAULT_READ_IDLE_TIMEOUT_MINUTES, ChronoUnit.MINUTES);
private final Property<Duration> readIdleTimeout = Property.of(Duration.of(HttpClientConfiguration.DEFAULT_READ_IDLE_TIMEOUT_MINUTES, ChronoUnit.MINUTES));


@Schema(title = "The time an idle connection can remain in the client's connection pool before being closed.")
@Builder.Default
@PluginProperty
@Deprecated
private final Duration connectionPoolIdleTimeout = Duration.ofSeconds(HttpClientConfiguration.DEFAULT_CONNECTION_POOL_IDLE_TIMEOUT_SECONDS);
private final Property<Duration> connectionPoolIdleTimeout = Property.of(Duration.ofSeconds(HttpClientConfiguration.DEFAULT_CONNECTION_POOL_IDLE_TIMEOUT_SECONDS));

@Schema(title = "The maximum content length of the response.")
@Builder.Default
@PluginProperty
@Deprecated
private final Integer maxContentLength = HttpClientConfiguration.DEFAULT_MAX_CONTENT_LENGTH;
private final Property<Integer> maxContentLength = Property.of(HttpClientConfiguration.DEFAULT_MAX_CONTENT_LENGTH);
}
Loading

0 comments on commit a934414

Please sign in to comment.