Repository navigation
Expand file tree
/
Copy pathConvertApi.java
More file actions
152 lines (129 loc) · 5.82 KB
/
Copy pathConvertApi.java
File metadata and controls
152 lines (129 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.convertapi;
import com.convertapi.model.ConversionResponse;
import com.convertapi.model.User;
import com.google.gson.Gson;
import okhttp3.HttpUrl;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
@SuppressWarnings("WeakerAccess")
public class ConvertApi {
@SuppressWarnings("SpellCheckingInspection")
private static final List<String> IGNORE_PARAMS = Arrays.asList( "storefile", "async", "jobid", "timeout");
@SuppressWarnings("unused")
public static CompletableFuture<ConversionResult> convert(String fromFormat, String toFormat, Param... params) {
return convert(fromFormat, toFormat, params, Config.defaults());
}
public static CompletableFuture<ConversionResult> convert(String fromFormat, String toFormat, Param[] params, Config config) {
CompletableFuture<ConversionResponse> completableResponse = CompletableFuture.supplyAsync(() -> {
@SuppressWarnings("SpellCheckingInspection")
HttpUrl url = Http.getUrlBuilder(config)
.addPathSegment("convert")
.addPathSegment(fromFormat)
.addPathSegment("to")
.addPathSegment(toFormat)
.addQueryParameter("storefile", "true")
.build();
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
HashMap<String, List<String>> paramValues = getParamValues(params);
for (String name : paramValues.keySet()) {
if (!IGNORE_PARAMS.contains(name)) {
List<String> values = paramValues.get(name);
if (paramValues.get(name).size() == 1) {
multipartBuilder.addFormDataPart(name, values.get(0));
} else {
for (int i = 0; i < values.size(); i++) {
multipartBuilder.addFormDataPart(name + "[" + i + "]", values.get(i));
}
}
}
}
Request request = Http.getRequestBuilder()
.url(url)
.addHeader("Accept", "application/json")
.post(multipartBuilder.build())
.build();
String bodyString;
try {
Response response = Http.getClient(config).newCall(request).execute();
//noinspection ConstantConditions
bodyString = response.body().string();
if (response.code() != 200) {
throw new ConversionException(bodyString, response.code());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return new Gson().fromJson(bodyString, ConversionResponse.class);
});
return completableResponse.thenApply(ConversionResult::new);
}
@SuppressWarnings("unused")
public static User getUser() {
return getUser(Config.defaults());
}
public static User getUser(Config config) {
HttpUrl url = Http.getUrlBuilder(config).addPathSegment("user").build();
Request request = Http.getRequestBuilder()
.url(url)
.addHeader("Accept", "application/json")
.build();
String bodyString;
try {
Response response = Http.getClient().newCall(request).execute();
//noinspection ConstantConditions
bodyString = response.body().string();
if (response.code() != 200) {
throw new ConversionException(bodyString, response.code());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return new Gson().fromJson(bodyString, User.class);
}
@SuppressWarnings("unused")
public static CompletableFuture<ConversionResult> convert(Path fromFile, String toFormat) throws IOException {
return convert(fromFile, toFormat, Config.defaults().getSecret());
}
public static CompletableFuture<ConversionResult> convert(Path fromFile, String toFormat, String secret) throws IOException {
return convert(getFileExtension(fromFile), toFormat, new Param[]{new Param("file", fromFile)}, Config.defaults(secret));
}
@SuppressWarnings("unused")
public static void convert(String fromPathToFile, String toPathToFile) {
convert(fromPathToFile, toPathToFile, Config.defaults().getSecret());
}
public static void convert(String fromPathToFile, String toPathToFile, String secret) {
try {
Path fromPath = Paths.get(fromPathToFile);
Path toPath = Paths.get(toPathToFile);
convert(fromPath, getFileExtension(toPath), secret).get().saveFile(toPath).get();
} catch (IOException | ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
}
private static String getFileExtension(Path path) {
String name = path.getFileName().toString();
return name.substring(name.lastIndexOf(".") + 1);
}
private static HashMap<String, List<String>> getParamValues(Param[] params) {
HashMap<String, List<String>> result = new HashMap<>();
try {
for (Param param : params) {
List<String> values = result.computeIfAbsent(param.getName(), (v) -> new ArrayList<>());
values.addAll(param.getValue());
}
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
return result;
}
}