Skip to content

Commit 6149311

Browse files
committed
Improvements to app identity for metrics
1 parent 763c963 commit 6149311

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

src/main/java/com/stackify/api/common/AppIdentityService.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public class AppIdentityService {
6161
* Jackson object mapper
6262
*/
6363
private final ObjectMapper objectMapper;
64+
65+
/**
66+
* True if deviceAppId is required in the response
67+
*/
68+
private final boolean deviceAppIdRequired;
6469

6570
/**
6671
* Constructor
@@ -73,6 +78,22 @@ public AppIdentityService(final ApiConfiguration apiConfig, final ObjectMapper o
7378

7479
this.defaultApiConfig = apiConfig;
7580
this.objectMapper = objectMapper;
81+
this.deviceAppIdRequired = false;
82+
}
83+
84+
/**
85+
* Constructor
86+
* @param apiConfig The API configuration
87+
* @param objectMapper Jackson object mapper
88+
* @param deviceAppIdRequired True if deviceAppId is required in the response
89+
*/
90+
public AppIdentityService(final ApiConfiguration apiConfig, final ObjectMapper objectMapper, final boolean deviceAppIdRequired) {
91+
Preconditions.checkNotNull(apiConfig);
92+
Preconditions.checkNotNull(objectMapper);
93+
94+
this.defaultApiConfig = apiConfig;
95+
this.objectMapper = objectMapper;
96+
this.deviceAppIdRequired = deviceAppIdRequired;
7697
}
7798

7899
/**
@@ -154,13 +175,34 @@ private AppIdentity identifyApp(ApiConfiguration apiConfig) throws IOException,
154175
// convert to json bytes
155176
byte[] jsonBytes = objectMapper.writer().writeValueAsBytes(apiConfig.getEnvDetail());
156177

178+
if (LOGGER.isDebugEnabled())
179+
{
180+
LOGGER.debug("IdentifyApp Request: {}", new String(jsonBytes, "UTF-8"));
181+
}
182+
157183
// post to stackify
158184
final HttpClient httpClient = new HttpClient(apiConfig);
159185
final String responseString = httpClient.post("/Metrics/IdentifyApp", jsonBytes);
160186

187+
LOGGER.debug("IdentifyApp Response: {}", responseString);
188+
161189
// deserialize the response and return the app identity
162190
ObjectReader jsonReader = objectMapper.reader(new TypeReference<AppIdentity>(){});
163-
return jsonReader.readValue(responseString);
191+
AppIdentity identity = jsonReader.readValue(responseString);
192+
193+
// make sure it has a valid DeviceAppID before accepting it
194+
195+
if (deviceAppIdRequired)
196+
{
197+
if (identity.getDeviceAppId() == null)
198+
{
199+
throw new NullPointerException("DeviceAppId is null");
200+
}
201+
}
202+
203+
// done
204+
205+
return identity;
164206
}
165207

166208

src/test/java/com/stackify/api/common/AppIdentityServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void testGetAppIdentity() throws Exception {
4747
EnvironmentDetail.newBuilder().deviceName("device").appName("app").build();
4848

4949
final AppIdentity appIdentity =
50-
AppIdentity.newBuilder().deviceId(123).appNameId("456").appName("app").build();
50+
AppIdentity.newBuilder().deviceId(123).deviceAppId(789).appNameId("456").appName("app").build();
5151

5252
final ObjectMapper objectMapper = new ObjectMapper();
5353

0 commit comments

Comments
 (0)