javapowerbiquarkuspowerbi-rest-api

Power BI import PBIX file with quarkus-rest-client


I am currently facing an issue to call the "POST import" endpoint of Power BI Rest API.

I have to import a pbix file (that is far less than 1GB so that's good for that).

My application uses quarkus-rest-client to generate client code. As a result I configured this endpoint:

    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Path("/groups/{groupId}/imports")
    @Produces(MediaType.APPLICATION_JSON)
    ImportResp importLightReport(
        @HeaderParam("Authorization") String bearerToken,
        @PathParam("groupId") String groupId,
        @QueryParam("datasetDisplayName") String datasetDisplayName,
        @QueryParam("nameConflict") String nameConflict,
        @QueryParam("overrideModelLabel") boolean overrideModelLabel,
        @QueryParam("overrideReportLabel") boolean overrideReportLabel,
        @QueryParam("skipReport") boolean skipReport,
        FileUpload file
    );

Then, I try to call it like that:

FormData data = new FormData(1);
CaseInsensitiveMap<String> headers = new CaseInsensitiveMap<>();
headers.add("Content-Type", "application/octet-stream");
data.add("file", path, newReportName, headers);
FileUpload fileUpload = new DefaultFileUpload("file", data.getFirst("file"));
importResp = reportClient.importLightReport(
buildBearerToken(accessToken), targetId, newReportName,
overwrite ? "Overwrite" : "Abort", true, true, false, fileUpload);

But the API always answer me

{"error":{"code":"PowerBIInvalidOrCorruptPbixFileError","pbi.error":{"code":"PowerBIInvalidOrCorruptPbixFileError","parameters":{},"details":[],"exceptionCulprit":1}}}

I don't understand why. Of course I tested my reports are valid pbix, I tested them.

For information, I tested putting "@RestForm" on the client, but there is another error that appears "invalid size". What am I doing wrong here?


Solution

  • With a colleague we investigated further on what's wrong with the OP code and why the errors were happening.

    TL;DR: it is impossible to call this specific endpoint of the Power BI API with quarkus-rest-client, we have to implement the call with native http client.

    The configuration

    With quarkus-rest-client, the @RestForm("file") is mandatory. If you do not put it, the payload is not even sent, hence the "corrupted file" error that triggered the OP.

    However, like I said, if we put this annotation we have a InvalidFileSizeError from microsoft.

    After some research we found that the Content-Length header was mandatory. But quarkus-rest-client does send it. So we did not understand what was happening.

    After some try-and-fail experiment, my colleague reached this conclusion: Microsoft expects the Content-Length header to exactly match the raw file size. On the opposite, quarkus-rest-client sends the http body size (i.e including the boundary, part headers...) and it is impossible to change this behaviour.

    The only way we found was to use the native java HTTP client: ClientConnection connection=new URI(url).toURL().openConnection() then build the boundaries and part by hand and valuate the Content-Length with the file size connection.setRequestProperty("Content-Length", path.toFile().length()).

    Regards