Skip to content

Commit 9d67c3e

Browse files
author
Dominik Schürmann
committed
Remove dependency on OkHttp, make it more modular
1 parent 0b0a605 commit 9d67c3e

16 files changed

+201
-141
lines changed

Lib/build.gradle

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
apply plugin: 'com.android.library'
22

3-
dependencies {
4-
compile 'com.squareup.okhttp:okhttp:2.4.0'
5-
}
6-
73
android {
84
compileSdkVersion rootProject.ext.compileSdkVersion
95
buildToolsVersion rootProject.ext.buildToolsVersion
106

117
defaultConfig {
128
minSdkVersion 9
13-
targetSdkVersion 19
9+
targetSdkVersion 22
1410
}
15-
11+
1612
lintOptions {
1713
abortOnError false
1814
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (C) 2015 Dominik Schürmann <[email protected]>
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.textuality.keybase.lib;
19+
20+
import java.io.IOException;
21+
import java.net.HttpURLConnection;
22+
import java.net.Proxy;
23+
import java.net.URL;
24+
25+
public class DefaultKeybaseUrlConnectionClient implements KeybaseUrlConnectionClient {
26+
27+
@Override
28+
public HttpURLConnection openConnection(URL url) throws IOException {
29+
return openConnection(url, null);
30+
}
31+
32+
@Override
33+
public HttpURLConnection openConnection(URL url, Proxy proxy) throws IOException {
34+
HttpURLConnection conn;
35+
if (proxy == null) {
36+
conn = (HttpURLConnection) url.openConnection();
37+
conn.setConnectTimeout(5000);
38+
conn.setReadTimeout(25000);
39+
} else {
40+
conn = (HttpURLConnection) url.openConnection(proxy);
41+
conn.setConnectTimeout(30000);
42+
conn.setReadTimeout(40000);
43+
}
44+
return conn;
45+
}
46+
}

Lib/src/main/java/com/textuality/keybase/lib/JWalk.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class JWalk {
4040
* @param json The object
4141
* @param path list of string object member selectors
4242
* @return the int addressed by the path, assuming such a thing exists
43-
* @throws JSONException if any step in the path doesn���t work
43+
* @throws JSONException if any step in the path doesn't work
4444
*/
4545
public static int getInt(JSONObject json, String... path) throws JSONException {
4646
json = walk(json, path);
@@ -53,7 +53,7 @@ public static int getInt(JSONObject json, String... path) throws JSONException {
5353
* @param json The object
5454
* @param path list of string object member selectors
5555
* @return the int addressed by the path, assuming such a thing exists
56-
* @throws JSONException if any step in the path doesn���t work
56+
* @throws JSONException if any step in the path doesn't work
5757
*/
5858
public static long getLong(JSONObject json, String... path) throws JSONException {
5959
json = walk(json, path);
@@ -66,7 +66,7 @@ public static long getLong(JSONObject json, String... path) throws JSONException
6666
* @param json The object
6767
* @param path list of string object member selectors
6868
* @return the int addressed by the path, assuming such a thing exists
69-
* @throws JSONException if any step in the path doesn���t work
69+
* @throws JSONException if any step in the path doesn't work
7070
*/
7171
public static String getString(JSONObject json, String... path) throws JSONException {
7272
json = walk(json, path);
@@ -79,7 +79,7 @@ public static String getString(JSONObject json, String... path) throws JSONExcep
7979
* @param json The object
8080
* @param path list of string object member selectors
8181
* @return the int addressed by the path, assuming such a thing exists
82-
* @throws JSONException if any step in the path doesn���t work
82+
* @throws JSONException if any step in the path doesn't work
8383
*/
8484
public static JSONArray getArray(JSONObject json, String... path) throws JSONException {
8585
json = walk(json, path);
@@ -92,7 +92,7 @@ public static JSONArray getArray(JSONObject json, String... path) throws JSONExc
9292
* @param json The object
9393
* @param path list of string object member selectors
9494
* @return the int addressed by the path, assuming such a thing exists
95-
* @throws JSONException if any step in the path, except for the last, doesn���t work
95+
* @throws JSONException if any step in the path, except for the last, doesn't work
9696
*/
9797
public static JSONObject optObject(JSONObject json, String... path) throws JSONException {
9898
json = walk(json, path);
@@ -105,7 +105,7 @@ public static JSONObject optObject(JSONObject json, String... path) throws JSONE
105105
* @param json The object
106106
* @param path list of string object member selectors
107107
* @return the int addressed by the path, assuming such a thing exists
108-
* @throws JSONException if any step in the path doesn���t work
108+
* @throws JSONException if any step in the path doesn't work
109109
*/
110110
public static JSONObject getObject(JSONObject json, String... path) throws JSONException {
111111
json = walk(json, path);
@@ -121,7 +121,7 @@ private static JSONObject walk(JSONObject json, String... path) throws JSONExcep
121121
pathIndex++;
122122
}
123123
} catch (JSONException e) {
124-
// try to give ���em a nice-looking error
124+
// try to give them a nice-looking error
125125
StringBuilder sb = new StringBuilder();
126126
for (int i = 0; i < len; i++) {
127127
sb.append(path[i]).append('.');

Lib/src/main/java/com/textuality/keybase/lib/Search.java renamed to Lib/src/main/java/com/textuality/keybase/lib/KeybaseQuery.java

Lines changed: 66 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,62 +16,60 @@
1616
*/
1717
package com.textuality.keybase.lib;
1818

19-
import android.util.Log;
19+
import com.textuality.keybase.lib.prover.Fetch;
2020

21-
import com.squareup.okhttp.OkHttpClient;
22-
import com.squareup.okhttp.Request;
23-
import com.squareup.okhttp.Response;
2421
import org.json.JSONArray;
2522
import org.json.JSONException;
2623
import org.json.JSONObject;
2724

2825
import java.io.ByteArrayOutputStream;
2926
import java.io.IOException;
3027
import java.io.InputStream;
28+
import java.net.HttpURLConnection;
29+
import java.net.MalformedURLException;
3130
import java.net.Proxy;
3231
import java.net.URL;
3332
import java.net.URLEncoder;
3433
import java.util.Iterator;
3534
import java.util.NoSuchElementException;
36-
import java.util.concurrent.TimeUnit;
3735

38-
public class Search {
39-
36+
public class KeybaseQuery {
37+
4038
private static final String TAG = "KEYBASE-LIB";
4139

42-
public static Iterable<Match> search(String query, Proxy proxy) throws KeybaseException {
43-
JSONObject result = getFromKeybase("_/api/1.0/user/autocomplete.json?q=", query, proxy);
40+
private KeybaseUrlConnectionClient connectionClient;
41+
private Proxy proxy;
42+
43+
public KeybaseQuery(KeybaseUrlConnectionClient connectionClient) {
44+
this.connectionClient = connectionClient;
45+
}
46+
47+
public void setProxy(Proxy proxy) {
48+
this.proxy = proxy;
49+
}
50+
51+
public Iterable<Match> search(String query) throws KeybaseException {
52+
JSONObject result = getFromKeybase("_/api/1.0/user/autocomplete.json?q=", query);
4453
try {
4554
return new MatchIterator(JWalk.getArray(result, "completions"));
4655
} catch (JSONException e) {
4756
throw KeybaseException.keybaseScrewup(e);
4857
}
4958
}
5059

51-
public static JSONObject getFromKeybase(String path, String query, Proxy proxy) throws KeybaseException {
60+
public JSONObject getFromKeybase(String path, String query) throws KeybaseException {
5261
try {
5362
String url = "https://keybase.io/" + path + URLEncoder.encode(query, "utf8");
5463

5564
URL realUrl = new URL(url);
5665

57-
OkHttpClient client = new OkHttpClient();
58-
client.setProxy(proxy);
66+
HttpURLConnection conn = (HttpURLConnection) connectionClient.openConnection(realUrl, proxy);
67+
conn.connect();
5968

60-
if (proxy != null) {
61-
client.setConnectTimeout(30000, TimeUnit.MILLISECONDS);
62-
client.setReadTimeout(40000, TimeUnit.MILLISECONDS);
63-
} else {
64-
client.setConnectTimeout(5000, TimeUnit.MILLISECONDS); // TODO: Reasonable values for keybase
65-
client.setReadTimeout(25000, TimeUnit.MILLISECONDS);
66-
}
67-
68-
Response resp = client.newCall(new Request.Builder().url(realUrl).build()).execute();
69-
70-
int response = resp.code();
71-
72-
String text = resp.body().string();
69+
int response = conn.getResponseCode();
7370

7471
if (response >= 200 && response < 300) {
72+
String text = snarf(conn.getInputStream());
7573
try {
7674
JSONObject json = new JSONObject(text);
7775
if (JWalk.getInt(json, "status", "code") != 0) {
@@ -83,7 +81,8 @@ public static JSONObject getFromKeybase(String path, String query, Proxy proxy)
8381
throw KeybaseException.keybaseScrewup(e);
8482
}
8583
} else {
86-
throw KeybaseException.networkScrewup("Keybase.io query error (status=" + response + "): " + text);
84+
String message = snarf(conn.getErrorStream());
85+
throw KeybaseException.networkScrewup("Keybase.io query error (status=" + response + "): " + message);
8786
}
8887
} catch (Exception e) {
8988
throw KeybaseException.networkScrewup(e);
@@ -95,25 +94,62 @@ public static String snarf(InputStream in)
9594
byte[] buf = new byte[1024];
9695
int count = 0;
9796
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
98-
while ((count = in.read(buf)) != -1)
97+
while ((count = in.read(buf)) != -1)
9998
out.write(buf, 0, count);
10099
return out.toString();
101100
}
102101

102+
public static final int REDIRECT_TRIES = 5;
103+
104+
public Fetch fetchProof(String urlString) {
105+
Fetch result = new Fetch();
106+
107+
try {
108+
HttpURLConnection conn = null;
109+
int status = 0;
110+
int redirects = 0;
111+
while (redirects < REDIRECT_TRIES) {
112+
result.mActualUrl = urlString;
113+
URL url = new URL(urlString);
114+
conn = (HttpURLConnection) connectionClient.openConnection(url, proxy);
115+
conn.addRequestProperty("User-Agent", "Keybase Java client, github.com/timbray/KeybaseLib");
116+
conn.connect();
117+
status = conn.getResponseCode();
118+
if (status == 301) {
119+
redirects++;
120+
urlString = conn.getHeaderFields().get("Location").get(0);
121+
} else {
122+
break;
123+
}
124+
}
125+
if (status >= 200 && status < 300) {
126+
result.mBody = KeybaseQuery.snarf(conn.getInputStream());
127+
} else {
128+
result.mProblem = "Fetch failed, status " + status + ": " + KeybaseQuery.snarf(conn.getErrorStream());
129+
}
130+
131+
} catch (MalformedURLException e) {
132+
result.mProblem = "Bad URL: " + urlString;
133+
} catch (IOException e) {
134+
result.mProblem = "Network error: " + e.getLocalizedMessage();
135+
}
136+
137+
return result;
138+
}
139+
103140
static class MatchIterator implements Iterable<Match>, Iterator<Match> {
104141

105142
private final JSONArray mMatches;
106143
private int mLastIndex;
107144
private Match mNextMatch;
108145

109146
public MatchIterator(JSONArray matches) throws KeybaseException {
110-
Log.d(TAG, "match count=" + matches.length());
111147
mMatches = matches;
112148
mLastIndex = -1;
113149
mNextMatch = null;
114150
hasNext();
115151
}
116-
152+
117153
// caches mNextMatch but not the index
118154
private int findNext() {
119155
try {
@@ -148,7 +184,7 @@ public Match next() {
148184

149185
@Override
150186
public void remove() {
151-
throw new RuntimeException("UserIterator.remove() not supported");
187+
throw new RuntimeException("UserIterator.remove() not supported");
152188
}
153189

154190
@Override
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (C) 2015 Dominik Schürmann <[email protected]>
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.textuality.keybase.lib;
19+
20+
import java.io.IOException;
21+
import java.net.Proxy;
22+
import java.net.URL;
23+
import java.net.URLConnection;
24+
25+
/**
26+
* wrapper
27+
*/
28+
public interface KeybaseUrlConnectionClient {
29+
30+
URLConnection openConnection(URL url) throws IOException;
31+
URLConnection openConnection(URL url, Proxy proxy) throws IOException;
32+
}

Lib/src/main/java/com/textuality/keybase/lib/User.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,26 @@
2020
import org.json.JSONException;
2121
import org.json.JSONObject;
2222

23-
import java.net.Proxy;
2423
import java.util.Iterator;
2524

2625
public class User {
2726

2827
private final JSONObject mJson;
2928

30-
public static User findByUsername(String username, Proxy proxy) throws KeybaseException {
31-
JSONObject json = Search.getFromKeybase("_/api/1.0/user/lookup.json?username=", username, proxy);
29+
public static User findByUsername(KeybaseQuery keybaseQuery, String username) throws KeybaseException {
30+
JSONObject json = keybaseQuery.getFromKeybase("_/api/1.0/user/lookup.json?username=", username);
3231
try {
3332
json = JWalk.getObject(json, "them");
3433
} catch (JSONException e) {
3534
throw KeybaseException.keybaseScrewup(e);
3635
}
3736
return new User(json);
3837
}
39-
public static String keyForUsername(String username, Proxy proxy) throws KeybaseException {
40-
return findByUsername(username, proxy).getKey();
38+
public static String keyForUsername(KeybaseQuery keybaseQuery, String username) throws KeybaseException {
39+
return findByUsername(keybaseQuery, username).getKey();
4140
}
42-
public static User findByFingerprint(String fingerprint, Proxy proxy) throws KeybaseException {
43-
JSONObject json = Search.getFromKeybase("_/api/1.0/user/lookup.json?key_fingerprint=", fingerprint, proxy);
41+
public static User findByFingerprint(KeybaseQuery keybaseQuery, String fingerprint) throws KeybaseException {
42+
JSONObject json = keybaseQuery.getFromKeybase("_/api/1.0/user/lookup.json?key_fingerprint=", fingerprint);
4443
try {
4544
JSONArray them = JWalk.getArray(json, "them");
4645
if (them.length() != 1) {

0 commit comments

Comments
 (0)