Skip to content

Commit ec15751

Browse files
committed
Handle variants
1 parent d362ac9 commit ec15751

File tree

1 file changed

+72
-73
lines changed

1 file changed

+72
-73
lines changed

examples/feature_flag_demo/lib/feature_flag_demo.ex

Lines changed: 72 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,34 @@ defmodule FeatureFlagDemo do
33
A simple console application to demonstrate PostHog feature flag functionality.
44
"""
55

6+
@default_api_url "https://app.posthog.com"
7+
68
def main(args) do
7-
# Print configuration for debugging
8-
api_url = System.get_env("POSTHOG_API_URL", "https://app.posthog.com")
9+
print_info(:config)
10+
args
11+
|> parse_args()
12+
|> process()
13+
end
14+
15+
defp print_info(:config) do
16+
api_url = System.get_env("POSTHOG_API_URL", @default_api_url)
917
api_key = System.get_env("POSTHOG_API_KEY")
1018

1119
IO.puts("Using API URL: #{api_url}")
12-
IO.puts("Using API Key: #{String.slice(api_key || "", 0, 8)}...") # Only show first 8 chars of key for security
20+
IO.puts("Using API Key: #{String.slice(api_key || "", 0, 8)}...")
21+
end
1322

14-
args
15-
|> parse_args()
16-
|> process()
23+
defp print_info(:usage) do
24+
IO.puts("""
25+
Usage: mix run run.exs --flag FLAG_NAME --distinct-id USER_ID [options]
26+
27+
Options:
28+
--flag FLAG_NAME The name of the feature flag to check
29+
--distinct-id USER_ID The distinct ID of the user
30+
--groups GROUPS JSON string of group properties (optional)
31+
--group_properties PROPERTIES JSON string of group properties (optional)
32+
--person_properties PROPERTIES JSON string of person properties (optional)
33+
""")
1734
end
1835

1936
defp parse_args(args) do
@@ -32,82 +49,64 @@ defmodule FeatureFlagDemo do
3249
opts
3350
end
3451

35-
defp process([]) do
36-
IO.puts("""
37-
Usage: mix run run.exs --flag FLAG_NAME --distinct-id USER_ID [options]
38-
39-
Options:
40-
--flag FLAG_NAME The name of the feature flag to check
41-
--distinct-id USER_ID The distinct ID of the user
42-
--groups GROUPS JSON string of group properties (optional)
43-
--group_properties PROPERTIES JSON string of group properties (optional)
44-
--person_properties PROPERTIES JSON string of person properties (optional)
45-
""")
46-
end
52+
defp process([]), do: print_info(:usage)
4753

48-
defp process(opts) do
49-
flag = Keyword.get(opts, :flag)
50-
distinct_id = Keyword.get(opts, :distinct_id)
54+
defp process([flag: flag, distinct_id: distinct_id] = opts) do
55+
IO.puts("Checking feature flag '#{flag}' for user '#{distinct_id}'...")
5156

52-
if is_nil(flag) or is_nil(distinct_id) do
53-
IO.puts("Error: --flag and --distinct-id are both required")
54-
process([])
57+
with {:ok, response} <- call_feature_flag(flag, distinct_id, opts),
58+
{:ok, message} <- format_response(flag, response) do
59+
IO.puts(message)
5560
else
56-
check_feature_flag(flag, distinct_id, opts)
61+
{:error, %{status: 403}} -> print_auth_error()
62+
{:error, %{status: status, body: body}} -> IO.puts("Error: Received status #{status}\nResponse body: #{inspect(body)}")
63+
{:error, reason} -> IO.puts("Error: #{inspect(reason)}")
5764
end
5865
end
5966

60-
defp check_feature_flag(flag, distinct_id, opts) do
61-
groups = parse_json(Keyword.get(opts, :groups))
62-
group_properties = parse_json(Keyword.get(opts, :group_properties))
63-
person_properties = parse_json(Keyword.get(opts, :person_properties))
67+
defp process(_), do: print_info(:usage)
6468

65-
IO.puts("Checking feature flag '#{flag}' for user '#{distinct_id}'...")
69+
defp call_feature_flag(flag, distinct_id, opts) do
70+
Posthog.feature_flag(flag, distinct_id,
71+
groups: parse_json(opts[:groups]),
72+
group_properties: parse_json(opts[:group_properties]),
73+
person_properties: parse_json(opts[:person_properties])
74+
)
75+
end
6676

67-
case Posthog.feature_flag(flag, distinct_id,
68-
groups: groups,
69-
group_properties: group_properties,
70-
person_properties: person_properties
71-
) do
72-
{:ok, %{enabled: true, payload: payload}} ->
73-
IO.puts("Feature flag '#{flag}' is ENABLED")
74-
IO.puts("Payload: #{inspect(payload)}")
75-
76-
{:ok, %{enabled: false}} ->
77-
IO.puts("Feature flag '#{flag}' is DISABLED")
78-
79-
{:ok, %{enabled: variant, payload: payload}} when is_binary(variant) ->
80-
IO.puts("Feature flag '#{flag}' is ENABLED with variant: #{variant}")
81-
if payload, do: IO.puts("Payload: #{inspect(payload)}")
82-
83-
{:error, %{status: 403}} ->
84-
IO.puts("""
85-
Error: Authentication failed (403 Forbidden)
86-
87-
Please check that:
88-
1. Your POSTHOG_API_KEY is set correctly
89-
2. Your POSTHOG_API_URL is set correctly (if using a self-hosted instance)
90-
3. The API key has the necessary permissions
91-
4. Your local PostHog instance is running and accessible
92-
93-
You can set these environment variables:
94-
export POSTHOG_API_KEY="your_project_api_key"
95-
export POSTHOG_API_URL="http://localhost:8000" # Note: no trailing slash
96-
""")
97-
98-
{:error, %{status: status, body: body}} ->
99-
IO.puts("""
100-
Error: Received status #{status}
101-
Response body: #{inspect(body)}
102-
""")
103-
104-
{:error, reason} ->
105-
IO.puts("Error: #{inspect(reason)}")
77+
defp format_response(flag, %{enabled: enabled, payload: payload}) do
78+
message = case enabled do
79+
true -> "Feature flag '#{flag}' is ENABLED"
80+
false -> "Feature flag '#{flag}' is DISABLED"
81+
variant when is_binary(variant) -> "Feature flag '#{flag}' is ENABLED with variant: #{variant}"
10682
end
83+
84+
message = if payload, do: message <> "\nPayload: #{inspect(payload)}", else: message
85+
{:ok, message}
10786
end
10887

109-
defp parse_json(nil), do: nil
110-
defp parse_json(""), do: nil
111-
defp parse_json(json) when is_binary(json), do: Jason.decode!(json)
112-
defp parse_json(value), do: value
88+
defp print_auth_error do
89+
IO.puts("""
90+
Error: Authentication failed (403 Forbidden)
91+
92+
Please check that:
93+
1. Your POSTHOG_API_KEY is set correctly
94+
2. Your POSTHOG_API_URL is set correctly (if using a self-hosted instance)
95+
3. The API key has the necessary permissions
96+
4. Your local PostHog instance is running and accessible
97+
98+
You can set these environment variables:
99+
export POSTHOG_API_KEY="your_project_api_key"
100+
export POSTHOG_API_URL="http://localhost:8000" # Note: no trailing slash
101+
""")
102+
end
103+
104+
defp parse_json(value) do
105+
case value do
106+
nil -> nil
107+
"" -> nil
108+
json when is_binary(json) -> Jason.decode!(json)
109+
other -> other
110+
end
111+
end
113112
end

0 commit comments

Comments
 (0)