Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion sdk_compliance_adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,20 @@ The adapter is a standalone Elixir application that:

### Endpoints

- `GET /health` - Health check, returns SDK name/version
- `GET /health` - Health check, returns SDK name/version and supported capabilities
- `POST /init` - Initialize SDK with configuration
- `POST /capture` - Capture a single event
- `POST /flush` - Flush pending events
- `POST /get_feature_flag` - Evaluate a feature flag against the `/flags` API
- `GET /state` - Get internal state for test assertions
- `POST /reset` - Reset SDK state

### Capabilities

The adapter declares `capture_v0` and `encoding_gzip` capabilities, which gates
the test suites the harness will run. The `feature_flags` suite has no
capability requirement and runs unconditionally.

## Documentation

For complete documentation, see:
Expand Down
76 changes: 75 additions & 1 deletion sdk_compliance_adapter/lib/sdk_compliance_adapter/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ defmodule SdkComplianceAdapter.Router do
response = %{
sdk_name: "posthog-elixir",
sdk_version: @sdk_version,
adapter_version: @adapter_version
adapter_version: @adapter_version,
capabilities: ["capture_v0", "encoding_gzip"]
}

json_response(conn, 200, response)
Expand Down Expand Up @@ -118,6 +119,58 @@ defmodule SdkComplianceAdapter.Router do
json_response(conn, 200, response)
end

# POST /get_feature_flag - Evaluate a feature flag
post "/get_feature_flag" do
params = conn.body_params

key = params["key"]
distinct_id = params["distinct_id"]

cond do
is_nil(key) ->
json_response(conn, 400, %{success: false, error: "Missing key"})

is_nil(distinct_id) ->
json_response(conn, 400, %{success: false, error: "Missing distinct_id"})

true ->
# Build the /flags request body. The PostHog Elixir SDK forwards the
# body to the /flags endpoint as-is (api_key is auto-injected by the
# API client). The harness asserts on the actual /flags HTTP request,
# so we mirror person_properties.distinct_id here per the contract:
# "auto-added distinct_id in person_properties".
person_properties =
(params["person_properties"] || %{})
|> Map.put("distinct_id", distinct_id)

body =
%{
distinct_id: distinct_id,
person_properties: person_properties,
groups: params["groups"] || %{},
group_properties: params["group_properties"] || %{},
flag_keys_to_evaluate: [key]
}
|> maybe_put(:geoip_disable, params["disable_geoip"])

case PostHog.FeatureFlags.flags(SdkComplianceAdapter.PostHog, body) do
{:ok, %{status: 200, body: %{"flags" => flags}}} ->
value = extract_flag_value(flags, key)
json_response(conn, 200, %{success: true, value: value})

{:ok, %{status: status, body: resp_body}} ->
json_response(conn, 200, %{
success: false,
error: "Unexpected response status: #{status}",
response: inspect(resp_body)
})

{:error, reason} ->
json_response(conn, 200, %{success: false, error: inspect(reason)})
end
end
end

# POST /reset - Reset SDK state
post "/reset" do
stop_posthog()
Expand Down Expand Up @@ -182,6 +235,27 @@ defmodule SdkComplianceAdapter.Router do
end
end

defp maybe_put(map, _key, nil), do: map
defp maybe_put(map, key, value), do: Map.put(map, key, value)

defp extract_flag_value(flags, key) when is_map(flags) do
case Map.get(flags, key) do
nil ->
nil

flag_data when is_map(flag_data) ->
cond do
is_binary(flag_data["variant"]) -> flag_data["variant"]
true -> Map.get(flag_data, "enabled", false) == true
end

other ->
other
end
end

defp extract_flag_value(_flags, _key), do: nil

defp stop_posthog do
case Process.whereis(SdkComplianceAdapter.PostHog) do
nil ->
Expand Down
Loading