Skip to content

Commit c2b91f1

Browse files
committed
Examples
1 parent bb1e9a6 commit c2b91f1

File tree

10 files changed

+178
-57
lines changed

10 files changed

+178
-57
lines changed

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

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,39 @@
1212
import java.util.concurrent.ExecutionException;
1313

1414
public class ConversionResult {
15-
private final CompletableFuture<ConversionResponse> responseFuture;
15+
private final ConversionResponse response;
1616

17-
public ConversionResult(CompletableFuture<ConversionResponse> responseFuture) {
18-
this.responseFuture = responseFuture;
17+
public ConversionResult(ConversionResponse responseFuture) {
18+
this.response = responseFuture;
1919
}
2020

21-
public CompletableFuture<Integer> fileCount() throws ExecutionException, InterruptedException {
22-
return responseFuture.thenApplyAsync(r -> r.Files.length);
21+
public Integer fileCount() throws ExecutionException, InterruptedException {
22+
return response.Files.length;
2323
}
2424

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-
});
25+
public List<String> urls() {
26+
List<String> valueList = new ArrayList();
27+
for (ConversionResponseFile file: response.Files) valueList.add(file.Url);
28+
return valueList;
3129
}
3230

33-
public CompletableFuture<Integer> conversionCost() throws ExecutionException, InterruptedException {
34-
return responseFuture.thenApplyAsync(r -> r.ConversionCost);
31+
public Integer conversionCost() throws ExecutionException, InterruptedException {
32+
return response.ConversionCost;
3533
}
3634

37-
public CompletableFuture<ConversionResultFile> getFile(int index) {
38-
return responseFuture.thenApplyAsync(r -> new ConversionResultFile(r.Files[index]));
35+
public ConversionResultFile getFile(int index) {
36+
return new ConversionResultFile(response.Files[index]);
3937
}
4038

41-
public Path saveFile(Path file) throws ExecutionException, InterruptedException {
42-
return getFile(0).thenCompose(f -> f.saveFile(file)).get();
39+
public CompletableFuture<Path> saveFile(Path file) {
40+
return getFile(0).saveFile(file);
4341
}
4442

45-
public List<Path> saveFiles(Path directory) throws ExecutionException, InterruptedException, NotDirectoryException {
46-
if (!Files.isDirectory(directory)) throw new NotDirectoryException(directory.toString());
47-
List<Path> paths = new ArrayList<Path>();
48-
for (int i = 0; i < responseFuture.get().Files.length; i++) {
49-
paths.add(getFile(i).thenCompose(f -> f.saveFile(directory)).get());
43+
public List<CompletableFuture<Path>> saveFiles(Path directory) {
44+
if (!Files.isDirectory(directory)) throw new RuntimeException("Directory expected, but received: " + directory.toString());
45+
List<CompletableFuture<Path>> paths = new ArrayList();
46+
for (int i = 0; i < response.Files.length; i++) {
47+
paths.add(getFile(i).saveFile(directory));
5048
}
5149
return paths;
5250
}

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

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

33
import com.convertapi.model.ConversionResponse;
4+
import com.convertapi.model.User;
45
import com.google.gson.Gson;
56
import okhttp3.HttpUrl;
67
import okhttp3.MultipartBody;
@@ -16,11 +17,11 @@
1617
public class ConvertApi {
1718
private static final List<String> IGNORE_PARAMS = Arrays.asList( "storefile", "async", "jobid", "timeout");
1819

19-
public static ConversionResult convert(String fromFormat, String toFormat, Param[] params) {
20+
public static CompletableFuture<ConversionResult> convert(String fromFormat, String toFormat, Param[] params) {
2021
return convert(fromFormat, toFormat, params, Config.defaults());
2122
}
2223

23-
public static ConversionResult convert(String fromFormat, String toFormat, Param[] params, Config config) {
24+
public static CompletableFuture<ConversionResult> convert(String fromFormat, String toFormat, Param[] params, Config config) {
2425
CompletableFuture<ConversionResponse> completableResponse = CompletableFuture.supplyAsync(() -> {
2526
HttpUrl url = Http.getUrlBuilder(config)
2627
.addPathSegment(fromFormat)
@@ -66,6 +67,31 @@ public static ConversionResult convert(String fromFormat, String toFormat, Param
6667
return new Gson().fromJson(bodyString, ConversionResponse.class);
6768
});
6869

69-
return new ConversionResult(completableResponse);
70+
return completableResponse.thenApply(r -> new ConversionResult(r));
71+
}
72+
73+
public static User getUser() {
74+
return getUser(Config.defaults());
75+
}
76+
77+
public static User getUser(Config config) {
78+
HttpUrl url = Http.getUrlBuilder(config).addPathSegment("user").build();
79+
Request request = new Request.Builder()
80+
.url(url)
81+
.addHeader("Accept", "application/json")
82+
.build();
83+
84+
String bodyString;
85+
try {
86+
Response response = Http.getClient().newCall(request).execute();
87+
bodyString = response.body().string();
88+
if (response.code() != 200) {
89+
throw new ConversionException(bodyString);
90+
}
91+
} catch (IOException e) {
92+
throw new RuntimeException(e);
93+
}
94+
95+
return new Gson().fromJson(bodyString, User.class);
7096
}
7197
}

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ public class Param {
1515
private String name;
1616
private CompletableFuture<List<String>> value;
1717

18-
public Param(String name, String value) {
18+
private Param(String name) {
1919
this.name = name.toLowerCase();
20+
}
21+
22+
public Param(String name, String value) {
23+
this(name.toLowerCase());
2024
List<String> valueList = new ArrayList();
2125
valueList.add(value);
2226
this.value = CompletableFuture.completedFuture(valueList);
@@ -35,7 +39,7 @@ public Param(String name, byte[] value, String fileFormat) {
3539
}
3640

3741
public Param(String name, byte[] value, String fileFormat, Config config) {
38-
this(name, "");
42+
this(name);
3943
String fileName = "getFile." + fileFormat;
4044
String contentTypeString = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(fileName);
4145
this.value = upload(value, fileName, MediaType.parse(contentTypeString), config);
@@ -46,23 +50,26 @@ public Param(String name, Path value) throws IOException {
4650
}
4751

4852
public Param(String name, Path value, Config config) throws IOException {
49-
this(name, "");
53+
this(name);
5054
String contentTypeString = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(value.toFile());
5155
this.value = upload(Files.readAllBytes(value), value.getFileName().toString(), MediaType.parse(contentTypeString), config);
5256
}
5357

5458
public Param(String name, ConversionResult value) throws ExecutionException, InterruptedException {
5559
this(name, value, 0);
56-
this.value = value.urls();
60+
this.value = CompletableFuture.completedFuture(value.urls());
5761
}
5862

5963
public Param(String name, ConversionResult value, int fileIndex) throws ExecutionException, InterruptedException {
60-
this(name, "");
61-
this.value = value.getFile(fileIndex).thenApplyAsync(f -> {
62-
List<String> valueList = new ArrayList();
63-
valueList.add(f.getUrl());
64-
return valueList;
65-
});
64+
this(name);
65+
List<String> valueList = new ArrayList();
66+
valueList.add(value.getFile(fileIndex).getUrl());
67+
this.value = CompletableFuture.completedFuture(valueList);
68+
}
69+
70+
public Param(String name, CompletableFuture<ConversionResult> value) throws ExecutionException, InterruptedException {
71+
this(name);
72+
this.value = value.thenApply(r -> r.urls());
6673
}
6774

6875
public String getName() {

src/main/java/com/convertapi/examples/ConversionChaining.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.nio.file.Path;
55
import java.nio.file.Paths;
6+
import java.util.concurrent.CompletableFuture;
67
import java.util.concurrent.ExecutionException;
78
import com.convertapi.Config;
89
import com.convertapi.ConversionResult;
@@ -16,21 +17,21 @@
1617

1718
public class ConversionChaining {
1819
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+
Config.setDefaultSecret("YOUR API SECRET"); //Get your secret at https://www.convertapi.com/a
2021

2122
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+
CompletableFuture<ConversionResult> jpgResult = ConvertApi.convert("docx", "jpg", new Param[]{new Param("file", Paths.get("test-files/test.docx"))});
2324
System.out.println("ConvertApi.convert is not blocking method, proceeding to ZIP conversion");
2425

25-
ConversionResult zipResult = ConvertApi.convert("jpg", "zip", new Param[]{new Param("files", jpgResult)});
26+
CompletableFuture<ConversionResult> zipResult = ConvertApi.convert("jpg", "zip", new Param[]{new Param("files", jpgResult)});
2627

2728
System.out.println("Saving result file (blocking operation)");
2829
Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
29-
Path path = zipResult.saveFile(tempDir);
30+
CompletableFuture<Path> path = zipResult.get().saveFile(tempDir);
3031

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());
32+
System.out.println("DOCX -> JPG conversion cost: " + jpgResult.get().conversionCost());
33+
System.out.println("DOCX -> JPG conversion result file count: " + jpgResult.get().fileCount());
34+
System.out.println("JPG -> ZIP conversion cost: " + zipResult.get().conversionCost());
35+
System.out.println("ZIP file saved to: " + path.get().toString());
3536
}
3637
}

src/main/java/com/convertapi/examples/ConvertRemoteFile.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.nio.file.Path;
55
import java.nio.file.Paths;
6+
import java.util.concurrent.CompletableFuture;
67
import java.util.concurrent.ExecutionException;
78
import com.convertapi.Config;
89
import com.convertapi.ConversionResult;
@@ -20,10 +21,10 @@ public static void main(String[] args) throws IOException, ExecutionException, I
2021
Param fileParam = new Param("file", "https://cdn.convertapi.com/cara/testfiles/presentation.pptx");
2122

2223
System.out.println("Converting remote PPTX to PDF");
23-
ConversionResult result = ConvertApi.convert("pptx", "pdf", new Param[]{fileParam});
24+
CompletableFuture<ConversionResult> result = ConvertApi.convert("pptx", "pdf", new Param[]{fileParam});
2425

2526
Path pdfFile = Paths.get(System.getProperty("java.io.tmpdir") + "/myfile.pdf");
26-
result.saveFile(pdfFile);
27+
result.get().saveFile(pdfFile);
2728

2829
System.out.println("PDF file saved to: " + pdfFile.toString());
2930
}

src/main/java/com/convertapi/examples/ConvertWebToPdf.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.nio.file.Path;
55
import java.nio.file.Paths;
6+
import java.util.concurrent.CompletableFuture;
67
import java.util.concurrent.ExecutionException;
78
import com.convertapi.Config;
89
import com.convertapi.ConversionResult;
@@ -19,14 +20,14 @@ public static void main(String[] args) throws IOException, ExecutionException, I
1920
Config.setDefaultSecret("YOUR API SECRET"); //Get your secret at https://www.convertapi.com/a
2021

2122
System.out.println("Converting WEB to PDF");
22-
ConversionResult result = ConvertApi.convert("web", "pdf", new Param[]{
23+
CompletableFuture<ConversionResult> result = ConvertApi.convert("web", "pdf", new Param[]{
2324
new Param("url", "https://en.wikipedia.org/wiki/Data_conversion"),
2425
new Param("filename", "web-example")
2526
});
2627

2728
Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir"));
28-
Path pdfFile = result.saveFile(tmpDir);
29+
CompletableFuture<Path> pdfFile = result.get().saveFile(tmpDir);
2930

30-
System.out.println("PDF file saved to: " + pdfFile.toString());
31+
System.out.println("PDF file saved to: " + pdfFile.get().toString());
3132
}
3233
}

src/main/java/com/convertapi/examples/ConvertWordToPdfAndPng.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.nio.file.Path;
1010
import java.nio.file.Paths;
1111
import java.util.List;
12+
import java.util.concurrent.CompletableFuture;
1213
import java.util.concurrent.ExecutionException;
1314

1415
/**
@@ -20,22 +21,21 @@
2021

2122
public class ConvertWordToPdfAndPng {
2223
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
23-
Config.setDefaultSecret("YOUR API SECRET"); //Get your secret at https://www.convertapi.com/a
24+
Config.setDefaultSecret("YOUR API SECRET"); //Get your secret at https://www.convertapi.com/a
25+
Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
2426

2527
System.out.println("Converting DOCX to PDF and JPG in parallel");
2628

2729
Param docxFileParam = new Param("file", Paths.get("test-files/test.docx"));
2830

29-
ConversionResult pdfResult = ConvertApi.convert("docx", "pdf", new Param[]{docxFileParam});
30-
ConversionResult jpgResult = ConvertApi.convert("docx", "jpg", new Param[]{docxFileParam});
31+
CompletableFuture<ConversionResult> pdfResult = ConvertApi.convert("docx", "pdf", new Param[]{docxFileParam});
32+
CompletableFuture<ConversionResult> jpgResult = ConvertApi.convert("docx", "jpg", new Param[]{docxFileParam});
3133

32-
Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
33-
Path pdfPath = pdfResult.saveFile(tempDir);
34-
List<Path> jpgPaths = jpgResult.saveFiles(tempDir);
34+
System.out.println("PDF file saved to: " + pdfResult.get().saveFile(tempDir).get());
3535

36-
System.out.println("PDF file saved to: " + pdfPath.toString());
37-
for (Path path: jpgPaths) {
38-
System.out.println("JPG file saved to: " + path.toString());
36+
List<CompletableFuture<Path>> jpgPaths = jpgResult.get().saveFiles(tempDir);
37+
for (CompletableFuture<Path> path: jpgPaths) {
38+
System.out.println("JPG file saved to: " + path.get().toString());
3939
}
4040
}
4141
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.convertapi.examples;
2+
3+
import com.convertapi.Config;
4+
import com.convertapi.ConversionResult;
5+
import com.convertapi.ConvertApi;
6+
import com.convertapi.Param;
7+
8+
import java.io.IOException;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
11+
import java.util.List;
12+
import java.util.concurrent.CompletableFuture;
13+
import java.util.concurrent.ExecutionException;
14+
15+
/**
16+
* Example of extracting first page from PDF and then chaining conversion PDF page to JPG.
17+
* https://www.convertapi.com/pdf-to-extract
18+
* https://www.convertapi.com/pdf-to-jpg
19+
*/
20+
21+
public class CreatePdfThumbnail {
22+
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
23+
Config.setDefaultSecret("YOUR API SECRET"); //Get your secret at https://www.convertapi.com/a
24+
Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
25+
26+
System.out.println("Creating PDF thumbnail");
27+
28+
CompletableFuture<ConversionResult> pdfFirstPageResult = ConvertApi.convert("pdf", "extract", new Param[]{
29+
new Param("file", Paths.get("test-files/test.pdf")),
30+
new Param("pagerange", "1")
31+
});
32+
33+
CompletableFuture<ConversionResult> thumbnailResult = ConvertApi.convert("pdf", "jpg", new Param[]{
34+
new Param("file", pdfFirstPageResult),
35+
new Param("scaleimage","true"),
36+
new Param("scaleproportions","true"),
37+
new Param("imageheight",300)
38+
});
39+
40+
System.out.println("JPG thumbnail file saved to: " + thumbnailResult.get().saveFile(tempDir).get());
41+
}
42+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.convertapi.examples;
2+
3+
import com.convertapi.Config;
4+
import com.convertapi.ConversionResult;
5+
import com.convertapi.ConvertApi;
6+
import com.convertapi.Param;
7+
import com.convertapi.model.User;
8+
9+
import java.io.IOException;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
12+
import java.util.List;
13+
import java.util.concurrent.CompletableFuture;
14+
import java.util.concurrent.ExecutionException;
15+
16+
/**
17+
* Retrieve user information
18+
* https://www.convertapi.com/doc/user
19+
*/
20+
21+
public class UserInformation {
22+
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
23+
Config.setDefaultSecret("YOUR API SECRET"); //Get your secret at https://www.convertapi.com/a
24+
User user = ConvertApi.getUser();
25+
26+
System.out.println("API Key: " + user.ApiKey);
27+
System.out.println("Secret: " + user.Secret);
28+
System.out.println("Email: " + user.Email);
29+
System.out.println("Name: " + user.FullName);
30+
System.out.println("Status: " + user.Status);
31+
System.out.println("Active: " + user.Active);
32+
System.out.println("Seconds left: " + user.SecondsLeft);
33+
}
34+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.convertapi.model;
2+
3+
public class User {
4+
public String Secret;
5+
public int ApiKey;
6+
public boolean Active;
7+
public String FullName;
8+
public String Email;
9+
public int SecondsLeft;
10+
public String Status;
11+
}

0 commit comments

Comments
 (0)