Skip to content

Commit b0df202

Browse files
committed
Code cleanup and java 7 support
1 parent e5df4b2 commit b0df202

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ The Java SDK for interacting with the Databox Push API v 2.0.
1919
System.err.println(e.getLocalizedMessage());
2020
}
2121
```
22+
23+
### Sending multiple metrics at once
24+
25+
```java
26+
String TOKEN = "your_token_goes_here";
27+
Databox notification = new Databox(TOKEN);
28+
try {
29+
List<Databox.KPI> kpis = new ArrayList<Databox.KPI>();
30+
kpis.add(new Databox.KPI().setKey("my_first_key").setValue(1201.41));
31+
kpis.add(new Databox.KPI().setKey("my_second_key").setValue(8249));
32+
notification.push(kpis);
33+
} catch (Exception e) {
34+
logger.error(e.getLocalizedMessage(), e);
35+
}
36+
```
37+
2238
## License
2339

2440
`databox-sdk` is licensed under the Apache License, Version 2.0 - see the [LICENSE](http://www.apache.org/licenses/LICENSE-2.0) file for details

build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ repositories {
77
mavenCentral()
88
}
99

10+
allprojects {
11+
apply plugin: 'java'
12+
sourceCompatibility = 1.7
13+
targetCompatibility = 1.7
14+
}
15+
1016
dependencies {
1117
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.+'
1218
testCompile group: 'junit', name: 'junit', version: '4.+'

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
public class Databox {
2626
static final Logger logger = LoggerFactory.getLogger(Databox.class);
2727
private static final String DEFAULT_HOST = "https://push2new.databox.com";
28+
private static final String CLIENT_VERSION = "2.0";
2829

2930
private final String _token;
3031
private String _host;
@@ -81,7 +82,7 @@ private boolean push(String rawData) {
8182
conn = (HttpURLConnection) url.openConnection();
8283
conn.setRequestMethod("POST");
8384
conn.setRequestProperty("Content-Type", "application/json");
84-
conn.setRequestProperty("User-Agent", "java-sdk");
85+
conn.setRequestProperty("User-Agent", "Databox/" + CLIENT_VERSION);
8586

8687
String encodedBytes = base64Encode((_token + ":").getBytes("UTF-8"));
8788
String authorization = "Basic " + encodedBytes;
@@ -113,8 +114,8 @@ private boolean push(String rawData) {
113114
logger.info(input.toString() + ": " + rawData);
114115
return true;
115116
} catch (Exception e) {
116-
logger.error("Not send: " + rawData);
117117
logger.error(e.getLocalizedMessage(), e);
118+
throw new RuntimeException(e);
118119
} finally {
119120
if (conn != null) {
120121
conn.disconnect();
@@ -127,7 +128,6 @@ private boolean push(String rawData) {
127128
}
128129
}
129130
}
130-
return false;
131131
}
132132

133133
public void setHost(String host) {
@@ -183,7 +183,7 @@ public static class KPI {
183183
private String key;
184184
private Object value;
185185
private Date date;
186-
private Map<String, Object> attributes = new HashMap<String, Object>();
186+
private Map<String, Object> attributes = new HashMap<>();
187187

188188
static {
189189
SDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

src/test/java/com/databox/sdk/DataboxTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import java.io.IOException;
55
import java.text.SimpleDateFormat;
6+
import java.util.ArrayList;
7+
import java.util.List;
68
import java.util.Locale;
79
import java.util.Map;
810

@@ -19,9 +21,9 @@
1921
public class DataboxTest extends TestCase {
2022
private static final Logger logger = LoggerFactory.getLogger(DataboxTest.class);
2123

22-
private static final String TOKEN = "mj2dx0y7xk0g4wwos8o884cc0gs0woow";
24+
private static final String TOKEN = "my_api_key";
2325

24-
public void testTrigger() throws IOException {
26+
public void testPush() throws IOException {
2527
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
2628
Databox notification = new Databox(TOKEN);
2729
try {
@@ -31,4 +33,16 @@ public void testTrigger() throws IOException {
3133
}
3234
}
3335

36+
public void testPushArray() throws IOException {
37+
Databox notification = new Databox(TOKEN);
38+
try {
39+
List<Databox.KPI> kpis = new ArrayList<>();
40+
kpis.add(new Databox.KPI().setKey("my_first_key").setValue(1201.41));
41+
kpis.add(new Databox.KPI().setKey("my_second_key").setValue(8249));
42+
notification.push(kpis);
43+
} catch (Exception e) {
44+
logger.error(e.getLocalizedMessage(), e);
45+
}
46+
}
47+
3448
}

0 commit comments

Comments
 (0)