1818
1919import android .util .Log ;
2020
21+ import com .squareup .okhttp .OkHttpClient ;
22+ import com .squareup .okhttp .Request ;
23+ import com .squareup .okhttp .Response ;
2124import org .json .JSONArray ;
2225import org .json .JSONException ;
2326import org .json .JSONObject ;
2629import java .io .IOException ;
2730import java .io .InputStream ;
2831import java .net .HttpURLConnection ;
32+ import java .net .Proxy ;
2933import java .net .URL ;
3034import java .net .URLEncoder ;
35+ import java .sql .Time ;
3136import java .util .Iterator ;
3237import java .util .NoSuchElementException ;
38+ import java .util .concurrent .TimeUnit ;
3339
3440public class Search {
3541
3642 private static final String TAG = "KEYBASE-LIB" ;
3743
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 );
4046 try {
4147 return new MatchIterator (JWalk .getArray (result , "completions" ));
4248 } catch (JSONException e ) {
4349 throw KeybaseException .keybaseScrewup (e );
4450 }
4551 }
4652
47- public static JSONObject getFromKeybase (String path , String query ) throws KeybaseException {
53+ public static JSONObject getFromKeybase (String path , String query , Proxy proxy ) throws KeybaseException {
4854 try {
4955 String url = "https://keybase.io/" + path + URLEncoder .encode (query , "utf8" );
5056
5157 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+
5778 if (response >= 200 && response < 300 ) {
58- String text = snarf (conn .getInputStream ());
5979 try {
6080 JSONObject json = new JSONObject (text );
6181 if (JWalk .getInt (json , "status" , "code" ) != 0 ) {
@@ -66,14 +86,18 @@ public static JSONObject getFromKeybase(String path, String query) throws Keybas
6686 throw KeybaseException .keybaseScrewup (e );
6787 }
6888 } 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 );
7190 }
7291 } catch (Exception e ) {
7392 throw KeybaseException .networkScrewup (e );
7493 }
7594 }
7695
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+
77101 public static String snarf (InputStream in )
78102 throws IOException {
79103 byte [] buf = new byte [1024 ];
0 commit comments