Skip to content

Commit b242e01

Browse files
author
ivolo
committed
adding requestId, requestTimestamp, and renaming to writeKey
1 parent 32e6db6 commit b242e01

5 files changed

Lines changed: 57 additions & 31 deletions

File tree

src/main/java/com/github/segmentio/Analytics.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ public class Analytics {
2727
*
2828
* This method is thread-safe.
2929
*
30-
* @param secret
31-
* Your segment.io secret. You can get one of these by
30+
* @param writeKey
31+
* Your segment.io writeKey. You can get one of these by
3232
* registering for a project at https://segment.io
3333
*
3434
*/
35-
public static synchronized void initialize(String secret) {
35+
public static synchronized void initialize(String writeKey) {
3636

3737
if (defaultClient == null)
38-
defaultClient = new AnalyticsClient(secret, new Options());
38+
defaultClient = new AnalyticsClient(writeKey, new Options());
3939
}
4040

4141
/**
@@ -51,19 +51,19 @@ public static synchronized void initialize(String secret) {
5151
*
5252
* This method is thread-safe.
5353
*
54-
* @param secret
55-
* Your segment.io secret. You can get one of these by
54+
* @param writeKey
55+
* Your segment.io writeKey. You can get one of these by
5656
* registering for a project at https://segment.io
5757
*
5858
* @param options
5959
* Options to configure the behavior of the Segment.io client
6060
*
6161
*
6262
*/
63-
public static synchronized void initialize(String secret, Options options) {
63+
public static synchronized void initialize(String writeKey, Options options) {
6464

6565
if (defaultClient == null)
66-
defaultClient = new AnalyticsClient(secret, options);
66+
defaultClient = new AnalyticsClient(writeKey, options);
6767
}
6868

6969
private static void checkInitialized() {

src/main/java/com/github/segmentio/AnalyticsClient.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333
public class AnalyticsClient {
3434

35-
private String secret;
35+
private String writeKey;
3636
private Options options;
3737

3838
private Flusher flusher;
@@ -51,14 +51,14 @@ public class AnalyticsClient {
5151
* your requests on a separate resource-constrained thread pool.
5252
*
5353
*
54-
* @param secret
55-
* Your segment.io secret. You can get one of these by
54+
* @param writeKey
55+
* Your segment.io writeKey. You can get one of these by
5656
* registering for a project at https://segment.io
5757
*
5858
*/
59-
public AnalyticsClient(String secret) {
59+
public AnalyticsClient(String writeKey) {
6060

61-
this(secret, new Options());
61+
this(writeKey, new Options());
6262
}
6363

6464
/**
@@ -73,26 +73,26 @@ public AnalyticsClient(String secret) {
7373
* your requests on a separate resource-constrained thread pool.
7474
*
7575
*
76-
* @param secret
77-
* Your segment.io secret. You can get one of these by
76+
* @param writeKey
77+
* Your segment.io writeKey. You can get one of these by
7878
* registering for a project at https://segment.io
7979
*
8080
* @param options
8181
* Options to configure the behavior of the Segment.io client
8282
*
8383
*
8484
*/
85-
public AnalyticsClient(String secret, Options options) {
85+
public AnalyticsClient(String writeKey, Options options) {
8686

8787
String errorPrefix = "analytics-java client must be initialized with a valid ";
8888

89-
if (StringUtils.isEmpty(secret))
90-
throw new IllegalArgumentException(errorPrefix + "secret.");
89+
if (StringUtils.isEmpty(writeKey))
90+
throw new IllegalArgumentException(errorPrefix + "writeKey.");
9191

9292
if (options == null)
9393
throw new IllegalArgumentException(errorPrefix + "options.");
9494

95-
this.secret = secret;
95+
this.writeKey = writeKey;
9696
this.options = options;
9797
this.statistics = new AnalyticsStatistics();
9898
this.requester = new BlockingRequester(this);
@@ -104,7 +104,7 @@ public AnalyticsClient(String secret, Options options) {
104104
private IBatchFactory factory = new IBatchFactory() {
105105

106106
public Batch create(List<BasePayload> batch) {
107-
return new Batch(secret, batch);
107+
return new Batch(writeKey, batch);
108108
}
109109
};
110110

@@ -541,12 +541,12 @@ public void close() {
541541
// Getters and Setters
542542
//
543543

544-
public String getSecret() {
545-
return secret;
544+
public String getWriteKey() {
545+
return writeKey;
546546
}
547547

548-
public void setSecret(String secret) {
549-
this.secret = secret;
548+
public void setWriteKey(String writeKey) {
549+
this.writeKey = writeKey;
550550
}
551551

552552
public Options getOptions() {

src/main/java/com/github/segmentio/models/BasePayload.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.segmentio.models;
22

3+
import java.util.UUID;
4+
35
import org.joda.time.DateTime;
46

57
/**
@@ -11,6 +13,7 @@ public class BasePayload {
1113
private String userId;
1214
private Context context;
1315
private DateTime timestamp;
16+
private String requestId;
1417

1518
private transient Callback callback;
1619

@@ -22,6 +25,7 @@ public BasePayload(String userId,
2225
this.userId = userId;
2326
this.timestamp = timestamp;
2427
this.context = context;
28+
this.requestId = UUID.randomUUID().toString();
2529

2630
this.callback = callback;
2731
}
@@ -57,5 +61,13 @@ public Callback getCallback() {
5761
public void setCallback(Callback callback) {
5862
this.callback = callback;
5963
}
64+
65+
public String getRequestId() {
66+
return requestId;
67+
}
68+
69+
public void setRequestId(String requestId) {
70+
this.requestId = requestId;
71+
}
6072

6173
}

src/main/java/com/github/segmentio/models/Batch.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@
22

33
import java.util.List;
44

5+
import org.joda.time.DateTime;
6+
57
public class Batch {
68

7-
private String secret;
9+
private String writeKey;
810
private List<BasePayload> batch;
11+
private DateTime requestTimestamp;
912

10-
public Batch(String secret, List<BasePayload> batch) {
11-
this.secret = secret;
13+
public Batch(String writeKey, List<BasePayload> batch) {
14+
this.writeKey = writeKey;
1215
this.batch = batch;
1316
}
1417

15-
public String getSecret() {
16-
return secret;
18+
public String getWriteKey() {
19+
return writeKey;
1720
}
1821

19-
public void setSecret(String secret) {
20-
this.secret = secret;
22+
public void setWriteKey(String writeKey) {
23+
this.writeKey = writeKey;
2124
}
2225

2326
public List<BasePayload> getBatch() {
@@ -28,4 +31,12 @@ public void setBatch(List<BasePayload> batch) {
2831
this.batch = batch;
2932
}
3033

34+
public DateTime getRequestTimestamp() {
35+
return requestTimestamp;
36+
}
37+
38+
public void setRequestTimestamp(DateTime requestTimestamp) {
39+
this.requestTimestamp = requestTimestamp;
40+
}
41+
3142
}

src/main/java/com/github/segmentio/request/BlockingRequester.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.apache.http.entity.ByteArrayEntity;
1111
import org.apache.http.impl.client.CloseableHttpClient;
1212
import org.apache.http.impl.client.HttpClients;
13+
import org.joda.time.DateTime;
1314
import org.slf4j.Logger;
1415
import org.slf4j.LoggerFactory;
1516

@@ -52,6 +53,8 @@ public void send(Batch batch) {
5253
try {
5354
long start = System.currentTimeMillis();
5455

56+
batch.setRequestTimestamp(DateTime.now());
57+
5558
String json = gson.toJson(batch);
5659

5760
HttpResponse response = executeRequest(json);

0 commit comments

Comments
 (0)