Skip to content

Commit 0a99c13

Browse files
author
Dominik Schürmann
committed
Merge pull request #2 from kepuss/URLConnection
Response wrapper for KeybaseLib (for open-keychain okhttp3)
2 parents bc02742 + 5787614 commit 0a99c13

File tree

3 files changed

+53
-24
lines changed

3 files changed

+53
-24
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
public class DefaultKeybaseUrlConnectionClient implements KeybaseUrlConnectionClient {
2626

2727
@Override
28-
public HttpURLConnection openConnection(URL url, Proxy proxy, boolean isKeybase) throws IOException {
28+
public Response getUrlResponse(URL url, Proxy proxy, boolean isKeybase) throws IOException {
2929
HttpURLConnection conn;
3030
if (proxy == null) {
3131
conn = (HttpURLConnection) url.openConnection();
@@ -36,7 +36,10 @@ public HttpURLConnection openConnection(URL url, Proxy proxy, boolean isKeybase)
3636
conn.setConnectTimeout(30000);
3737
conn.setReadTimeout(40000);
3838
}
39-
return conn;
39+
conn.connect();
40+
41+
42+
return new Response(conn.getInputStream(),conn.getResponseCode(),conn.getResponseMessage(),conn.getHeaderFields());
4043
}
4144

4245
@Override

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

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.io.ByteArrayOutputStream;
2626
import java.io.IOException;
2727
import java.io.InputStream;
28-
import java.net.HttpURLConnection;
2928
import java.net.MalformedURLException;
3029
import java.net.Proxy;
3130
import java.net.URL;
@@ -62,14 +61,9 @@ public JSONObject getFromKeybase(String path, String query) throws KeybaseExcept
6261
String url = connectionClient.getKeybaseBaseUrl() + path + URLEncoder.encode(query, "utf8");
6362

6463
URL realUrl = new URL(url);
65-
66-
HttpURLConnection conn = (HttpURLConnection) connectionClient.openConnection(realUrl, proxy, true);
67-
conn.connect();
68-
69-
int response = conn.getResponseCode();
70-
71-
if (response >= 200 && response < 300) {
72-
String text = snarf(conn.getInputStream());
64+
KeybaseUrlConnectionClient.Response response = connectionClient.getUrlResponse(realUrl, proxy, true);
65+
if (response.getCode() >= 200 && response.getCode() < 300) {
66+
String text = snarf(response.getStream());
7367
try {
7468
JSONObject json = new JSONObject(text);
7569
if (JWalk.getInt(json, "status", "code") != 0) {
@@ -81,7 +75,7 @@ public JSONObject getFromKeybase(String path, String query) throws KeybaseExcept
8175
throw KeybaseException.keybaseScrewup(e);
8276
}
8377
} else {
84-
String message = snarf(conn.getErrorStream());
78+
String message = response.getMessage();
8579
throw KeybaseException.networkScrewup("api.keybase.io query error (status=" + response + "): " + message);
8680
}
8781
} catch (Exception e) {
@@ -103,27 +97,23 @@ public Fetch fetchProof(String urlString) {
10397
Fetch result = new Fetch();
10498

10599
try {
106-
HttpURLConnection conn = null;
107-
int status = 0;
100+
KeybaseUrlConnectionClient.Response response = null;
108101
int redirects = 0;
109102
while (redirects < REDIRECT_TRIES) {
110103
result.mActualUrl = urlString;
111104
URL url = new URL(urlString);
112-
conn = (HttpURLConnection) connectionClient.openConnection(url, proxy, false);
113-
conn.addRequestProperty("User-Agent", "Keybase Java client, github.com/timbray/KeybaseLib");
114-
conn.connect();
115-
status = conn.getResponseCode();
116-
if (status == 301) {
105+
response = connectionClient.getUrlResponse(url, proxy, false);
106+
if (response.getCode() == 301) {
117107
redirects++;
118-
urlString = conn.getHeaderFields().get("Location").get(0);
108+
urlString = response.getHeaders().get("Location").get(0);
119109
} else {
120110
break;
121111
}
122112
}
123-
if (status >= 200 && status < 300) {
124-
result.mBody = KeybaseQuery.snarf(conn.getInputStream());
113+
if (response.getCode() >= 200 && response.getCode() < 300) {
114+
result.mBody = KeybaseQuery.snarf(response.getStream());
125115
} else {
126-
result.mProblem = "Fetch failed, status " + status + ": " + KeybaseQuery.snarf(conn.getErrorStream());
116+
result.mProblem = "Fetch failed, status " + response.getCode() + ": " + response.getMessage();
127117
}
128118

129119
} catch (MalformedURLException e) {

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,51 @@
1818
package com.textuality.keybase.lib;
1919

2020
import java.io.IOException;
21+
import java.io.InputStream;
2122
import java.net.Proxy;
2223
import java.net.URL;
2324
import java.net.URLConnection;
25+
import java.util.List;
26+
import java.util.Map;
2427

2528
/**
2629
* wrapper
2730
*/
2831
public interface KeybaseUrlConnectionClient {
2932

30-
URLConnection openConnection(URL url, Proxy proxy, boolean isKeybase) throws IOException;
33+
Response getUrlResponse(URL url, Proxy proxy, boolean isKeybase) throws IOException;
3134
String getKeybaseBaseUrl();
35+
36+
class Response {
37+
private final InputStream stream;
38+
private final int code;
39+
private final String message;
40+
private final Map<String, List<String>> headers;
41+
42+
public Response(InputStream stream, int code, String message, Map<String, List<String>> headers) {
43+
if (stream == null) {
44+
throw new IllegalArgumentException("Stream may not be null.");
45+
}
46+
this.stream = stream;
47+
this.code = code;
48+
this.message = message;
49+
this.headers = headers;
50+
}
51+
52+
public InputStream getStream() {
53+
return stream;
54+
}
55+
56+
public int getCode() {
57+
return code;
58+
}
59+
60+
public String getMessage() {
61+
return message;
62+
}
63+
64+
public Map<String, List<String>> getHeaders() {
65+
return headers;
66+
}
67+
}
3268
}

0 commit comments

Comments
 (0)