Skip to content

Commit 911b261

Browse files
committed
No result methods
0 parents  commit 911b261

File tree

13 files changed

+419
-0
lines changed

13 files changed

+419
-0
lines changed

.idea/uiDesigner.xml

Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

convertapi-java.iml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="library" name="lib" level="project" />
11+
</component>
12+
</module>

lib/gson-2.8.5.jar

236 KB
Binary file not shown.

lib/okhttp-3.10.0.jar

402 KB
Binary file not shown.

lib/okio-1.14.1.jar

83.8 KB
Binary file not shown.

src/com/baltsoft/Config.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baltsoft;
2+
3+
public class Config {
4+
private static String defaultSecret;
5+
private static final String SCHEME = "https";
6+
private static final String HOST = "v2.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+
}

src/com/baltsoft/ConvertApi.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baltsoft;
2+
3+
import com.baltsoft.Model.ConversionResponse;
4+
import com.google.gson.Gson;
5+
import okhttp3.HttpUrl;
6+
import okhttp3.MultipartBody;
7+
import okhttp3.Request;
8+
import java.io.IOException;
9+
import java.util.Arrays;
10+
import java.util.List;
11+
import java.util.concurrent.CompletableFuture;
12+
13+
public class ConvertApi {
14+
private static final List<String> IGNORE_PARAMS = Arrays.asList( "storefile", "async", "jobid", "timeout");
15+
16+
public static CompletableFuture<ConversionResponse> convert(String fromFormat, String toFormat, Param[] params) {
17+
return convert(fromFormat, toFormat, params, Config.defaults());
18+
}
19+
20+
public static CompletableFuture<ConversionResponse> convert(String fromFormat, String toFormat, Param[] params, Config config) {
21+
return CompletableFuture.supplyAsync(() -> {
22+
HttpUrl url = Http.getUrlBuilder(config)
23+
.addPathSegment(fromFormat)
24+
.addPathSegment("to")
25+
.addPathSegment(toFormat)
26+
.addQueryParameter("storefile", "true")
27+
.build();
28+
29+
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
30+
for (Param param: params) {
31+
if (!IGNORE_PARAMS.contains(param.getName())) {
32+
try {
33+
String[] values = param.getValues();
34+
if (values.length == 1) {
35+
multipartBuilder.addFormDataPart(param.getName(), values[0]);
36+
} else {
37+
for (int i = 0; i < values.length; i++) {
38+
multipartBuilder.addFormDataPart(param.getName() + "[" + i + "]", values[i]);
39+
}
40+
}
41+
} catch (Exception e) {
42+
e.printStackTrace();
43+
}
44+
}
45+
}
46+
47+
Request request = new Request.Builder()
48+
.url(url)
49+
.post(multipartBuilder.build())
50+
.build();
51+
52+
String bodyString = null;
53+
try {
54+
bodyString = Http.getClient().newCall(request).execute().body().string();
55+
} catch (IOException e) {
56+
e.printStackTrace();
57+
}
58+
59+
return new Gson().fromJson(bodyString, ConversionResponse.class);
60+
});
61+
}
62+
}

src/com/baltsoft/Http.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baltsoft;
2+
3+
import okhttp3.HttpUrl;
4+
import okhttp3.OkHttpClient;
5+
6+
public class Http {
7+
private static OkHttpClient client = new OkHttpClient();
8+
9+
public static OkHttpClient getClient() {
10+
return client;
11+
}
12+
13+
public static HttpUrl.Builder getUrlBuilder(Config config) {
14+
return new HttpUrl.Builder()
15+
.scheme(config.getScheme())
16+
.host(config.getHost())
17+
.addQueryParameter("timeout", String.valueOf(config.getTimeout()))
18+
.addQueryParameter("secret", config.getSecret());
19+
}
20+
}

src/com/baltsoft/Main.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baltsoft;
2+
3+
import com.baltsoft.Model.ConversionResponse;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.net.URISyntaxException;
8+
import java.util.concurrent.CompletableFuture;
9+
import java.util.concurrent.ExecutionException;
10+
11+
public class Main {
12+
13+
public static void main(String[] args) throws IOException, URISyntaxException, ExecutionException, InterruptedException {
14+
Config.setDefaultSecret("");
15+
CompletableFuture<ConversionResponse> rez = ConvertApi.convert("docx", "pdf", new Param[]{new Param("File", new File("/home/jon/trinti/test.docx"))});
16+
17+
String s = ""; //f.get();
18+
String a = s;
19+
Thread.sleep(100000);
20+
21+
22+
}
23+
24+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baltsoft.Model;
2+
3+
public class ConversionResponse {
4+
public int ConversionCost = 0;
5+
public ConversionResponseFile[] Files = null;
6+
}
7+

0 commit comments

Comments
 (0)