Skip to content

Commit 2c9646c

Browse files
zhu-xiaoweixiaoweii
andauthored
feat: add auth api. (#9)
* feat: add auth api. * ci: delete unsupported event for commit title lint. --------- Co-authored-by: xiaoweii <xiaoweii@amazom.com>
1 parent 90225a8 commit 2c9646c

File tree

4 files changed

+111
-37
lines changed

4 files changed

+111
-37
lines changed

.github/workflows/lint.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
name: Commit Title Lint
22

33
on:
4-
push:
5-
branches: [ "main" ]
64
pull_request:
75
branches: [ "main" ]
86

clickstream/src/main/java/software/aws/solution/clickstream/client/ClickstreamConfiguration.java

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class ClickstreamConfiguration {
3434
private boolean isTrackAppLifecycleEvents;
3535
private boolean isTrackAppExceptionEvents;
3636
private boolean isLogEvents;
37+
private String authCookie;
3738

3839
/**
3940
* Create an {@link ClickstreamConfiguration} object with the specified parameters.
@@ -90,15 +91,6 @@ public String getEndpoint() {
9091
return this.endpoint;
9192
}
9293

93-
/**
94-
* For get the Clickstream Okhttp3 dns.
95-
*
96-
* @return the dns.
97-
*/
98-
public Dns getDns() {
99-
return this.dns;
100-
}
101-
10294
/**
10395
* The endpoint for Clickstream.
10496
*
@@ -110,6 +102,15 @@ public ClickstreamConfiguration withEndpoint(final String endpoint) {
110102
return this;
111103
}
112104

105+
/**
106+
* For get the Clickstream Okhttp3 dns.
107+
*
108+
* @return the dns.
109+
*/
110+
public Dns getDns() {
111+
return this.dns;
112+
}
113+
113114
/**
114115
* The Custom Okhttp3 dns for Clickstream.
115116
*
@@ -130,15 +131,6 @@ public long getSendEventsInterval() {
130131
return this.sendEventsInterval;
131132
}
132133

133-
/**
134-
* The time out of entire http call.
135-
*
136-
* @return callTimeOut.
137-
*/
138-
public Long getCallTimeOut() {
139-
return this.callTimeOut;
140-
}
141-
142134
/**
143135
* The interval of events sent at once.
144136
*
@@ -150,6 +142,15 @@ public ClickstreamConfiguration withSendEventsInterval(final long sendEventsInte
150142
return this;
151143
}
152144

145+
/**
146+
* The time out of entire http call.
147+
*
148+
* @return callTimeOut.
149+
*/
150+
public Long getCallTimeOut() {
151+
return this.callTimeOut;
152+
}
153+
153154
/**
154155
* The http call time out.
155156
*
@@ -190,6 +191,17 @@ public boolean isTrackAppLifecycleEvents() {
190191
return this.isTrackAppLifecycleEvents;
191192
}
192193

194+
/**
195+
* Is track app lifecycle events.
196+
*
197+
* @param isTrackAppLifecycleEvents Is track app lifecycle events.
198+
* @return the current ClickstreamConfiguration instance.
199+
*/
200+
public ClickstreamConfiguration withTrackAppLifecycleEvents(final boolean isTrackAppLifecycleEvents) {
201+
this.isTrackAppLifecycleEvents = isTrackAppLifecycleEvents;
202+
return this;
203+
}
204+
193205
/**
194206
* Is track app exception events.
195207
*
@@ -199,6 +211,17 @@ public boolean isTrackAppExceptionEvents() {
199211
return this.isTrackAppExceptionEvents;
200212
}
201213

214+
/**
215+
* Is track app exception events.
216+
*
217+
* @param isTrackAppExceptionEvents Is track app exception events.
218+
* @return the current ClickstreamConfiguration instance.
219+
*/
220+
public ClickstreamConfiguration withTrackAppExceptionEvents(final boolean isTrackAppExceptionEvents) {
221+
this.isTrackAppExceptionEvents = isTrackAppExceptionEvents;
222+
return this;
223+
}
224+
202225
/**
203226
* Is log events.
204227
*
@@ -209,35 +232,33 @@ public boolean isLogEvents() {
209232
}
210233

211234
/**
212-
* Is track app lifecycle events.
235+
* Is log events json when record event, set true for debug mode.
213236
*
214-
* @param isTrackAppLifecycleEvents Is track app lifecycle events.
237+
* @param isLogEvents Is log events json.
215238
* @return the current ClickstreamConfiguration instance.
216239
*/
217-
public ClickstreamConfiguration withTrackAppLifecycleEvents(final boolean isTrackAppLifecycleEvents) {
218-
this.isTrackAppLifecycleEvents = isTrackAppLifecycleEvents;
240+
public ClickstreamConfiguration withLogEvents(final boolean isLogEvents) {
241+
this.isLogEvents = isLogEvents;
219242
return this;
220243
}
221244

222245
/**
223-
* Is track app exception events.
246+
* Get The Clickstream authCookie.
224247
*
225-
* @param isTrackAppExceptionEvents Is track app exception events.
226-
* @return the current ClickstreamConfiguration instance.
248+
* @return the authCookie.
227249
*/
228-
public ClickstreamConfiguration withTrackAppExceptionEvents(final boolean isTrackAppExceptionEvents) {
229-
this.isTrackAppExceptionEvents = isTrackAppExceptionEvents;
230-
return this;
250+
public String getAuthCookie() {
251+
return this.authCookie;
231252
}
232253

233254
/**
234-
* Is log events json when record event, set true for debug mode.
255+
* Set the auth cookie for Clickstream.
235256
*
236-
* @param isLogEvents Is log events json.
257+
* @param authCookie The authCookie.
237258
* @return the current ClickstreamConfiguration instance.
238259
*/
239-
public ClickstreamConfiguration withLogEvents(final boolean isLogEvents) {
240-
this.isLogEvents = isLogEvents;
260+
public ClickstreamConfiguration withAuthCookie(final String authCookie) {
261+
this.authCookie = authCookie;
241262
return this;
242263
}
243264
}

clickstream/src/main/java/software/aws/solution/clickstream/client/network/NetRequest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ public static boolean uploadEvents(String eventJson, ClickstreamConfiguration co
6969
LOG.debug("submitEvents success. \n" + response);
7070
return true;
7171
} else {
72-
LOG.warn(
73-
String.format(Locale.US, "submitEvents onResponse fail %d-%s", response.code(), response.body()));
72+
LOG.error("submitEvents fail. \n" + response);
7473
}
7574
} catch (final Exception exception) {
7675
LOG.error("submitEvents error: " + exception.getMessage());
@@ -109,7 +108,11 @@ private static Response request(@NonNull String eventJson, @NonNull ClickstreamC
109108
.addQueryParameter("appId", appId)
110109
.addQueryParameter("compression", compression)
111110
.build();
112-
request = request.newBuilder().url(url).post(body).build();
111+
Request.Builder builder = request.newBuilder().url(url).post(body);
112+
if (!StringUtil.isNullOrEmpty(configuration.getAuthCookie())) {
113+
builder.addHeader("cookie", configuration.getAuthCookie());
114+
}
115+
request = builder.build();
113116

114117
final OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
115118
okHttpClientBuilder.connectTimeout(HTTP_CONNECT_TIME_OUT, TimeUnit.SECONDS);

clickstream/src/test/java/software/aws/solution/clickstream/IntegrationTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@
4646

4747
import java.util.Map;
4848

49+
import static com.github.dreamhead.moco.Moco.and;
4950
import static com.github.dreamhead.moco.Moco.by;
51+
import static com.github.dreamhead.moco.Moco.eq;
52+
import static com.github.dreamhead.moco.Moco.header;
5053
import static com.github.dreamhead.moco.Moco.httpServer;
5154
import static com.github.dreamhead.moco.Moco.status;
5255
import static com.github.dreamhead.moco.Moco.text;
@@ -63,6 +66,7 @@
6366
public class IntegrationTest {
6467
private static final String COLLECT_SUCCESS = "/collect/success";
6568
private static final String COLLECT_SUCCESS1 = "/collect/success1";
69+
private static final String COLLECT_FOR_AUTH = "/collect/auth";
6670
private static final String COLLECT_FAIL = "/collect/fail";
6771
private static final String COLLECT_HOST = "http://localhost:8082";
6872
private static Runner runner;
@@ -81,6 +85,8 @@ public static void beforeClass() {
8185
final HttpServer server = httpServer(8082);
8286
server.request(by(uri(COLLECT_SUCCESS))).response(status(200), text("success"));
8387
server.request(by(uri(COLLECT_SUCCESS1))).response(status(200), text("success"));
88+
server.request(and(by(uri(COLLECT_FOR_AUTH)), eq(header("cookie"), "testCookie")))
89+
.response(status(200), text("success"));
8490
server.request(by(uri(COLLECT_FAIL))).response(status(403), text("fail"));
8591
runner = runner(server);
8692
runner.start();
@@ -582,6 +588,52 @@ public void testCustomDnsForUnKnowHostFail() throws Exception {
582588
assertEquals(0, dbUtil.getTotalNumber());
583589
}
584590

591+
/**
592+
* test set auth cookie success.
593+
*
594+
* @throws Exception exception
595+
*/
596+
@Test
597+
public void testSetAuthCookieSuccess() throws Exception {
598+
String authCookie = "testCookie";
599+
ClickstreamAnalytics.getClickStreamConfiguration()
600+
.withAuthCookie(authCookie)
601+
.withEndpoint(assembleEndpointUrl(COLLECT_FOR_AUTH));
602+
603+
assertEquals(authCookie, this.analyticsClient.getClickstreamConfiguration().getAuthCookie());
604+
605+
ClickstreamAnalytics.recordEvent("testRecordEventForAuth");
606+
assertEquals(1, dbUtil.getTotalNumber());
607+
ClickstreamAnalytics.flushEvents();
608+
Thread.sleep(1000);
609+
assertEquals(0, dbUtil.getTotalNumber());
610+
}
611+
612+
/**
613+
* test set auth cookie fail.
614+
*
615+
* @throws Exception exception
616+
*/
617+
@Test
618+
public void testSetAuthCookieFail() throws Exception {
619+
String authCookie = "testCookieFail";
620+
ClickstreamAnalytics.getClickStreamConfiguration()
621+
.withAuthCookie(authCookie)
622+
.withEndpoint(assembleEndpointUrl(COLLECT_FOR_AUTH));
623+
624+
assertEquals(authCookie, this.analyticsClient.getClickstreamConfiguration().getAuthCookie());
625+
626+
ClickstreamAnalytics.recordEvent("testRecordEventFailForAuth");
627+
assertEquals(1, dbUtil.getTotalNumber());
628+
ClickstreamAnalytics.flushEvents();
629+
Thread.sleep(1000);
630+
assertEquals(1, dbUtil.getTotalNumber());
631+
632+
ClickstreamAnalytics.getClickStreamConfiguration().withAuthCookie("testCookie");
633+
ClickstreamAnalytics.flushEvents();
634+
Thread.sleep(1000);
635+
assertEquals(0, dbUtil.getTotalNumber());
636+
}
585637

586638
/**
587639
* test enable.

0 commit comments

Comments
 (0)