-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathClient.java
More file actions
187 lines (158 loc) · 6.55 KB
/
Copy pathClient.java
File metadata and controls
187 lines (158 loc) · 6.55 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package com.tinify;
import com.google.gson.Gson;
import com.squareup.okhttp.*;
import java.io.IOException;
import java.net.Proxy;
import java.net.InetSocketAddress;
import java.net.Proxy.Type;
import java.net.URL;
import java.util.concurrent.TimeUnit;
public class Client {
private OkHttpClient client;
private String credentials;
private String userAgent;
public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
public static final String API_ENDPOINT = "https://api.tinify.com";
public static final String USER_AGENT = "Tinify/"
+ Client.class.getPackage().getImplementationVersion()
+ " Java/" + System.getProperty("java.version")
+ " (" + System.getProperty("java.vendor")
+ ", " + System.getProperty("os.name") + " " + System.getProperty("os.arch") + ")";
public enum Method {
POST,
GET
}
public Client(final String key) {
this(key, null, null);
}
public Client(final String key, final String appIdentifier) {
this(key, appIdentifier, null);
}
public Client(final String key, final String appIdentifier, final String proxy) {
client = new OkHttpClient();
if (proxy != null) {
try {
URL url = new URL(proxy);
Proxy proxyAddress = createProxyAddress(url);
Authenticator proxyAuthenticator = createProxyAuthenticator(url);
if (proxyAddress != null) {
client.setProxy(proxyAddress);
if (proxyAuthenticator != null) {
client.setAuthenticator(proxyAuthenticator);
}
}
} catch (java.lang.Exception e) {
throw new ConnectionException("Invalid proxy: " + e.getMessage(), e);
}
}
client.setSslSocketFactory(SSLContext.getSocketFactory());
client.setConnectTimeout(0, TimeUnit.SECONDS);
client.setReadTimeout(0, TimeUnit.SECONDS);
client.setWriteTimeout(0, TimeUnit.SECONDS);
credentials = Credentials.basic("api", key);
if (appIdentifier == null) {
userAgent = USER_AGENT;
} else {
userAgent = USER_AGENT + " " + appIdentifier;
}
}
public final Response request(final Method method, final String endpoint) throws Exception {
/* OkHttp does not support null request bodies if the method is POST. */
if (method.equals(Method.POST)) {
return request(method, endpoint, RequestBody.create(null, new byte[] {}));
} else {
return request(method, endpoint, (RequestBody) null);
}
}
public final Response request(final Method method, final String endpoint, final Options options) throws Exception {
if (method.equals(Method.GET)) {
return request(method, endpoint, options.isEmpty() ? null : RequestBody.create(JSON, options.toJson()));
} else {
return request(method, endpoint, RequestBody.create(JSON, options.toJson()));
}
}
public final Response request(final Method method, final String endpoint, final byte[] body) throws Exception {
return request(method, endpoint, RequestBody.create(null, body));
}
private Proxy createProxyAddress(final URL proxy) {
if (proxy == null) return null;
String host = proxy.getHost();
int port = proxy.getPort();
if (port < 0) {
port = proxy.getDefaultPort();
}
return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
}
private Authenticator createProxyAuthenticator(final URL proxy) {
if (proxy == null) return null;
String user = proxy.getUserInfo();
if (user == null) return null;
final String username, password;
int c = user.indexOf(':');
if (0 < c) {
username = user.substring(0, c);
password = user.substring(c + 1);
} else {
username = user;
password = null;
}
return new Authenticator() {
@Override public Request authenticate(Proxy proxy, Response response) throws IOException {
return null;
}
@Override public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
String credential = Credentials.basic(username, password);
return response.request().newBuilder().header("Proxy-Authorization", credential).build();
}
};
}
private Response request(final Method method, final String endpoint, final RequestBody body) throws Exception {
HttpUrl url;
if (endpoint.startsWith("https")) {
url = HttpUrl.parse(endpoint);
} else {
url = HttpUrl.parse(API_ENDPOINT + endpoint);
}
Request request = new Request.Builder()
.header("Authorization", credentials)
.header("User-Agent", userAgent)
.url(url)
.method(method.toString(), body)
.build();
Response response;
try {
response = client.newCall(request).execute();
} catch (java.lang.Exception e) {
throw new ConnectionException("Error while connecting: " + e.getMessage(), e);
}
String compressionCount = response.header("Compression-Count");
if (compressionCount != null && !compressionCount.isEmpty()) {
Tinify.setCompressionCount(Integer.valueOf(compressionCount));
}
if (response.isSuccessful()) {
return response;
} else {
Exception.Data data;
Gson gson = new Gson();
try {
data = gson.fromJson(response.body().charStream(), Exception.Data.class);
if (data == null) {
data = new Exception.Data();
data.setMessage("Error while parsing response: received empty body");
data.setError("ParseError");
}
} catch (com.google.gson.JsonParseException e) {
data = new Exception.Data();
data.setMessage("Error while parsing response: " + e.getMessage());
data.setError("ParseError");
} catch (IOException e) {
throw new Exception(e);
}
throw Exception.create(
data.getMessage(),
data.getError(),
response.code());
}
}
}