Skip to content

Commit

Permalink
Changes to enable edge functions support (#727)
Browse files Browse the repository at this point in the history
* convert WebhookIntegration to java

* remove lambda notation

* run linter

* implement all methods

* fix formatting

* add databridge abstract methods
  • Loading branch information
prayansh authored Oct 13, 2020
1 parent da2d982 commit 8315850
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 128 deletions.
8 changes: 7 additions & 1 deletion analytics/src/main/java/com/segment/analytics/Analytics.java
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,11 @@ public void flush() {
runOnMainThread(IntegrationOperation.FLUSH);
}

/** Get the underlying {@link JSMiddleware} associated with this analytics object */
public JSMiddleware getEdgeFunctionMiddleware() {
return edgeFunctionMiddleware;
}

/** Get the {@link AnalyticsContext} used by this instance. */
@SuppressWarnings("UnusedDeclaration")
public AnalyticsContext getAnalyticsContext() {
Expand Down Expand Up @@ -1559,7 +1564,8 @@ void performInitializeIntegrations(ProjectSettings projectSettings) throws Asser
throw new AssertionError("The factory key is empty!");
}
ValueMap settings = integrationSettings.getValueMap(key);
if (!(factory instanceof WebhookIntegrationFactory) && isNullOrEmpty(settings)) {
if (!(factory instanceof WebhookIntegration.WebhookIntegrationFactory)
&& isNullOrEmpty(settings)) {
logger.debug("Integration %s is not enabled.", key);
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ private AnalyticsActivityLifecycleCallbacks(
this.packageInfo = packageInfo;
}

@Override
public void onStop(@NonNull LifecycleOwner owner) {
// App in background
if (shouldTrackApplicationLifecycleEvents) {
analytics.track("Application Backgrounded");
}
}

@Override
public void onStart(@NonNull LifecycleOwner owner) {
// App in foreground
if (shouldTrackApplicationLifecycleEvents) {
Expand All @@ -88,6 +90,7 @@ public void onStart(@NonNull LifecycleOwner owner) {
}
}

@Override
public void onCreate(@NonNull LifecycleOwner owner) {
// App created
if (!trackedApplicationLifecycleEvents.getAndSet(true)
Expand All @@ -98,6 +101,15 @@ public void onCreate(@NonNull LifecycleOwner owner) {
}
}

@Override
public void onResume(@NonNull LifecycleOwner owner) {}

@Override
public void onPause(@NonNull LifecycleOwner owner) {}

@Override
public void onDestroy(@NonNull LifecycleOwner owner) {}

@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
analytics.runOnMainThread(IntegrationOperation.onActivityCreated(activity, bundle));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ public JSMiddleware(Context context) {
}

public abstract void setEdgeFunctionData(ValueMap data);

public abstract void addToDataBridge(String key, Object value);

public abstract void removeFromDataBridge(String key);

public abstract Map<String, Object> getDataBridgeSnapshot();
}
166 changes: 166 additions & 0 deletions analytics/src/main/java/com/segment/analytics/WebhookIntegration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2014 Segment.io, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.segment.analytics;

import static com.segment.analytics.internal.Utils.getInputStream;
import static com.segment.analytics.internal.Utils.readFully;

import android.util.Log;
import com.segment.analytics.integrations.AliasPayload;
import com.segment.analytics.integrations.GroupPayload;
import com.segment.analytics.integrations.IdentifyPayload;
import com.segment.analytics.integrations.Integration;
import com.segment.analytics.integrations.ScreenPayload;
import com.segment.analytics.integrations.TrackPayload;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ExecutorService;

public class WebhookIntegration extends Integration<Void> {

public static class WebhookIntegrationFactory implements Factory {
private String key, webhookUrl;

public WebhookIntegrationFactory(String key, String webhookUrl) {
this.key = key;
this.webhookUrl = webhookUrl;
}

@Override
public Integration<?> create(ValueMap settings, Analytics analytics) {
return new WebhookIntegration(
webhookUrl, analytics.tag, key(), analytics.networkExecutor);
}

@Override
public String key() {
return "webhook_" + key;
}
}

private String webhookUrl, tag, key;
private ExecutorService networkExecutor;

public WebhookIntegration(
String webhookUrl, String tag, String key, ExecutorService networkExecutor) {
this.webhookUrl = webhookUrl;
this.tag = tag;
this.key = key;
this.networkExecutor = networkExecutor;
}

/**
* Sends a JSON payload to the specified webhookUrl, with the Content-Type=application/json
* header set
*/
private void sendPayloadToWebhook(ValueMap payload) {
networkExecutor.submit(
new Runnable() {
@Override
public void run() {
try {
Log.d(
tag,
String.format(
"Running %s payload through %s",
payload.getString("type"), key));
URL requestedURL = null;
try {
requestedURL = new URL(webhookUrl);
} catch (MalformedURLException e) {
throw new IOException(
"Attempted to use malformed url: $webhookUrl", e);
}

HttpURLConnection connection =
(HttpURLConnection) requestedURL.openConnection();
connection.setDoOutput(true);
connection.setChunkedStreamingMode(0);
connection.setRequestProperty("Content-Type", "application/json");

DataOutputStream outputStream =
new DataOutputStream(connection.getOutputStream());
String payloadJson = payload.toJsonObject().toString();
outputStream.writeBytes(payloadJson);

try {
int responseCode = connection.getResponseCode();
if (responseCode >= 300) {
String responseBody;
InputStream inputStream = null;
try {
inputStream = getInputStream(connection);
responseBody = readFully(inputStream);
} catch (IOException e) {
responseBody =
"Could not read response body for rejected message: "
+ e.toString();
} finally {
if (inputStream != null) {
inputStream.close();
}
}
Log.w(
tag,
String.format(
"Failed to send payload, statusCode=%d, body=%s",
responseCode, responseBody));
}
} finally {
outputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public void identify(IdentifyPayload identify) {
sendPayloadToWebhook(identify);
}

public void group(GroupPayload group) {
sendPayloadToWebhook(group);
}

@Override
public void track(TrackPayload track) {
sendPayloadToWebhook(track);
}

@Override
public void alias(AliasPayload alias) {
sendPayloadToWebhook(alias);
}

@Override
public void screen(ScreenPayload screen) {
sendPayloadToWebhook(screen);
}
}
127 changes: 0 additions & 127 deletions analytics/src/main/java/com/segment/analytics/WebhookIntegration.kt

This file was deleted.

0 comments on commit 8315850

Please sign in to comment.