Skip to content

Commit 8b8a983

Browse files
authored
feat: Added ODP RestApi interface for sending Events to ODP (#485)
## Summary This module provides an internal service for ODP event REST api access. ## Test plan - Manually tested thoroughly. - Added new unit tests. - All Existing Unit tests pass. - All existing Full stack compatibility tests pass. ## Jira [OASIS-8387](https://optimizely.atlassian.net/browse/OASIS-8387)
1 parent 1cf2d72 commit 8b8a983

File tree

13 files changed

+541
-3
lines changed

13 files changed

+541
-3
lines changed

core-api/src/main/java/com/optimizely/ab/odp/ODPApiManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@
1919

2020
public interface ODPApiManager {
2121
String fetchQualifiedSegments(String apiKey, String apiEndpoint, String userKey, String userValue, List<String> segmentsToCheck);
22+
23+
Integer sendEvents(String apiKey, String apiEndpoint, String eventPayload);
2224
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Copyright 2022, Optimizely Inc. and contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.optimizely.ab.odp;
17+
18+
import java.util.Map;
19+
20+
public class ODPEvent {
21+
private String type;
22+
private String action;
23+
private Map<String, String > identifiers;
24+
private Map<String, Object> data;
25+
26+
public ODPEvent(String type, String action, Map<String, String> identifiers, Map<String, Object> data) {
27+
this.type = type;
28+
this.action = action;
29+
this.identifiers = identifiers;
30+
this.data = data;
31+
}
32+
33+
public String getType() {
34+
return type;
35+
}
36+
37+
public void setType(String type) {
38+
this.type = type;
39+
}
40+
41+
public String getAction() {
42+
return action;
43+
}
44+
45+
public void setAction(String action) {
46+
this.action = action;
47+
}
48+
49+
public Map<String, String> getIdentifiers() {
50+
return identifiers;
51+
}
52+
53+
public void setIdentifiers(Map<String, String> identifiers) {
54+
this.identifiers = identifiers;
55+
}
56+
57+
public Map<String, Object> getData() {
58+
return data;
59+
}
60+
61+
public void setData(Map<String, Object> data) {
62+
this.data = data;
63+
}
64+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright 2022, Optimizely Inc. and contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.optimizely.ab.odp.serializer;
17+
18+
import com.optimizely.ab.odp.ODPEvent;
19+
20+
import java.util.List;
21+
22+
public interface ODPJsonSerializer {
23+
public String serializeEvents(List<ODPEvent> events);
24+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright 2022, Optimizely Inc. and contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.optimizely.ab.odp.serializer;
17+
18+
import com.optimizely.ab.internal.JsonParserProvider;
19+
import com.optimizely.ab.odp.serializer.impl.GsonSerializer;
20+
import com.optimizely.ab.odp.serializer.impl.JacksonSerializer;
21+
import com.optimizely.ab.odp.serializer.impl.JsonSerializer;
22+
import com.optimizely.ab.odp.serializer.impl.JsonSimpleSerializer;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
26+
public class ODPJsonSerializerFactory {
27+
private static final Logger logger = LoggerFactory.getLogger(ODPJsonSerializerFactory.class);
28+
29+
public static ODPJsonSerializer getSerializer() {
30+
JsonParserProvider parserProvider = JsonParserProvider.getDefaultParser();
31+
ODPJsonSerializer jsonSerializer = null;
32+
switch (parserProvider) {
33+
case GSON_CONFIG_PARSER:
34+
jsonSerializer = new GsonSerializer();
35+
break;
36+
case JACKSON_CONFIG_PARSER:
37+
jsonSerializer = new JacksonSerializer();
38+
break;
39+
case JSON_CONFIG_PARSER:
40+
jsonSerializer = new JsonSerializer();
41+
break;
42+
case JSON_SIMPLE_CONFIG_PARSER:
43+
jsonSerializer = new JsonSimpleSerializer();
44+
break;
45+
}
46+
logger.info("Using " + parserProvider.toString() + " serializer");
47+
return jsonSerializer;
48+
}
49+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright 2022, Optimizely Inc. and contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.optimizely.ab.odp.serializer.impl;
17+
18+
import com.google.gson.Gson;
19+
import com.google.gson.GsonBuilder;
20+
import com.optimizely.ab.odp.ODPEvent;
21+
import com.optimizely.ab.odp.serializer.ODPJsonSerializer;
22+
23+
import java.util.List;
24+
25+
public class GsonSerializer implements ODPJsonSerializer {
26+
@Override
27+
public String serializeEvents(List<ODPEvent> events) {
28+
Gson gson = new GsonBuilder().serializeNulls().create();
29+
return gson.toJson(events);
30+
}
31+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright 2022, Optimizely Inc. and contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.optimizely.ab.odp.serializer.impl;
17+
18+
import com.fasterxml.jackson.core.JsonProcessingException;
19+
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import com.optimizely.ab.odp.ODPEvent;
21+
import com.optimizely.ab.odp.serializer.ODPJsonSerializer;
22+
23+
import java.util.List;
24+
25+
public class JacksonSerializer implements ODPJsonSerializer {
26+
@Override
27+
public String serializeEvents(List<ODPEvent> events) {
28+
ObjectMapper objectMapper = new ObjectMapper();
29+
try {
30+
return objectMapper.writeValueAsString(events);
31+
} catch (JsonProcessingException e) {
32+
// log error here
33+
}
34+
return null;
35+
}
36+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright 2022, Optimizely Inc. and contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.optimizely.ab.odp.serializer.impl;
17+
18+
import com.optimizely.ab.odp.ODPEvent;
19+
import com.optimizely.ab.odp.serializer.ODPJsonSerializer;
20+
import org.json.JSONArray;
21+
import org.json.JSONObject;
22+
23+
import java.util.List;
24+
import java.util.Map;
25+
26+
public class JsonSerializer implements ODPJsonSerializer {
27+
@Override
28+
public String serializeEvents(List<ODPEvent> events) {
29+
JSONArray jsonArray = new JSONArray();
30+
for (ODPEvent event: events) {
31+
JSONObject eventObject = new JSONObject();
32+
eventObject.put("type", event.getType());
33+
eventObject.put("action", event.getAction());
34+
35+
if (event.getIdentifiers() != null) {
36+
JSONObject identifiers = new JSONObject();
37+
for (Map.Entry<String, String> identifier : event.getIdentifiers().entrySet()) {
38+
identifiers.put(identifier.getKey(), identifier.getValue());
39+
}
40+
eventObject.put("identifiers", identifiers);
41+
}
42+
43+
if (event.getData() != null) {
44+
JSONObject data = new JSONObject();
45+
for (Map.Entry<String, Object> dataEntry : event.getData().entrySet()) {
46+
data.put(dataEntry.getKey(), getJSONObjectValue(dataEntry.getValue()));
47+
}
48+
eventObject.put("data", data);
49+
}
50+
51+
jsonArray.put(eventObject);
52+
}
53+
return jsonArray.toString();
54+
}
55+
56+
private static Object getJSONObjectValue(Object value) {
57+
return value == null ? JSONObject.NULL : value;
58+
}
59+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright 2022, Optimizely Inc. and contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.optimizely.ab.odp.serializer.impl;
17+
18+
import com.optimizely.ab.odp.ODPEvent;
19+
import com.optimizely.ab.odp.serializer.ODPJsonSerializer;
20+
import org.json.simple.JSONArray;
21+
import org.json.simple.JSONObject;
22+
23+
import java.util.List;
24+
import java.util.Map;
25+
26+
public class JsonSimpleSerializer implements ODPJsonSerializer {
27+
@Override
28+
public String serializeEvents(List<ODPEvent> events) {
29+
JSONArray jsonArray = new JSONArray();
30+
for (ODPEvent event: events) {
31+
JSONObject eventObject = new JSONObject();
32+
eventObject.put("type", event.getType());
33+
eventObject.put("action", event.getAction());
34+
35+
if (event.getIdentifiers() != null) {
36+
JSONObject identifiers = new JSONObject();
37+
for (Map.Entry<String, String> identifier : event.getIdentifiers().entrySet()) {
38+
identifiers.put(identifier.getKey(), identifier.getValue());
39+
}
40+
eventObject.put("identifiers", identifiers);
41+
}
42+
43+
if (event.getData() != null) {
44+
JSONObject data = new JSONObject();
45+
for (Map.Entry<String, Object> dataEntry : event.getData().entrySet()) {
46+
data.put(dataEntry.getKey(), dataEntry.getValue());
47+
}
48+
eventObject.put("data", data);
49+
}
50+
51+
jsonArray.add(eventObject);
52+
}
53+
return jsonArray.toJSONString();
54+
}
55+
}

core-api/src/test/java/com/optimizely/ab/odp/parser/ResponseJsonParserFactoryTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.optimizely.ab.odp.parser;
1717

18-
import com.optimizely.ab.internal.JsonParserProvider;
1918
import com.optimizely.ab.internal.PropertyUtils;
2019
import com.optimizely.ab.odp.parser.impl.GsonParser;
2120
import com.optimizely.ab.odp.parser.impl.JsonParser;

0 commit comments

Comments
 (0)