-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
Metrics.set/1
resource action (#5)
Also adds `Metrics.flush/0` and resource action param validation.
- Loading branch information
Showing
12 changed files
with
274 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import Config | ||
|
||
config :kickplan, :base_url, "https://demo-control.fly.dev/api" | ||
|
||
if File.exists?("config/config.secret.exs") do | ||
import_config "config.secret.exs" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule Kickplan.Requests.Metrics.Set do | ||
@moduledoc """ | ||
TODO | ||
""" | ||
|
||
@options NimbleOptions.new!( | ||
key: [ | ||
type: :string, | ||
required: true | ||
], | ||
value: [ | ||
type: {:or, [:float, :integer]}, | ||
required: true | ||
], | ||
account_key: [type: :string], | ||
idempotency_key: [type: :string], | ||
time: [type: :any] | ||
) | ||
|
||
@doc false | ||
def options, do: @options | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
defmodule Kickplan.Metrics do | ||
@moduledoc """ | ||
TODO | ||
""" | ||
|
||
alias Kickplan.{Client, Requests} | ||
|
||
@doc """ | ||
TODO | ||
""" | ||
def flush do | ||
with {:ok, resp} <- Client.post("metrics/flush") do | ||
{:ok, resp.success?} | ||
end | ||
end | ||
|
||
@doc """ | ||
TODO | ||
""" | ||
def set(opts \\ %{}) do | ||
with {:ok, params} <- validate(opts, Requests.Metrics.Set), | ||
{:ok, resp} <- Client.post("metrics/set", params) do | ||
{:ok, resp.success?} | ||
end | ||
end | ||
|
||
defp validate(opts, request) do | ||
NimbleOptions.validate(opts, request.options()) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
[ | ||
{ | ||
"request": { | ||
"options": { | ||
"with_body": "true" | ||
}, | ||
"body": "{}", | ||
"url": "https://demo-control.fly.dev/api/features/", | ||
"headers": { | ||
"Authorization": "***", | ||
"Content-Type": "application/json", | ||
"User-Agent": "Kickplan/0.1.0 (sdk-elixir)" | ||
}, | ||
"method": "post", | ||
"request_body": "" | ||
}, | ||
"response": { | ||
"binary": false, | ||
"type": "ok", | ||
"body": "[{\"value\":false,\"key\":\"upload-contacts\"},{\"value\":false,\"key\":\"ai-llm-generation\"},{\"value\":false,\"key\":\"fancy\"},{\"value\":\"{\\n \\\"Currency\\\": \\\"US Dollar (USD)\\\",\\n \\\"Allowed Payment Gateways\\\": [\\\"PayPal\\\", \\\"Stripe\\\", \\\"Square\\\"]\\n }\",\"key\":\"payment-configuration\"},{\"value\":0,\"key\":\"contact-limit\"}]", | ||
"headers": { | ||
"cache-control": "max-age=0, private, must-revalidate", | ||
"content-length": "308", | ||
"content-type": "application/json; charset=utf-8", | ||
"date": "Thu, 22 Aug 2024 19:19:14 GMT", | ||
"server": "Fly/5e55b43a7 (2024-08-21)", | ||
"x-request-id": "F-4jJk91UgTYSXkAAAyB", | ||
"via": "1.1 fly.io", | ||
"fly-request-id": "01J5XREQJRTKHBC3Y5CJQAPDYK-sea" | ||
}, | ||
"status_code": 200 | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
[ | ||
{ | ||
"request": { | ||
"options": { | ||
"with_body": "true" | ||
}, | ||
"body": "{}", | ||
"url": "https://demo-control.fly.dev/api/features/contact-limit", | ||
"headers": { | ||
"Authorization": "***", | ||
"Content-Type": "application/json", | ||
"User-Agent": "Kickplan/0.1.0 (sdk-elixir)" | ||
}, | ||
"method": "post", | ||
"request_body": "" | ||
}, | ||
"response": { | ||
"binary": false, | ||
"type": "ok", | ||
"body": "{\"value\":0,\"key\":\"contact-limit\"}", | ||
"headers": { | ||
"cache-control": "max-age=0, private, must-revalidate", | ||
"content-length": "33", | ||
"content-type": "application/json; charset=utf-8", | ||
"date": "Thu, 22 Aug 2024 19:43:57 GMT", | ||
"server": "Fly/5e55b43a7 (2024-08-21)", | ||
"x-request-id": "F-4kf6KMXpujMxwAAAyx", | ||
"via": "1.1 fly.io", | ||
"fly-request-id": "01J5XSVZZEBDC5XTBGR0QKYXXJ-sea" | ||
}, | ||
"status_code": 200 | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
[ | ||
{ | ||
"request": { | ||
"options": { | ||
"with_body": "true" | ||
}, | ||
"body": "{}", | ||
"url": "https://demo-control.fly.dev/api/metrics/flush", | ||
"headers": { | ||
"Authorization": "***", | ||
"Content-Type": "application/json", | ||
"User-Agent": "Kickplan/0.1.0 (sdk-elixir)" | ||
}, | ||
"method": "post", | ||
"request_body": "" | ||
}, | ||
"response": { | ||
"binary": false, | ||
"type": "ok", | ||
"body": "", | ||
"headers": { | ||
"cache-control": "max-age=0, private, must-revalidate", | ||
"date": "Thu, 22 Aug 2024 22:37:26 GMT", | ||
"server": "Fly/5e55b43a7 (2024-08-21)", | ||
"x-request-id": "F-4t9xPC0bL6wOQAAA0B", | ||
"via": "1.1 fly.io", | ||
"fly-request-id": "01J5Y3SMMC67WZPQJYTVY1ZVP0-sea", | ||
"content-length": "0" | ||
}, | ||
"status_code": 202 | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
[ | ||
{ | ||
"request": { | ||
"options": { | ||
"with_body": "true" | ||
}, | ||
"body": "{\"value\":3,\"time\":\"2024-08-22T21:40:19.923716Z\",\"key\":\"sdk_elixir_test\",\"account_key\":\"9a592f57-6da0-408e-99e7-8918b48a7dbe\",\"idempotency_key\":\"004b7f07-71fd-4ea5-a43c-dcde2516305b\"}", | ||
"url": "https://demo-control.fly.dev/api/metrics/set", | ||
"headers": { | ||
"Authorization": "***", | ||
"Content-Type": "application/json", | ||
"User-Agent": "Kickplan/0.1.0 (sdk-elixir)" | ||
}, | ||
"method": "post", | ||
"request_body": "" | ||
}, | ||
"response": { | ||
"binary": false, | ||
"type": "ok", | ||
"body": "", | ||
"headers": { | ||
"cache-control": "max-age=0, private, must-revalidate", | ||
"date": "Thu, 22 Aug 2024 21:40:19 GMT", | ||
"server": "Fly/5e55b43a7 (2024-08-21)", | ||
"x-request-id": "F-4q2U-SUX7DPj0AAAzx", | ||
"via": "1.1 fly.io", | ||
"fly-request-id": "01J5Y0H2HYJAJ2PJVFS2D0TM7A-sea", | ||
"content-length": "0" | ||
}, | ||
"status_code": 202 | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
defmodule Kickplan.FeaturesTest do | ||
use Kickplan.VCRCase, async: true | ||
|
||
alias Kickplan.Features | ||
alias Kickplan.Schema.Resolution | ||
|
||
describe "resolve/2" do | ||
test "success: resolves all features" do | ||
use_cassette "features/resolve-all" do | ||
{:ok, resolutions} = Features.resolve() | ||
|
||
assert length(resolutions) == 5 | ||
|
||
assert Enum.all?(resolutions, fn resolution -> | ||
%Resolution{} = resolution | ||
end) | ||
end | ||
end | ||
|
||
test "success: resolves a single feature" do | ||
use_cassette "features/resolve" do | ||
{:ok, resolution} = Features.resolve("contact-limit") | ||
|
||
assert %Resolution{key: "contact-limit"} = resolution | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule Kickplan.MetricsTest do | ||
use Kickplan.VCRCase, async: true | ||
|
||
alias Kickplan.Metrics | ||
|
||
describe "flush/0" do | ||
test "success: flushes the metrics" do | ||
use_cassette "metrics/flush" do | ||
assert {:ok, true} = Metrics.flush() | ||
end | ||
end | ||
end | ||
|
||
describe "set/1" do | ||
test "success: resolves a single feature" do | ||
use_cassette "metrics/set" do | ||
assert {:ok, true} = | ||
Metrics.set( | ||
key: "sdk_elixir_test", | ||
value: 3, | ||
account_key: "9a592f57-6da0-408e-99e7-8918b48a7dbe", | ||
time: DateTime.utc_now(), | ||
idempotency_key: "004b7f07-71fd-4ea5-a43c-dcde2516305b" | ||
) | ||
end | ||
end | ||
|
||
test "failure: request has invalid params" do | ||
assert {:error, %NimbleOptions.ValidationError{}} = | ||
Metrics.set(key: "sdk_elixir_test") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule Kickplan.VCRCase do | ||
@moduledoc false | ||
|
||
use ExUnit.CaseTemplate | ||
|
||
using do | ||
quote do | ||
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney | ||
end | ||
end | ||
|
||
setup do | ||
ExVCR.Config.cassette_library_dir("test/cassettes") | ||
ExVCR.Config.filter_request_headers("Authorization") | ||
|
||
:ok | ||
end | ||
end |