Skip to content

Commit 3c61b0c

Browse files
committed
Merge pull request #19 from auth0/chore-improve-telemetry
Update telemetry classes
2 parents ecb6073 + 97c7053 commit 3c61b0c

5 files changed

Lines changed: 56 additions & 48 deletions

File tree

auth0/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
apply plugin: 'java'
22

3+
compileJava {
4+
sourceCompatibility "1.7"
5+
}
6+
37
dependencies {
48
compile 'com.squareup.okhttp:okhttp:2.5.0'
59
compile 'com.fasterxml.jackson.core:jackson-databind:2.4.1'

auth0/src/main/java/com/auth0/Auth0.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
package com.auth0;
2626

2727
import com.auth0.authentication.AuthenticationAPIClient;
28-
import com.auth0.util.BaseTelemetry;
2928
import com.auth0.util.Telemetry;
3029
import com.squareup.okhttp.HttpUrl;
3130

@@ -79,8 +78,7 @@ public Auth0(String clientId, String clientSecret, String domain, String configu
7978
this.clientSecret = clientSecret;
8079
this.domainUrl = ensureUrlString(domain);
8180
this.configurationUrl = resolveConfiguration(configurationDomain, this.domainUrl);
82-
this.telemetry = new BaseTelemetry();
83-
this.telemetry.usingLibrary(BuildConfig.NAME, BuildConfig.VERSION);
81+
this.telemetry = new Telemetry(BuildConfig.NAME, BuildConfig.VERSION);
8482
}
8583

8684
/**

auth0/src/main/java/com/auth0/util/BaseTelemetry.java

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
11
package com.auth0.util;
22

3-
public interface Telemetry {
4-
String NAME_KEY = "name";
5-
String VERSION_KEY = "version";
6-
String HEADER_NAME = "Auth0-Client";
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
75

8-
void usingLibrary(String name, String version);
9-
String getValue();
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
public class Telemetry {
10+
public static final String HEADER_NAME = "Auth0-Client";
11+
12+
private static final String NAME_KEY = "name";
13+
private static final String VERSION_KEY = "version";
14+
private static final String LIB_VERSION_KEY = "lib_version";
15+
16+
private final String name;
17+
private final String version;
18+
private final String libraryVersion;
19+
20+
public Telemetry(String name, String version) {
21+
this(name, version, null);
22+
}
23+
24+
public Telemetry(String name, String version, String libraryVersion) {
25+
this.name = name;
26+
this.version = version;
27+
this.libraryVersion = libraryVersion;
28+
}
29+
30+
public String getValue() {
31+
Map<String, String> values = new HashMap<>();
32+
if (name != null) {
33+
values.put(NAME_KEY, name);
34+
}
35+
if (version != null) {
36+
values.put(VERSION_KEY, version);
37+
}
38+
if (libraryVersion != null) {
39+
values.put(LIB_VERSION_KEY, libraryVersion);
40+
}
41+
if (values.isEmpty()) {
42+
return null;
43+
}
44+
try {
45+
String json = new ObjectMapper().writeValueAsString(values);
46+
return Base64.encodeUrlSafe(json);
47+
} catch (JsonProcessingException e) {
48+
return null;
49+
}
50+
}
1051
}

auth0/src/test/java/com/auth0/util/BaseTelemetryTest.java renamed to auth0/src/test/java/com/auth0/util/TelemetryTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@
88
import static org.hamcrest.Matchers.nullValue;
99
import static org.junit.Assert.assertThat;
1010

11-
public class BaseTelemetryTest {
11+
public class TelemetryTest {
1212

13-
private BaseTelemetry telemetry;
13+
private Telemetry telemetry;
1414

1515
@Before
1616
public void setUp() throws Exception {
17-
telemetry = new BaseTelemetry();
17+
telemetry = new Telemetry("auth0-java", "1.0.0");
1818
}
1919

2020
@Test
2121
public void shouldReturnBase64() throws Exception {
22-
telemetry.usingLibrary("auth0-java", "1.0.0");
2322
assertThat(telemetry.getValue(), is(notNullValue()));
2423
}
2524

2625
@Test
2726
public void shouldReturnNullWhenNoInfoIsProvided() throws Exception {
27+
telemetry = new Telemetry(null, null);
2828
assertThat(telemetry.getValue(), is(nullValue()));
2929
}
3030
}

0 commit comments

Comments
 (0)