Skip to content

Commit bb1e9a6

Browse files
committed
Examples
1 parent 9ba2c09 commit bb1e9a6

15 files changed

+185
-22
lines changed

convertapi-java.iml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
88
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
99
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10+
<excludeFolder url="file://$MODULE_DIR$/.idea" />
11+
<excludeFolder url="file://$MODULE_DIR$/src/test" />
1012
<excludeFolder url="file://$MODULE_DIR$/target" />
13+
<excludeFolder url="file://$MODULE_DIR$/test-files" />
1114
</content>
1215
<orderEntry type="inheritedJdk" />
1316
<orderEntry type="sourceFolder" forTests="false" />

src/main/java/META-INF/MANIFEST.MF

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
Manifest-Version: 1.0
2-
Main-Class: com.convertapi.ConvertApi
3-

src/main/java/com/convertapi/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public class Config {
44
private static String defaultSecret;
55
private static final String SCHEME = "https";
6-
private static final String HOST = "v2.com.convertapi.com";
6+
private static final String HOST = "v2.convertapi.com";
77
private static final int TIMEOUT = 180;
88
private String scheme;
99
private String host;

src/main/java/com/convertapi/ConversionResult.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.convertapi;
22

3-
import com.convertapi.Model.ConversionResponse;
3+
import com.convertapi.model.ConversionResponse;
4+
import com.convertapi.model.ConversionResponseFile;
45

56
import java.nio.file.Files;
67
import java.nio.file.NotDirectoryException;
@@ -21,6 +22,18 @@ public CompletableFuture<Integer> fileCount() throws ExecutionException, Interru
2122
return responseFuture.thenApplyAsync(r -> r.Files.length);
2223
}
2324

25+
public CompletableFuture<List<String>> urls() throws ExecutionException, InterruptedException {
26+
return responseFuture.thenApplyAsync(r -> {
27+
List<String> valueList = new ArrayList();
28+
for (ConversionResponseFile file: r.Files) valueList.add(file.Url);
29+
return valueList;
30+
});
31+
}
32+
33+
public CompletableFuture<Integer> conversionCost() throws ExecutionException, InterruptedException {
34+
return responseFuture.thenApplyAsync(r -> r.ConversionCost);
35+
}
36+
2437
public CompletableFuture<ConversionResultFile> getFile(int index) {
2538
return responseFuture.thenApplyAsync(r -> new ConversionResultFile(r.Files[index]));
2639
}

src/main/java/com/convertapi/ConversionResultFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.convertapi;
22

3-
import com.convertapi.Model.ConversionResponseFile;
3+
import com.convertapi.model.ConversionResponseFile;
44

55
import java.io.IOException;
66
import java.io.InputStream;

src/main/java/com/convertapi/ConvertApi.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.convertapi;
22

3-
import com.convertapi.Model.ConversionResponse;
3+
import com.convertapi.model.ConversionResponse;
44
import com.google.gson.Gson;
55
import okhttp3.HttpUrl;
66
import okhttp3.MultipartBody;
@@ -33,7 +33,13 @@ public static ConversionResult convert(String fromFormat, String toFormat, Param
3333
for (Param param: params) {
3434
if (!IGNORE_PARAMS.contains(param.getName())) {
3535
try {
36-
multipartBuilder.addFormDataPart(param.getName(), param.getValue());
36+
if (param.getValue().size() == 1) {
37+
multipartBuilder.addFormDataPart(param.getName(), param.getValue().get(0));
38+
} else {
39+
for (int i = 0; i < param.getValue().size(); i++) {
40+
multipartBuilder.addFormDataPart(param.getName() + "[" + i + "]", param.getValue().get(i));
41+
}
42+
}
3743
} catch (ExecutionException | InterruptedException e) {
3844
throw new RuntimeException(e);
3945
}

src/main/java/com/convertapi/Param.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@
66
import java.math.BigDecimal;
77
import java.nio.file.Files;
88
import java.nio.file.Path;
9+
import java.util.ArrayList;
10+
import java.util.List;
911
import java.util.concurrent.CompletableFuture;
1012
import java.util.concurrent.ExecutionException;
1113

1214
public class Param {
1315
private String name;
14-
private CompletableFuture<String> value;
15-
private Config config = Config.defaults();
16+
private CompletableFuture<List<String>> value;
1617

1718
public Param(String name, String value) {
1819
this.name = name.toLowerCase();
19-
this.value = CompletableFuture.completedFuture(value);
20+
List<String> valueList = new ArrayList();
21+
valueList.add(value);
22+
this.value = CompletableFuture.completedFuture(valueList);
2023
}
2124

2225
public Param(String name, int value) {
@@ -50,28 +53,27 @@ public Param(String name, Path value, Config config) throws IOException {
5053

5154
public Param(String name, ConversionResult value) throws ExecutionException, InterruptedException {
5255
this(name, value, 0);
56+
this.value = value.urls();
5357
}
5458

5559
public Param(String name, ConversionResult value, int fileIndex) throws ExecutionException, InterruptedException {
5660
this(name, "");
57-
this.value = value.getFile(fileIndex).thenApplyAsync(f -> f.
58-
getUrl());
61+
this.value = value.getFile(fileIndex).thenApplyAsync(f -> {
62+
List<String> valueList = new ArrayList();
63+
valueList.add(f.getUrl());
64+
return valueList;
65+
});
5966
}
6067

6168
public String getName() {
6269
return name;
6370
}
6471

65-
public Param setConfig(Config config) {
66-
this.config = config;
67-
return this;
68-
}
69-
70-
public String getValue() throws ExecutionException, InterruptedException {
72+
public List<String> getValue() throws ExecutionException, InterruptedException {
7173
return this.value.get();
7274
}
7375

74-
private static CompletableFuture<String> upload(byte[] data, String fileName, MediaType fileContentType, Config config) {
76+
private static CompletableFuture<List<String>> upload(byte[] data, String fileName, MediaType fileContentType, Config config) {
7577
return CompletableFuture.supplyAsync(() -> {
7678
Request request = new Request.Builder()
7779
.url(Http.getUrlBuilder(config).addPathSegment("upload")
@@ -80,7 +82,9 @@ private static CompletableFuture<String> upload(byte[] data, String fileName, Me
8082
.post(RequestBody.create(fileContentType, data))
8183
.build();
8284
try {
83-
return Http.getClient().newCall(request).execute().body().string();
85+
List<String> valueList = new ArrayList<String>();
86+
valueList.add(Http.getClient().newCall(request).execute().body().string());
87+
return valueList;
8488
} catch (IOException e) {
8589
throw new RuntimeException(e);
8690
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.convertapi.examples;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Path;
5+
import java.nio.file.Paths;
6+
import java.util.concurrent.ExecutionException;
7+
import com.convertapi.Config;
8+
import com.convertapi.ConversionResult;
9+
import com.convertapi.ConvertApi;
10+
import com.convertapi.Param;
11+
12+
/**
13+
* Short example of conversions chaining, the PDF pages extracted and saved as separated JPGs and then ZIP'ed
14+
* https://www.convertapi.com/doc/chaining
15+
*/
16+
17+
public class ConversionChaining {
18+
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
19+
Config.setDefaultSecret("YOUR API SECRET"); //Get your secret at https://www.convertapi.com/a
20+
21+
System.out.println("Converting PDF to JPG and compressing result files with ZIP");
22+
ConversionResult jpgResult = ConvertApi.convert("docx", "jpg", new Param[]{new Param("file", Paths.get("./test-files/test.docx"))});
23+
System.out.println("ConvertApi.convert is not blocking method, proceeding to ZIP conversion");
24+
25+
ConversionResult zipResult = ConvertApi.convert("jpg", "zip", new Param[]{new Param("files", jpgResult)});
26+
27+
System.out.println("Saving result file (blocking operation)");
28+
Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
29+
Path path = zipResult.saveFile(tempDir);
30+
31+
System.out.println("DOCX -> JPG conversion cost: " + jpgResult.conversionCost().get());
32+
System.out.println("DOCX -> JPG conversion result file count: " + jpgResult.fileCount().get());
33+
System.out.println("JPG -> ZIP conversion cost: " + zipResult.conversionCost().get());
34+
System.out.println("ZIP file saved to: " + path.toString());
35+
}
36+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.convertapi.examples;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Path;
5+
import java.nio.file.Paths;
6+
import java.util.concurrent.ExecutionException;
7+
import com.convertapi.Config;
8+
import com.convertapi.ConversionResult;
9+
import com.convertapi.ConvertApi;
10+
import com.convertapi.Param;
11+
12+
/**
13+
* Example of conversion remote file. Converting file must be accessible from the internet.
14+
*/
15+
16+
public class ConvertRemoteFile {
17+
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
18+
Config.setDefaultSecret("YOUR API SECRET"); //Get your secret at https://www.convertapi.com/a
19+
20+
Param fileParam = new Param("file", "https://cdn.convertapi.com/cara/testfiles/presentation.pptx");
21+
22+
System.out.println("Converting remote PPTX to PDF");
23+
ConversionResult result = ConvertApi.convert("pptx", "pdf", new Param[]{fileParam});
24+
25+
Path pdfFile = Paths.get(System.getProperty("java.io.tmpdir") + "/myfile.pdf");
26+
result.saveFile(pdfFile);
27+
28+
System.out.println("PDF file saved to: " + pdfFile.toString());
29+
}
30+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.convertapi.examples;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Path;
5+
import java.nio.file.Paths;
6+
import java.util.concurrent.ExecutionException;
7+
import com.convertapi.Config;
8+
import com.convertapi.ConversionResult;
9+
import com.convertapi.ConvertApi;
10+
import com.convertapi.Param;
11+
12+
/**
13+
* Example of converting Web Page URL to PDF file
14+
* https://www.convertapi.com/web-to-pdf
15+
*/
16+
17+
public class ConvertWebToPdf {
18+
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
19+
Config.setDefaultSecret("YOUR API SECRET"); //Get your secret at https://www.convertapi.com/a
20+
21+
System.out.println("Converting WEB to PDF");
22+
ConversionResult result = ConvertApi.convert("web", "pdf", new Param[]{
23+
new Param("url", "https://en.wikipedia.org/wiki/Data_conversion"),
24+
new Param("filename", "web-example")
25+
});
26+
27+
Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir"));
28+
Path pdfFile = result.saveFile(tmpDir);
29+
30+
System.out.println("PDF file saved to: " + pdfFile.toString());
31+
}
32+
}

0 commit comments

Comments
 (0)