io.kestra.plugin.powerbi.AbstractPowerBi Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of plugin-powerbi Show documentation
Show all versions of plugin-powerbi Show documentation
Embed Power BI reports and dashboards within Kestra data workflows.
package io.kestra.plugin.powerbi;
import io.kestra.core.exceptions.IllegalVariableEvaluationException;
import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.models.tasks.Task;
import io.kestra.core.runners.RunContext;
import io.micronaut.core.type.Argument;
import io.micronaut.http.*;
import io.micronaut.http.client.DefaultHttpClientConfiguration;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import io.micronaut.http.client.netty.DefaultHttpClient;
import io.micronaut.http.client.netty.NettyHttpClientFactory;
import io.micronaut.http.codec.MediaTypeCodecRegistry;
import io.micronaut.http.uri.UriTemplate;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.apache.commons.lang3.mutable.Mutable;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.Map;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
@SuperBuilder
@ToString
@EqualsAndHashCode
@Getter
@NoArgsConstructor
public abstract class AbstractPowerBi extends Task {
@NotNull
@NotEmpty
@Schema(
title = "Azure username."
)
@PluginProperty(dynamic = true)
private String tenantId;
@NotNull
@NotEmpty
@Schema(
title = "Azure client ID."
)
@PluginProperty(dynamic = true)
private String clientId;
@NotNull
@NotEmpty
@Schema(
title = "Azure client secret."
)
@PluginProperty(dynamic = true)
private String clientSecret;
@Getter(AccessLevel.NONE)
private transient String token;
private static final NettyHttpClientFactory FACTORY = new NettyHttpClientFactory();
protected HttpClient client(RunContext runContext, String base) throws IllegalVariableEvaluationException, MalformedURLException, URISyntaxException {
MediaTypeCodecRegistry mediaTypeCodecRegistry = runContext.getApplicationContext().getBean(MediaTypeCodecRegistry.class);
DefaultHttpClient client = (DefaultHttpClient) FACTORY.createClient(URI.create(base).toURL(), new DefaultHttpClientConfiguration());
client.setMediaTypeCodecRegistry(mediaTypeCodecRegistry);
return client;
}
private String token(RunContext runContext) throws IllegalVariableEvaluationException, MalformedURLException, URISyntaxException {
if (this.token != null) {
return this.token;
}
UriTemplate uriTemplate = UriTemplate.of("/{tenantId}/oauth2/token");
String uri = uriTemplate.expand(Map.of(
"tenantId", runContext.render(this.tenantId)
));
MutableHttpRequest request = HttpRequest.create(HttpMethod.POST, uri)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.body("grant_type=client_credentials" +
"&client_id=" + runContext.render(this.clientId)+
"&client_secret=" + runContext.render(this.clientSecret)+
"&resource=https://analysis.windows.net/powerbi/api" +
"&scope=https://analysis.windows.net/powerbi/api/.default"
);
try (HttpClient client = this.client(runContext, "https://login.microsoftonline.com")) {
HttpResponse