All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.convertapi.client.Param Maven / Gradle / Ivy

package com.convertapi.client;

import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@SuppressWarnings("WeakerAccess")
public class Param {
    private final String name;
    private CompletableFuture> value;
    private boolean isUploadedFile = false;

    @SuppressWarnings("unused")
    private Param(String name) {
        this.name = name.toLowerCase();
    }

    @SuppressWarnings("unused")
    public Param(String name, String value) {
        this(name.toLowerCase());
        List valueList = new ArrayList<>();
        valueList.add(value);
        this.value = CompletableFuture.completedFuture(valueList);
    }

    @SuppressWarnings("unused")
    public Param(String name, int value) {
        this(name, String.valueOf(value));
    }

    @SuppressWarnings("unused")
    public Param(String name, BigDecimal value) {
        this(name, String.valueOf(value));
    }

    @SuppressWarnings("unused")
    public Param(String name, byte[] value, String fileFormat) {
        this(name, value, fileFormat, Config.defaults());
    }

    @SuppressWarnings("WeakerAccess")
    public Param(String name, byte[] value, String fileFormat, Config config) {
        this(name);
        String fileName = "getFile." + fileFormat;
        this.value = upload(value, fileName, config);
        isUploadedFile = true;
    }

    public Param(String name, Path value) throws IOException {
        this(name, value, Config.defaults());
    }

    @SuppressWarnings("WeakerAccess")
    public Param(String name, Path value, Config config) throws IOException {
        this(name);
        this.value = upload(Files.readAllBytes(value), value.getFileName().toString(), config);
        isUploadedFile = true;
    }

    @SuppressWarnings("unused")
    public Param(String name, ConversionResult value) {
        this(name, value, 0);
        this.value = CompletableFuture.completedFuture(value.urls());
    }

    @SuppressWarnings("WeakerAccess")
    public Param(String name, ConversionResult value, int fileIndex) {
        this(name);
        List valueList = new ArrayList<>();
        valueList.add(value.getFile(fileIndex).getUrl());
        this.value = CompletableFuture.completedFuture(valueList);
    }

    @SuppressWarnings("WeakerAccess")
    public Param(String name, CompletableFuture value, int fileIndex) {
        this(name);
        this.value = value.thenApply((res) -> Collections.singletonList(res.getFile(fileIndex).getUrl()));
    }

    @SuppressWarnings("unused")
    public Param(String name, CompletableFuture value) {
        this(name);
        this.value = value.thenApply(ConversionResult::urls);
    }

    private static CompletableFuture> upload(byte[] data, String fileName, Config config) {
        return CompletableFuture.supplyAsync(() -> {
            Request request = Http.getRequestBuilder()
                    .url(Http.getUrlBuilder(config).addPathSegment("upload")
                            .addQueryParameter("filename", fileName)
                            .build())
                    .post(RequestBody.create(MediaType.parse("application/octet-stream"), data))
                    .build();
            try {
                List valueList = new ArrayList<>();
                //noinspection ConstantConditions
                valueList.add(Http.getClient().newCall(request).execute().body().string());
                return valueList;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
    }

    public String getName() {
        return name;
    }

    public List getValue() throws ExecutionException, InterruptedException {
        return this.value.get();
    }

    public CompletableFuture delete() {
        return isUploadedFile
                ? value.thenCompose(urls -> Http.requestDelete(urls.get(0)))
                : CompletableFuture.completedFuture(null);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy