Skip to content

Commit 9ba2c09

Browse files
committed
Jar
1 parent 8380e91 commit 9ba2c09

File tree

12 files changed

+401
-0
lines changed

12 files changed

+401
-0
lines changed

convertapi-java.iml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10+
<excludeFolder url="file://$MODULE_DIR$/target" />
11+
</content>
12+
<orderEntry type="inheritedJdk" />
13+
<orderEntry type="sourceFolder" forTests="false" />
14+
<orderEntry type="library" name="Maven: com.squareup.okhttp3:okhttp:3.10.0" level="project" />
15+
<orderEntry type="library" name="Maven: com.squareup.okio:okio:1.14.0" level="project" />
16+
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.5" level="project" />
17+
</component>
18+
</module>

pom.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>convertapi</groupId>
8+
<artifactId>convertapi</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<build>
12+
<plugins>
13+
<plugin>
14+
<groupId>org.apache.maven.plugins</groupId>
15+
<artifactId>maven-compiler-plugin</artifactId>
16+
<configuration>
17+
<source>1.8</source>
18+
<target>1.8</target>
19+
</configuration>
20+
</plugin>
21+
</plugins>
22+
</build>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>com.squareup.okhttp3</groupId>
27+
<artifactId>okhttp</artifactId>
28+
<version>3.10.0</version>
29+
</dependency>
30+
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
31+
<dependency>
32+
<groupId>com.google.code.gson</groupId>
33+
<artifactId>gson</artifactId>
34+
<version>2.8.5</version>
35+
</dependency>
36+
</dependencies>
37+
</project>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Main-Class: com.convertapi.ConvertApi
3+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.convertapi;
2+
3+
public class Config {
4+
private static String defaultSecret;
5+
private static final String SCHEME = "https";
6+
private static final String HOST = "v2.com.convertapi.com";
7+
private static final int TIMEOUT = 180;
8+
private String scheme;
9+
private String host;
10+
private String secret;
11+
private int timeout;
12+
13+
public static void setDefaultSecret(String defaultSecret) {
14+
Config.defaultSecret = defaultSecret;
15+
}
16+
17+
public static Config defaults() {
18+
return new Config(Config.defaultSecret, SCHEME, HOST, TIMEOUT);
19+
}
20+
21+
public Config(String secret, String scheme, String host, int timeout) {
22+
this.scheme = scheme;
23+
this.host = host;
24+
this.secret = secret;
25+
this.timeout = timeout;
26+
}
27+
28+
public int getTimeout() {
29+
return timeout;
30+
}
31+
32+
public String getScheme() {
33+
return scheme;
34+
}
35+
36+
public String getHost() {
37+
return host;
38+
}
39+
40+
public String getSecret() {
41+
return secret;
42+
}
43+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.convertapi;
2+
3+
public class ConversionException extends RuntimeException{
4+
public ConversionException(String message){
5+
super(message);
6+
}
7+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.convertapi;
2+
3+
import com.convertapi.Model.ConversionResponse;
4+
5+
import java.nio.file.Files;
6+
import java.nio.file.NotDirectoryException;
7+
import java.nio.file.Path;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.concurrent.CompletableFuture;
11+
import java.util.concurrent.ExecutionException;
12+
13+
public class ConversionResult {
14+
private final CompletableFuture<ConversionResponse> responseFuture;
15+
16+
public ConversionResult(CompletableFuture<ConversionResponse> responseFuture) {
17+
this.responseFuture = responseFuture;
18+
}
19+
20+
public CompletableFuture<Integer> fileCount() throws ExecutionException, InterruptedException {
21+
return responseFuture.thenApplyAsync(r -> r.Files.length);
22+
}
23+
24+
public CompletableFuture<ConversionResultFile> getFile(int index) {
25+
return responseFuture.thenApplyAsync(r -> new ConversionResultFile(r.Files[index]));
26+
}
27+
28+
public Path saveFile(Path file) throws ExecutionException, InterruptedException {
29+
return getFile(0).thenCompose(f -> f.saveFile(file)).get();
30+
}
31+
32+
public List<Path> saveFiles(Path directory) throws ExecutionException, InterruptedException, NotDirectoryException {
33+
if (!Files.isDirectory(directory)) throw new NotDirectoryException(directory.toString());
34+
List<Path> paths = new ArrayList<Path>();
35+
for (int i = 0; i < responseFuture.get().Files.length; i++) {
36+
paths.add(getFile(i).thenCompose(f -> f.saveFile(directory)).get());
37+
}
38+
return paths;
39+
}
40+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.convertapi;
2+
3+
import com.convertapi.Model.ConversionResponseFile;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
import java.nio.file.StandardCopyOption;
11+
import java.util.concurrent.CompletableFuture;
12+
13+
public class ConversionResultFile {
14+
private final ConversionResponseFile conversionResponseFile;
15+
16+
public ConversionResultFile(ConversionResponseFile conversionResponseFile) {
17+
this.conversionResponseFile = conversionResponseFile;
18+
}
19+
20+
public String getName() {
21+
return conversionResponseFile.FileName;
22+
}
23+
24+
public int getSize() {
25+
return conversionResponseFile.FileSize;
26+
}
27+
28+
public String getUrl() {
29+
return conversionResponseFile.Url;
30+
}
31+
32+
public CompletableFuture<InputStream> asStream() {
33+
return Http.requestGet(getUrl());
34+
}
35+
36+
public CompletableFuture<Path> saveFile(Path path) {
37+
return asStream().thenApplyAsync(s -> {
38+
try {
39+
Path filePath = Files.isDirectory(path) ? Paths.get(path.toString(), getName()) : path;
40+
Files.copy(s, filePath, StandardCopyOption.REPLACE_EXISTING);
41+
return filePath;
42+
} catch (IOException e) {
43+
throw new RuntimeException(e);
44+
}
45+
});
46+
}
47+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.convertapi;
2+
3+
import com.convertapi.Model.ConversionResponse;
4+
import com.google.gson.Gson;
5+
import okhttp3.HttpUrl;
6+
import okhttp3.MultipartBody;
7+
import okhttp3.Request;
8+
import okhttp3.Response;
9+
10+
import java.io.IOException;
11+
import java.util.Arrays;
12+
import java.util.List;
13+
import java.util.concurrent.CompletableFuture;
14+
import java.util.concurrent.ExecutionException;
15+
16+
public class ConvertApi {
17+
private static final List<String> IGNORE_PARAMS = Arrays.asList( "storefile", "async", "jobid", "timeout");
18+
19+
public static ConversionResult convert(String fromFormat, String toFormat, Param[] params) {
20+
return convert(fromFormat, toFormat, params, Config.defaults());
21+
}
22+
23+
public static ConversionResult convert(String fromFormat, String toFormat, Param[] params, Config config) {
24+
CompletableFuture<ConversionResponse> completableResponse = CompletableFuture.supplyAsync(() -> {
25+
HttpUrl url = Http.getUrlBuilder(config)
26+
.addPathSegment(fromFormat)
27+
.addPathSegment("to")
28+
.addPathSegment(toFormat)
29+
.addQueryParameter("storefile", "true")
30+
.build();
31+
32+
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
33+
for (Param param: params) {
34+
if (!IGNORE_PARAMS.contains(param.getName())) {
35+
try {
36+
multipartBuilder.addFormDataPart(param.getName(), param.getValue());
37+
} catch (ExecutionException | InterruptedException e) {
38+
throw new RuntimeException(e);
39+
}
40+
}
41+
}
42+
43+
Request request = new Request.Builder()
44+
.url(url)
45+
.addHeader("Accept", "application/json")
46+
.post(multipartBuilder.build())
47+
.build();
48+
49+
String bodyString;
50+
try {
51+
Response response = Http.getClient().newCall(request).execute();
52+
bodyString = response.body().string();
53+
if (response.code() != 200) {
54+
throw new ConversionException(bodyString);
55+
}
56+
} catch (IOException e) {
57+
throw new RuntimeException(e);
58+
}
59+
60+
return new Gson().fromJson(bodyString, ConversionResponse.class);
61+
});
62+
63+
return new ConversionResult(completableResponse);
64+
}
65+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.convertapi;
2+
3+
import okhttp3.HttpUrl;
4+
import okhttp3.OkHttpClient;
5+
import okhttp3.Request;
6+
import okhttp3.Response;
7+
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.util.concurrent.CompletableFuture;
11+
12+
public class Http {
13+
private static OkHttpClient client = new OkHttpClient();
14+
15+
public static OkHttpClient getClient() {
16+
return client;
17+
}
18+
19+
public static HttpUrl.Builder getUrlBuilder(Config config) {
20+
return new HttpUrl.Builder()
21+
.scheme(config.getScheme())
22+
.host(config.getHost())
23+
.addQueryParameter("timeout", String.valueOf(config.getTimeout()))
24+
.addQueryParameter("secret", config.getSecret());
25+
}
26+
27+
public static CompletableFuture<InputStream> requestGet(String url) {
28+
return CompletableFuture.supplyAsync(() -> {
29+
Request request = new Request.Builder().url(url).build();
30+
Response response = null;
31+
try {
32+
response = getClient().newCall(request).execute();
33+
} catch (IOException e) {
34+
throw new RuntimeException(e);
35+
}
36+
return response.body().byteStream();
37+
});
38+
}
39+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.convertapi.Model;
2+
3+
public class ConversionResponse {
4+
public int ConversionCost = 0;
5+
public ConversionResponseFile[] Files = null;
6+
}

0 commit comments

Comments
 (0)