Skip to content

Commit

Permalink
Merge pull request #118 from orbit-apps/jason/migrate-to-contexts-api
Browse files Browse the repository at this point in the history
Migrate from deprecated Users API to contexts API
  • Loading branch information
freemer authored Jul 26, 2024
2 parents 0a7bc55 + 2685cf8 commit 50ab1c5
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 41 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## Unreleased

## 0.3.0

- Migrate away from deprecated Users api
- BREAKING: removal of LaunchDarklyAPI.UserSettings and LaunchDarklyAPI.Users modules
- BREAKING: fixes a typo in a FeatureFlag.UserFlag struct key
- New: LaunchDarklyAPI.ContextSettings module

## 0.2.3

- Bump elixir, erlang, and all deps
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ WARNING: Not complete.
```elixir
def deps do
[
{:launch_darkly_api, github: "orbit-apps/elixir-launchdarklyapi", tag: "v0.2.3"}
{:launch_darkly_api, github: "orbit-apps/elixir-launchdarklyapi", tag: "v0.3.0"}
]
end
```
Expand Down
23 changes: 23 additions & 0 deletions lib/launch_darkly_api/context_settings.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule LaunchDarklyAPI.ContextSettings do
alias LaunchDarklyAPI.REST

def list(project_key, env_key, context_kind, context_key) do
REST.post(
"projects/#{project_key}/environments/#{env_key}/flags/evaluate",
%{key: context_key, kind: context_kind}
)
end

def update(project_key, env_key, context_kind, context_key, feature_key, values) do
REST.put(
"projects/#{project_key}/environments/#{env_key}/contexts/#{context_kind}/#{context_key}/flags/#{feature_key}",
values
)
end

def enable(project_key, env_key, context_kind, context_key, feature_key),
do: update(project_key, env_key, context_kind, context_key, feature_key, %{setting: true})

def disable(project_key, env_key, context_kind, context_key, feature_key),
do: update(project_key, env_key, context_kind, context_key, feature_key, %{setting: false})
end
25 changes: 13 additions & 12 deletions lib/launch_darkly_api/feature_flag/user_flag.ex
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
defmodule LaunchDarklyAPI.FeatureFlag.UserFlag do
alias LaunchDarklyAPI.UserSettings
alias LaunchDarklyAPI.ContextSettings

require Logger

defstruct environment: "staging", feature: "", product: "", user: "", value: false
defstruct environment: "staging", feature: "", project: "", user: "", value: false

@type t :: %__MODULE__{
environment: String.t(),
feature: String.t(),
product: String.t(),
project: String.t(),
user: String.t(),
value: boolean
}

@default_env "staging"

@spec update(String.t()) :: list()
def all(product, user) do
case UserSettings.list(product, get_env(), user) do
@spec all(String.t(), String.t()) :: list()
def all(project, user) do
case ContextSettings.list(project, get_env(), "user", user) do
{:ok, %{"items" => flags}} ->
Enum.map(flags, fn {k, %{"setting" => v}} -> new(product, user, k, v == true) end)
Enum.map(flags, fn %{"key" => k, "_value" => v} -> new(project, user, k, v == true) end)

_ ->
[]
Expand All @@ -32,24 +32,25 @@ defmodule LaunchDarklyAPI.FeatureFlag.UserFlag do

update_func =
case feature_flag.value do
true -> &UserSettings.enable/4
false -> &UserSettings.disable/4
true -> &ContextSettings.enable/5
false -> &ContextSettings.disable/5
end

update_func.(
feature_flag.product,
feature_flag.project,
feature_flag.environment,
"user",
feature_flag.user,
feature_flag.feature
)
end

@spec new(String.t(), String.t(), String.t(), boolean) :: __MODULE__.t()
def new(product, user, key, value) do
def new(project, user, key, value) do
%__MODULE__{
environment: get_env(),
feature: key,
product: product,
project: project,
user: user,
value: value
}
Expand Down
18 changes: 0 additions & 18 deletions lib/launch_darkly_api/user_settings.ex

This file was deleted.

9 changes: 0 additions & 9 deletions lib/launch_darkly_api/users.ex

This file was deleted.

2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule LaunchDarklyAPI.MixProject do
use Mix.Project

@version "0.2.3"
@version "0.3.0"

def project do
[
Expand Down

0 comments on commit 50ab1c5

Please sign in to comment.