|
1 | 1 | package com.auth0.util; |
2 | 2 |
|
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; |
7 | 5 |
|
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 | + } |
10 | 51 | } |
0 commit comments