Skip to content

Commit

Permalink
Add support for tier cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
gordalina committed May 4, 2023
1 parent 6021ade commit 06c3188
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ end

### Versioning

ExTier follow's Tier's major and minor versions, but reserves minor versions for ExTier's patch updates. So Tier's `v0.6` version will map to ExTier `0.6.x` version.
ExTier follows Tier's major and minor versions, but reserves minor versions for ExTier's patch updates. So Tier's `v0.6` version will map to ExTier `0.6.x` version.

## Configuration

Expand All @@ -32,6 +32,16 @@ You need to specify where the Tier server is reachable at:
config :ex_tier, url: "http://localhost:8080"
```

### Tier Cloud

You can use tier cloud by using the following configuration:

```elixir
config :ex_tier,
url: "https://api.tier.run",
stripe_api_key: "your-stripe-api-key"
```

### Test Clocks

To use test clocks, pass the clock_id in the configuration:
Expand Down
1 change: 1 addition & 0 deletions lib/client/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule ExTier.Client do
apply(adapter, :call, [env, opts])
end)

plug(ExTier.Client.TierCloudMiddleware)
plug(ExTier.Client.ClockMiddleware)
plug(ExTier.Client.ResponseMiddleware)
plug(Tesla.Middleware.BaseUrl, "#{Application.fetch_env!(:ex_tier, :url)}/v1")
Expand Down
27 changes: 27 additions & 0 deletions lib/client/tier_cloud_middleware.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule ExTier.Client.TierCloudMiddleware do
@moduledoc false

@behaviour Tesla.Middleware

require Logger

@impl Tesla.Middleware
def call(env, next, _options) do
env
|> Tesla.put_headers(authorization_header())
|> Tesla.run(next)
end

defp authorization_header() do
:ex_tier
|> Application.fetch_env(:stripe_api_key)
|> create_header()
end

defp create_header({:ok, nil}), do: []

defp create_header({:ok, stripe_api_key}),
do: [{"authorization", "Basic " <> Base.encode64(stripe_api_key <> ":")}]

defp create_header(_), do: []
end
25 changes: 25 additions & 0 deletions test/client/tier_cloud_middleware_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule ExTier.Client.TierCloudMiddlewareTest do
use ExUnit.Case, async: false

import ExUnit.CaptureLog

alias ExTier.Client

describe "with tier cloud" do
setup do
Application.put_env(:ex_tier, :stripe_api_key, nil)
on_exit(fn -> Application.put_env(:ex_tier, :stripe_api_key, nil) end)
end

test "enabled" do
Application.put_env(:ex_tier, :stripe_api_key, "api-key")
Tesla.Mock.mock(fn env -> %{env | status: 200, body: env.headers} end)
assert {:ok, [{"authorization", "Basic YXBpLWtleTo="}]} == Client.get("/")
end

test "disabled" do
Tesla.Mock.mock(fn env -> %{env | status: 200, body: env.headers} end)
assert {:ok, []} == Client.get("/")
end
end
end

0 comments on commit 06c3188

Please sign in to comment.