Skip to content

Commit 0e64018

Browse files
authored
Merge pull request #1 from actionml/feature/get-delete-user-data
Feature/get delete user data
2 parents 4b59791 + 127d050 commit 0e64018

File tree

4 files changed

+148
-12
lines changed

4 files changed

+148
-12
lines changed

example/src/main/java/EventsClientExample.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
import java.util.List;
3030
import java.util.Map;
3131
import java.util.Optional;
32-
import java.util.concurrent.*;
33-
import java.util.function.Function;
32+
import java.util.concurrent.CompletableFuture;
33+
import java.util.concurrent.ExecutionException;
3434
import java.util.stream.Collectors;
3535

3636
/**
@@ -43,9 +43,25 @@ public class EventsClientExample {
4343

4444
public static void main(String[] args) {
4545

46-
String serverHost = args[0]; // this should be https://... for TLS/SSL when Harness has TLS/SSL enabled
47-
String engineId = args[1];
48-
String fileName = args[2];
46+
String serverHost = null;
47+
try {
48+
serverHost = args[0]; // this should be https://... for TLS/SSL when Harness has TLS/SSL enabled
49+
} catch (Exception e) {
50+
serverHost = "http://localhost";
51+
}
52+
String engineId = null;
53+
try {
54+
engineId = args[1];
55+
} catch (Exception e) {
56+
e.printStackTrace();
57+
System.exit(1);
58+
}
59+
String fileName = null;
60+
try {
61+
fileName = args[2];
62+
} catch (Exception e) {
63+
e.printStackTrace();
64+
}
4965
Integer serverPort = 9090;
5066

5167
log.info("Args: {}, {}, {}, {}", engineId, fileName, serverHost, serverPort);
@@ -60,16 +76,21 @@ public static void main(String[] args) {
6076
Optional<Path> cert = Optional.empty();
6177
EventsClient client = new EventsClient(engineId, serverHost, serverPort, optionalCreds, cert);
6278

79+
runCreateEvent(client, engineId, fileName);
80+
}
81+
82+
83+
private static void runCreateEvent(EventsClient client, String engineId, String fileName) {
6384
// example of JSON for creating an event
6485
String json = "{" +
6586
"\"eventId\":\"ed15537661f2492cab64615096c93160\"," +
6687
"\"event\":\"$set\"," +
6788
"\"entityType\":\"testGroup\"," +
6889
"\"entityId\":\"9\"," +
6990
"\"properties\":{" +
70-
"\"testPeriodStart\":\"2016-07-12T00:00:00.000+09:00\"," +
71-
"\"pageVariants\":[\"17\",\"18\"]," +
72-
"\"testPeriodEnd\":\"2016-08-31T00:00:00.000+09:00\"}," +
91+
"\"testPeriodStart\":\"2016-07-12T00:00:00.000+09:00\"," +
92+
"\"pageVariants\":[\"17\",\"18\"]," +
93+
"\"testPeriodEnd\":\"2016-08-31T00:00:00.000+09:00\"}," +
7394
"\"eventTime\":\"2016-07-12T16:08:49.677+09:00\"," +
7495
"\"creationTime\":\"2016-07-12T07:09:58.273Z\"}";
7596

@@ -163,7 +184,5 @@ public static void main(String[] args) {
163184
log.error("Oops, we have an unrecoverable error: ", e);
164185
client.close();
165186
}
166-
167187
}
168-
169188
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright ActionML, LLC under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* ActionML licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import com.actionml.HarnessClient;
19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
22+
import java.net.PasswordAuthentication;
23+
import java.util.Map;
24+
import java.util.Optional;
25+
26+
public class HarnessClientExample {
27+
private static Logger log = LoggerFactory.getLogger(HarnessClientExample.class);
28+
29+
public static void main(String[] args) {
30+
31+
String serverHost = null;
32+
try {
33+
serverHost = args[0]; // this should be https://... for TLS/SSL when Harness has TLS/SSL enabled
34+
} catch (Exception e) {
35+
serverHost = "http://localhost";
36+
}
37+
String engineId = null;
38+
try {
39+
engineId = args[1];
40+
} catch (Exception e) {
41+
e.printStackTrace();
42+
System.exit(1);
43+
}
44+
String fileName = null;
45+
try {
46+
fileName = args[2];
47+
} catch (Exception e) {
48+
e.printStackTrace();
49+
}
50+
Integer serverPort = 9090;
51+
52+
log.info("Args: {}, {}, {}, {}", engineId, fileName, serverHost, serverPort);
53+
54+
Map<String, String> env = System.getenv();
55+
Optional<String> optUsername = Optional.ofNullable(env.getOrDefault("HARNESS_CLIENT_USER_ID", null));
56+
Optional<String> optPassword = Optional.ofNullable(env.getOrDefault("HARNESS_CLIENT_USER_SECRET", null));
57+
Optional<PasswordAuthentication> optionalCreds = optUsername.flatMap(username -> optPassword.map(password ->
58+
new PasswordAuthentication(username, password.toCharArray())
59+
));
60+
61+
HarnessClient client = new HarnessClient(engineId, serverHost, serverPort, optionalCreds);
62+
63+
runGetUserData(client, "u1");
64+
// runDeleteUserData(client, "u1");
65+
}
66+
67+
68+
private static void runGetUserData(HarnessClient client, String userId) {
69+
client.deleteEvents(userId).thenAccept(System.out::println);
70+
}
71+
72+
private static void runDeleteUserData(HarnessClient client, String userId) {
73+
client.deleteEvents(userId).thenAccept(System.out::println);
74+
}
75+
}

src/main/java/com/actionml/EventsClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.actionml.entity.Event;
2727

2828
import java.net.PasswordAuthentication;
29+
import java.net.URI;
2930
import java.nio.file.Path;
3031
import java.util.ArrayList;
3132
import java.util.List;

src/main/java/com/actionml/HarnessClient.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,55 @@
1717

1818
package com.actionml;
1919

20+
import akka.http.javadsl.model.Uri;
21+
import akka.japi.Pair;
22+
import akka.stream.javadsl.Source;
23+
24+
import java.net.PasswordAuthentication;
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
import java.util.Optional;
28+
import java.util.concurrent.CompletionStage;
29+
2030
/**
2131
* @author The ActionML Team (<a href="http://actionml.com">http://actionml.com</a>)
2232
* 26.02.17 18:02
2333
*/
24-
public class HarnessClient {
34+
public class HarnessClient extends RestClient {
35+
36+
public HarnessClient(String engineId, String host, Integer port, Optional<PasswordAuthentication> optionalCreds) {
37+
super(host, port, Uri.create("/engines").addPathSegment(engineId).addPathSegment("entities"), optionalCreds, Optional.empty());
38+
}
2539

26-
public HarnessClient(String host, Integer port, String clientId, String clientSecret) {
40+
public CompletionStage<Pair<Integer, List<String>>> getEvents(String userId) {
41+
return withAuth().thenCompose(optionalToken ->
42+
Source.single(this.uri.addPathSegment(userId))
43+
.map(this::createGet)
44+
.zipWithIndex()
45+
.map(pair -> pair.copy(pair.first(), (Long) pair.second()))
46+
.via(this.poolClientFlow)
47+
.mapAsync(1, this::extractResponse)
48+
.mapAsync(1, this::extractResponses)
49+
.runFold(Pair.create(0, new ArrayList<>()), (acc, pair) -> {
50+
Integer code = pair.second().first();
51+
List<String> events = acc.second();
52+
events.add(pair.second().second());
53+
return Pair.create(code, events);
54+
}, this.materializer)
55+
);
56+
}
2757

58+
public CompletionStage<Integer> deleteEvents(String userId) {
59+
return withAuth().thenCompose(optionalToken ->
60+
Source.single(this.uri.addPathSegment(userId))
61+
.map(this::createDelete)
62+
.zipWithIndex()
63+
.map(pair -> pair.copy(pair.first(), (Long) pair.second()))
64+
.via(this.poolClientFlow)
65+
.mapAsync(1, this::extractResponse)
66+
.runFold(0, (a, i) -> i.second().status().intValue(), this.materializer)
67+
);
2868
}
2969

70+
3071
}

0 commit comments

Comments
 (0)