-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSearch.java
More file actions
159 lines (136 loc) · 5.36 KB
/
Search.java
File metadata and controls
159 lines (136 loc) · 5.36 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright (C) 2014 Tim Bray <tbray@textuality.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.textuality.keybase.lib;
import android.util.Log;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Proxy;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
public class Search {
private static final String TAG = "KEYBASE-LIB";
public static Iterable<Match> search(String query, Proxy proxy) throws KeybaseException {
JSONObject result = getFromKeybase("_/api/1.0/user/autocomplete.json?q=", query, proxy);
try {
return new MatchIterator(JWalk.getArray(result, "completions"));
} catch (JSONException e) {
throw KeybaseException.keybaseScrewup(e);
}
}
public static JSONObject getFromKeybase(String path, String query, Proxy proxy) throws KeybaseException {
try {
String url = "https://keybase.io/" + path + URLEncoder.encode(query, "utf8");
URL realUrl = new URL(url);
OkHttpClient client = new OkHttpClient();
client.setProxy(proxy);
if (proxy != null) {
client.setConnectTimeout(30000, TimeUnit.MILLISECONDS);
client.setReadTimeout(40000, TimeUnit.MILLISECONDS);
} else {
client.setConnectTimeout(5000, TimeUnit.MILLISECONDS); // TODO: Reasonable values for keybase
client.setReadTimeout(25000, TimeUnit.MILLISECONDS);
}
Response resp = client.newCall(new Request.Builder().url(realUrl).build()).execute();
int response = resp.code();
String text = resp.body().string();
if (response >= 200 && response < 300) {
try {
JSONObject json = new JSONObject(text);
if (JWalk.getInt(json, "status", "code") != 0) {
throw KeybaseException.queryScrewup("Keybase.io query failed: " + path + "?" + query +
" using proxy: " + proxy);
}
return json;
} catch (JSONException e) {
throw KeybaseException.keybaseScrewup(e);
}
} else {
throw KeybaseException.networkScrewup("Keybase.io query error (status=" + response + "): " + text);
}
} catch (Exception e) {
throw KeybaseException.networkScrewup(e);
}
}
public static String snarf(InputStream in)
throws IOException {
byte[] buf = new byte[1024];
int count = 0;
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
while ((count = in.read(buf)) != -1)
out.write(buf, 0, count);
return out.toString();
}
static class MatchIterator implements Iterable<Match>, Iterator<Match> {
private final JSONArray mMatches;
private int mLastIndex;
private Match mNextMatch;
public MatchIterator(JSONArray matches) throws KeybaseException {
Log.d(TAG, "match count=" + matches.length());
mMatches = matches;
mLastIndex = -1;
mNextMatch = null;
hasNext();
}
// caches mNextMatch but not the index
private int findNext() {
try {
for (int index = mLastIndex + 1; index < mMatches.length(); index++) {
mNextMatch = new Match(mMatches.getJSONObject(index));
if (mNextMatch.hasKey()) {
return index;
}
}
} catch (JSONException e) {
throw new RuntimeException(KeybaseException.keybaseScrewup(e));
} catch (KeybaseException e) {
throw new RuntimeException(e);
}
return -1;
}
@Override
public boolean hasNext() {
return (findNext() != -1);
}
@Override
public Match next() {
mLastIndex = findNext();
if (mLastIndex == -1) {
throw new NoSuchElementException();
} else {
return mNextMatch;
}
}
@Override
public void remove() {
throw new RuntimeException("UserIterator.remove() not supported");
}
@Override
public Iterator<Match> iterator() {
return this;
}
}
}