/
OkHttpClientAdapter.java
142 lines (115 loc) · 5.07 KB
/
OkHttpClientAdapter.java
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
package com.github.yeriomin.playstoreapi;
import okhttp3.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
class OkHttpClientAdapter extends HttpClientAdapter {
OkHttpClient client;
public OkHttpClientAdapter() {
setClient(new OkHttpClient.Builder()
.connectTimeout(6, TimeUnit.SECONDS)
.readTimeout(6, TimeUnit.SECONDS)
.cookieJar(new CookieJar() {
private final HashMap<HttpUrl, List<Cookie>> cookieStore = new HashMap<HttpUrl, List<Cookie>>();
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
cookieStore.put(url, cookies);
}
public List<Cookie> loadForRequest(HttpUrl url) {
List<Cookie> cookies = cookieStore.get(url);
return cookies != null ? cookies : new ArrayList<Cookie>();
}
})
.build()
);
}
public void setClient(OkHttpClient client) {
this.client = client;
}
@Override
public byte[] getEx(String url, Map<String, List<String>> params, Map<String, String> headers) throws IOException {
return request(new Request.Builder().url(buildUrlEx(url, params)).get(), headers);
}
@Override
public byte[] get(String url, Map<String, String> params, Map<String, String> headers) throws IOException {
return request(new Request.Builder().url(buildUrl(url, params)).get(), headers);
}
@Override
public byte[] postWithoutBody(String url, Map<String, String> urlParams, Map<String, String> headers) throws IOException {
return post(buildUrl(url, urlParams), new HashMap<String, String>(), headers);
}
@Override
public byte[] post(String url, Map<String, String> params, Map<String, String> headers) throws IOException {
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
FormBody.Builder bodyBuilder = new FormBody.Builder();
if (null != params && !params.isEmpty()) {
for (String name: params.keySet()) {
bodyBuilder.add(name, params.get(name));
}
}
Request.Builder requestBuilder = new Request.Builder()
.url(url)
.post(bodyBuilder.build());
return post(url, requestBuilder, headers);
}
@Override
public byte[] post(String url, byte[] body, Map<String, String> headers) throws IOException {
if (!headers.containsKey("Content-Type")) {
headers.put("Content-Type", "application/x-protobuf");
}
Request.Builder requestBuilder = new Request.Builder()
.url(url)
.post(RequestBody.create(MediaType.parse("application/x-protobuf"), body));
return post(url, requestBuilder, headers);
}
byte[] post(String url, Request.Builder requestBuilder, Map<String, String> headers) throws IOException {
requestBuilder.url(url);
return request(requestBuilder, headers);
}
byte[] request(Request.Builder requestBuilder, Map<String, String> headers) throws IOException {
Request request = requestBuilder
.headers(Headers.of(headers))
.build();
System.out.println("Requesting: " + request.url().toString());
Response response = client.newCall(request).execute();
int code = response.code();
byte[] content = response.body().bytes();
if (code >= 400) {
GooglePlayException e = new GooglePlayException("Malformed request", code);
if (code == 401 || code == 403) {
e = new AuthException("Auth error", code);
Map<String, String> authResponse = GooglePlayAPI.parseResponse(new String(content));
if (authResponse.containsKey("Error") && authResponse.get("Error").equals("NeedsBrowser")) {
((AuthException) e).setTwoFactorUrl(authResponse.get("Url"));
}
} else if (code >= 500) {
e = new GooglePlayException("Server error", code);
}
e.setRawResponse(content);
throw e;
}
return content;
}
public String buildUrl(String url, Map<String, String> params) {
HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
if (null != params && !params.isEmpty()) {
for (String name: params.keySet()) {
urlBuilder.addQueryParameter(name, params.get(name));
}
}
return urlBuilder.build().toString();
}
public String buildUrlEx(String url, Map<String, List<String>> params) {
HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
if (null != params && !params.isEmpty()) {
for (String name: params.keySet()) {
for (String value: params.get(name)) {
urlBuilder.addQueryParameter(name, value);
}
}
}
return urlBuilder.build().toString();
}
}