|
19 | 19 |
|
20 | 20 | public class Databox { |
21 | 21 | static final Logger logger = LoggerFactory.getLogger(Databox.class); |
22 | | - private static final String DEFAULT_HOST = "https://push2new.databox.com"; |
23 | | - private static final String CLIENT_VERSION = "2.2"; |
| 22 | + private static final String DEFAULT_HOST = "https://push.databox.com"; |
| 23 | + private static final String CLIENT_VERSION = "2.3.0"; |
24 | 24 |
|
25 | 25 | private final String _token; |
26 | 26 | private String _host; |
@@ -78,8 +78,9 @@ private boolean push(String rawData) { |
78 | 78 | conn = (HttpURLConnection) url.openConnection(); |
79 | 79 | conn.setRequestMethod("POST"); |
80 | 80 | conn.setRequestProperty("Content-Type", "application/json"); |
81 | | - conn.setRequestProperty("User-Agent", "Databox/" + CLIENT_VERSION + " (Java)"); |
82 | | - String encodedToken = base64Encode((_token + ": ").getBytes("UTF-8")); |
| 81 | + conn.setRequestProperty("User-Agent", "databox-java/" + CLIENT_VERSION); |
| 82 | + conn.setRequestProperty("Accept", "application/vnd.databox.v" + CLIENT_VERSION.split("\\.")[0] + "+json"); |
| 83 | + String encodedToken = base64Encode((_token + ":").getBytes("UTF-8")); |
83 | 84 | conn.setRequestProperty("Authorization", "Basic " + encodedToken); |
84 | 85 | conn.setDoOutput(true); |
85 | 86 | conn.setDoInput(true); |
@@ -125,10 +126,11 @@ private boolean push(String rawData) { |
125 | 126 |
|
126 | 127 | private HttpURLConnection buildConnection(String method, String path) throws IOException { |
127 | 128 | HttpURLConnection connection = (HttpURLConnection) (new URL(_host + path)).openConnection(); |
128 | | - String encodedToken = base64Encode((_token + ": ").getBytes("UTF-8")); |
| 129 | + String encodedToken = base64Encode((_token + ":").getBytes("UTF-8")); |
129 | 130 | connection.setRequestProperty("Authorization", "Basic " + encodedToken); |
130 | 131 | connection.setRequestProperty("Content-Type", "application/json"); |
131 | | - connection.setRequestProperty("User-Agent", "Databox/" + CLIENT_VERSION + " (Java)"); |
| 132 | + connection.setRequestProperty("User-Agent", "databox-java/" + CLIENT_VERSION); |
| 133 | + connection.setRequestProperty("Accept", "application/vnd.databox.v" + CLIENT_VERSION.split("\\.")[0] + "+json"); |
132 | 134 | connection.setRequestMethod(method); |
133 | 135 | connection.setDoOutput(true); |
134 | 136 | connection.setDoInput(true); |
@@ -184,14 +186,37 @@ private StringBuffer post(String path, String rawData) throws IOException { |
184 | 186 | return responseAsString; |
185 | 187 | } |
186 | 188 |
|
| 189 | + private StringBuffer delete(String path) throws IOException { |
| 190 | + HttpURLConnection connection = buildConnection("DELETE", path); |
| 191 | + connection.connect(); |
| 192 | + |
| 193 | + int responseCode = connection.getResponseCode(); |
| 194 | + |
| 195 | + StringBuffer responseAsString = handleResponseInputStream( |
| 196 | + (responseCode >= 200 && responseCode < 300) ? connection.getInputStream() : connection.getErrorStream()); |
| 197 | + |
| 198 | + lastResponseCode = responseCode; |
| 199 | + |
| 200 | + connection.disconnect(); |
| 201 | + return responseAsString; |
| 202 | + } |
| 203 | + |
187 | 204 | public StringBuffer lastPushes(int n) throws IOException { |
188 | | - return get("/lastpushes/" + (n + "")); |
| 205 | + return get("/lastpushes?limit=" + (n + "")); |
189 | 206 | } |
190 | 207 |
|
191 | 208 | public StringBuffer lastPush() throws IOException { |
192 | 209 | return lastPushes(1); |
193 | 210 | } |
194 | 211 |
|
| 212 | + public StringBuffer metrics() throws IOException { |
| 213 | + return get("/metrickeys"); |
| 214 | + } |
| 215 | + |
| 216 | + public StringBuffer purge() throws IOException { |
| 217 | + return delete("/data"); |
| 218 | + } |
| 219 | + |
195 | 220 | private String base64Encode(byte[] input) { |
196 | 221 | return DatatypeConverter.printBase64Binary(input); |
197 | 222 | } |
|
0 commit comments