From db75b313869cce6ecdfe5916fc87e616675a868f Mon Sep 17 00:00:00 2001 From: Mathieu Gabelle Date: Tue, 10 Dec 2024 12:14:13 +0100 Subject: [PATCH] refactor: dynamic properties --- .../AbstractHightouchConnection.java | 13 ++++---- .../java/io/kestra/plugin/hightouch/Sync.java | 33 +++++++++---------- .../hightouch/SyncInvalidSyncIdTest.java | 5 +-- .../hightouch/SyncInvalidTokenTest.java | 5 +-- .../io/kestra/plugin/hightouch/SyncTest.java | 5 +-- 5 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/main/java/io/kestra/plugin/hightouch/AbstractHightouchConnection.java b/src/main/java/io/kestra/plugin/hightouch/AbstractHightouchConnection.java index c4ce8f0..74f071a 100644 --- a/src/main/java/io/kestra/plugin/hightouch/AbstractHightouchConnection.java +++ b/src/main/java/io/kestra/plugin/hightouch/AbstractHightouchConnection.java @@ -1,6 +1,7 @@ package io.kestra.plugin.hightouch; -import io.kestra.core.models.annotations.PluginProperty; +import io.kestra.core.exceptions.IllegalVariableEvaluationException; +import io.kestra.core.models.property.Property; import io.kestra.core.models.tasks.Task; import java.io.IOException; @@ -14,6 +15,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; +import io.kestra.core.runners.RunContext; import io.kestra.core.serializers.JacksonMapper; import io.swagger.v3.oas.annotations.media.Schema; import lombok.EqualsAndHashCode; @@ -35,13 +37,12 @@ abstract class AbstractHightouchConnection extends Task { title = "API Bearer token" ) @NotNull - @PluginProperty(dynamic = true) - String token; + Property token; private final static String BASE_URL = "https://api.hightouch.com"; private final static ObjectMapper OBJECT_MAPPER = JacksonMapper.ofJson(); - protected RES request(String method, String path, String body, Class responseType) throws IOException, InterruptedException { + protected RES request(String method, String path, String body, Class responseType, RunContext runContext) throws IOException, InterruptedException { HttpClient httpClient = HttpClient.newBuilder().build(); @@ -51,7 +52,7 @@ protected RES request(String method, String path, String body, Class< HttpRequest request = HttpRequest.newBuilder(fullPath) .method(method, HttpRequest.BodyPublishers.ofString(body, StandardCharsets.UTF_8)) .header("Content-Type", "application/json") - .header("Authorization", "Bearer " + token) + .header("Authorization", "Bearer " + runContext.render(token).as(String.class).orElseThrow()) .build(); HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); @@ -64,7 +65,7 @@ protected RES request(String method, String path, String body, Class< "Request to '" + fullPath.getPath() + "' failed with status '" + response.statusCode() + "' and body '" + response.body() + "'" ); } - } catch (ConnectException | MalformedURLException e) { + } catch (ConnectException | MalformedURLException | IllegalVariableEvaluationException e) { throw new RuntimeException(e); } } diff --git a/src/main/java/io/kestra/plugin/hightouch/Sync.java b/src/main/java/io/kestra/plugin/hightouch/Sync.java index 158b913..4e060e0 100644 --- a/src/main/java/io/kestra/plugin/hightouch/Sync.java +++ b/src/main/java/io/kestra/plugin/hightouch/Sync.java @@ -2,8 +2,8 @@ import io.kestra.core.models.annotations.Example; import io.kestra.core.models.annotations.Plugin; -import io.kestra.core.models.annotations.PluginProperty; import io.kestra.core.models.executions.metrics.Counter; +import io.kestra.core.models.property.Property; import io.kestra.core.models.tasks.RunnableTask; import io.kestra.core.runners.RunContext; import io.kestra.core.utils.Await; @@ -47,7 +47,7 @@ - id: sync type: io.kestra.plugin.hightouch.Sync token: YOUR_API_TOKEN - syncId: 1127166 + syncId: 1127166 """ ) } @@ -65,30 +65,26 @@ public class Sync extends AbstractHightouchConnection implements RunnableTask syncId; @Schema( title = "Whether to do a full resynchronization" ) - @PluginProperty(dynamic = true) @Builder.Default - private Boolean fullResynchronization = false; + private Property fullResynchronization = Property.of(false); @Schema( title = "Whether to wait for the end of the run.", description = "Allowing to capture run status and logs" ) - @PluginProperty @Builder.Default - private Boolean wait = true; + private Property wait = Property.of(true); @Schema( title = "The max total wait duration" ) - @PluginProperty @Builder.Default - private Duration maxDuration = Duration.ofMinutes(5); + private Property maxDuration = Property.of(Duration.ofMinutes(5)); @Builder.Default @Getter(AccessLevel.NONE) @@ -98,14 +94,15 @@ public class Sync extends AbstractHightouchConnection implements RunnableTask