18
18
19
19
import android .util .Log ;
20
20
21
+ import com .squareup .okhttp .OkHttpClient ;
22
+ import com .squareup .okhttp .Request ;
23
+ import com .squareup .okhttp .Response ;
21
24
import org .json .JSONArray ;
22
25
import org .json .JSONException ;
23
26
import org .json .JSONObject ;
26
29
import java .io .IOException ;
27
30
import java .io .InputStream ;
28
31
import java .net .HttpURLConnection ;
32
+ import java .net .Proxy ;
29
33
import java .net .URL ;
30
34
import java .net .URLEncoder ;
35
+ import java .sql .Time ;
31
36
import java .util .Iterator ;
32
37
import java .util .NoSuchElementException ;
38
+ import java .util .concurrent .TimeUnit ;
33
39
34
40
public class Search {
35
41
36
42
private static final String TAG = "KEYBASE-LIB" ;
37
43
38
- public static Iterable <Match > search (String query ) throws KeybaseException {
39
- JSONObject result = getFromKeybase ("_/api/1.0/user/autocomplete.json?q=" , query );
44
+ public static Iterable <Match > search (String query , Proxy proxy ) throws KeybaseException {
45
+ JSONObject result = getFromKeybase ("_/api/1.0/user/autocomplete.json?q=" , query , proxy );
40
46
try {
41
47
return new MatchIterator (JWalk .getArray (result , "completions" ));
42
48
} catch (JSONException e ) {
43
49
throw KeybaseException .keybaseScrewup (e );
44
50
}
45
51
}
46
52
47
- public static JSONObject getFromKeybase (String path , String query ) throws KeybaseException {
53
+ public static JSONObject getFromKeybase (String path , String query , Proxy proxy ) throws KeybaseException {
48
54
try {
49
55
String url = "https://keybase.io/" + path + URLEncoder .encode (query , "utf8" );
50
56
51
57
URL realUrl = new URL (url );
52
- HttpURLConnection conn = (HttpURLConnection ) realUrl .openConnection ();
53
- conn .setConnectTimeout (5000 ); // TODO: Reasonable values for keybase
54
- conn .setReadTimeout (25000 );
55
- conn .connect ();
56
- int response = conn .getResponseCode ();
58
+
59
+ OkHttpClient client = new OkHttpClient ();
60
+ client .setProxy (proxy );
61
+
62
+ if (proxy != null ) {
63
+ client .setConnectTimeout (30000 , TimeUnit .MILLISECONDS );
64
+ client .setReadTimeout (40000 , TimeUnit .MILLISECONDS );
65
+ } else {
66
+ client .setConnectTimeout (5000 , TimeUnit .MILLISECONDS ); // TODO: Reasonable values for keybase
67
+ client .setReadTimeout (25000 , TimeUnit .MILLISECONDS );
68
+ }
69
+
70
+ tempIpTest (client );
71
+
72
+ Response resp = client .newCall (new Request .Builder ().url (realUrl ).build ()).execute ();
73
+
74
+ int response = resp .code ();
75
+
76
+ String text = resp .body ().string ();
77
+
57
78
if (response >= 200 && response < 300 ) {
58
- String text = snarf (conn .getInputStream ());
59
79
try {
60
80
JSONObject json = new JSONObject (text );
61
81
if (JWalk .getInt (json , "status" , "code" ) != 0 ) {
@@ -66,14 +86,18 @@ public static JSONObject getFromKeybase(String path, String query) throws Keybas
66
86
throw KeybaseException .keybaseScrewup (e );
67
87
}
68
88
} else {
69
- String message = snarf (conn .getErrorStream ());
70
- throw KeybaseException .networkScrewup ("Keybase.io query error (status=" + response + "): " + message );
89
+ throw KeybaseException .networkScrewup ("Keybase.io query error (status=" + response + "): " + text );
71
90
}
72
91
} catch (Exception e ) {
73
92
throw KeybaseException .networkScrewup (e );
74
93
}
75
94
}
76
95
96
+ public static void tempIpTest (OkHttpClient client ) throws IOException {
97
+ Log .e ("PHILIP" ,"ipTest: " + client .newCall (new Request .Builder ().url ("https://wtfismyip.com/text" ).build ())
98
+ .execute ().body ().string ());
99
+ }
100
+
77
101
public static String snarf (InputStream in )
78
102
throws IOException {
79
103
byte [] buf = new byte [1024 ];
0 commit comments