Skip to content

Commit eacfe6f

Browse files
authored
Merge pull request #4 from databox/pushapi-v3
Support Databox API version 3
2 parents 1f249ba + a6d62f2 commit eacfe6f

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Change Log
2+
Following guidelines of http://keepachangelog.com/
3+
4+
## [Unreleased]
5+
- update to support Databox API version 3
6+
7+
## [2.2] - Apr 15, 2016
8+
- Support for `GET /lastpushes`
9+
- Usage of `.editorconfig`
10+
- Usage of [DatatypeConverter](https://docs.oracle.com/javase/7/docs/api/javax/xml/bind/DatatypeConverter.html) for `base64` encoding
11+
- `KPI` is now separate class
12+
- Usage of [findbugs](http://findbugs.sourceforge.net/)
13+
- TravisCI now tests SDK on Java 1.7 and Java 1.8
14+
- Small update to test suite
15+
- Example for `lastpushes` was added.
16+
17+
## [2.0] - Jan 8, 2016
18+
- Test case and sample updated to match 2.0 Push API
19+
20+
[Unreleased]: https://github.com/databox/databox-java/compare/2.2...master
21+
[2.2]: https://github.com/databox/databox-java/compare/2.0...2.2
22+
[2.0]: https://github.com/databox/databox-java/tree/2.0

src/main/java/com/databox/sdk/Databox.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
public class Databox {
2121
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";
2424

2525
private final String _token;
2626
private String _host;
@@ -78,8 +78,9 @@ private boolean push(String rawData) {
7878
conn = (HttpURLConnection) url.openConnection();
7979
conn.setRequestMethod("POST");
8080
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"));
8384
conn.setRequestProperty("Authorization", "Basic " + encodedToken);
8485
conn.setDoOutput(true);
8586
conn.setDoInput(true);
@@ -125,10 +126,11 @@ private boolean push(String rawData) {
125126

126127
private HttpURLConnection buildConnection(String method, String path) throws IOException {
127128
HttpURLConnection connection = (HttpURLConnection) (new URL(_host + path)).openConnection();
128-
String encodedToken = base64Encode((_token + ": ").getBytes("UTF-8"));
129+
String encodedToken = base64Encode((_token + ":").getBytes("UTF-8"));
129130
connection.setRequestProperty("Authorization", "Basic " + encodedToken);
130131
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");
132134
connection.setRequestMethod(method);
133135
connection.setDoOutput(true);
134136
connection.setDoInput(true);
@@ -184,14 +186,37 @@ private StringBuffer post(String path, String rawData) throws IOException {
184186
return responseAsString;
185187
}
186188

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+
187204
public StringBuffer lastPushes(int n) throws IOException {
188-
return get("/lastpushes/" + (n + ""));
205+
return get("/lastpushes?limit=" + (n + ""));
189206
}
190207

191208
public StringBuffer lastPush() throws IOException {
192209
return lastPushes(1);
193210
}
194211

212+
public StringBuffer metrics() throws IOException {
213+
return get("/metrickeys");
214+
}
215+
216+
public StringBuffer purge() throws IOException {
217+
return delete("/data");
218+
}
219+
195220
private String base64Encode(byte[] input) {
196221
return DatatypeConverter.printBase64Binary(input);
197222
}

src/main/java/com/databox/sdk/KPI.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class KPI {
1313
private String key;
1414
private Object value;
1515
private Date date;
16+
private String unit;
1617
private Map<String, Object> attributes = new HashMap<>();
1718

1819
static {
@@ -50,6 +51,15 @@ public KPI setDate(Date date) {
5051
return this;
5152
}
5253

54+
public String getUnit() {
55+
return unit;
56+
}
57+
58+
public KPI setUnit(String unit) {
59+
this.unit = unit;
60+
return this;
61+
}
62+
5363
public KPI addAttribute(String key, Object value) {
5464
attributes.put(key, value);
5565
return this;
@@ -82,6 +92,9 @@ public String toString() {
8292
if (date != null) {
8393
json += ", \"date\": \"" + SDF.format(date) + "\"";
8494
}
95+
if (unit != null) {
96+
json += ", \"unit\": \"" + unit + "\"";
97+
}
8598
for (Map.Entry<String, Object> attribute : attributes.entrySet()) {
8699
json += ", \"" + attribute.getKey() + "\": \"" + attribute.getValue() + "\"";
87100
}

0 commit comments

Comments
 (0)