Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: send feature flag event to product analytics (3/4) #56

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
509 changes: 509 additions & 0 deletions posthog/src/main/java/com/posthog/java/FeatureFlagPoller.java

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions posthog/src/main/java/com/posthog/java/Getter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.posthog.java;

import org.json.JSONObject;

import java.util.Map;

/*
* Getter interface for making HTTP GET requests to the PostHog API
*/
interface Getter {

/*
* Make a GET request to the PostHog API
*
* @param route The route to make the GET request to
* @param headers The headers to include in the GET request
* @return The JSON response from the GET request
*/
JSONObject get(String route, Map<String, String> headers);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.time.Duration;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -14,7 +15,7 @@
import okhttp3.RequestBody;
import okhttp3.Response;

public class HttpSender implements Sender {
public class HttpInteractor implements Sender, Getter {
private String apiKey;
private String host;
private OkHttpClient client;
Expand Down Expand Up @@ -53,12 +54,12 @@ public Builder initialRetryInterval(Duration initialRetryInterval) {
return this;
}

public HttpSender build() {
return new HttpSender(this);
public HttpInteractor build() {
return new HttpInteractor(this);
}
}

private HttpSender(Builder builder) {
private HttpInteractor(Builder builder) {
this.apiKey = builder.apiKey;
this.host = builder.host;
this.maxRetries = builder.maxRetries;
Expand Down Expand Up @@ -188,4 +189,35 @@ public JSONObject post(String route, String distinctId) {
}
return null;
}

@Override
public JSONObject get(String route, Map<String, String> headers) {
final String url = this.host + route;
final Request.Builder requestBuilder = new Request.Builder()
.url(url);

if (headers != null) {
headers.forEach(requestBuilder::addHeader);
}

final Request request = requestBuilder.build();

try (final Response response = this.client.newCall(request).execute()) {
if (!response.isSuccessful()) {
System.err.println("Failed to fetch feature flags: " + response.message());
return null;
}

if (response.body() == null) {
System.err.println("Failed to fetch feature flags: response body is null");
return null;
}

return new JSONObject(response.body().string());
} catch (IOException e) {
System.err.println("Failed to fetch feature flags: " + e.getMessage());
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.posthog.java;

public class InconclusiveMatchException extends Exception {
public InconclusiveMatchException(String message) {
super(message);
}
}
26 changes: 26 additions & 0 deletions posthog/src/main/java/com/posthog/java/LimitedSizeMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.posthog.java;

import java.util.LinkedHashMap;
import java.util.Map;

class LimitedSizeMap<K, V> extends LinkedHashMap<K, V> {
private final int maxSize;

LimitedSizeMap(int maxSize) {
super(maxSize, 0.75f, false);
this.maxSize = maxSize;
}

@Override
public V put(K key, V value) {
if (size() >= this.maxSize) {
clear();
}
return super.put(key, value);
}

@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return false; // We handle the removal logic in the put method
}
}
Loading