Skip to content

Commit 8f6aa5d

Browse files
authored
Configured params and creds should be passed to client (#38)
1 parent 8672c3a commit 8f6aa5d

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

lib/phoenix/sync/electric.ex

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -519,16 +519,9 @@ defmodule Phoenix.Sync.Electric do
519519
end
520520

521521
defp http_mode_plug_opts(electric_config) do
522-
with {:ok, url} <- fetch_with_error(electric_config, :url),
523-
credential_params = electric_config |> Keyword.get(:credentials, []) |> Map.new(),
524-
extra_params = electric_config |> Keyword.get(:params, []) |> Map.new(),
525-
params = Map.merge(extra_params, credential_params),
526-
{:ok, client} <-
527-
Electric.Client.new(
528-
base_url: url,
529-
params: params,
530-
fetch: {Electric.Client.Fetch.HTTP, [request: [raw: true]]}
531-
) do
522+
with {:ok, client} <- configure_client(electric_config, :http) do
523+
# don't decode the body - just pass it directly
524+
client = %{client | fetch: {Electric.Client.Fetch.HTTP, [request: [raw: true]]}}
532525
{:ok, %Phoenix.Sync.Electric.ClientAdapter{client: client}}
533526
end
534527
end
@@ -543,13 +536,15 @@ defmodule Phoenix.Sync.Electric do
543536
end
544537
end
545538

546-
defp configure_client(opts, :http) do
547-
case Keyword.fetch(opts, :url) do
548-
{:ok, url} ->
549-
Electric.Client.new(base_url: url)
550-
551-
:error ->
552-
{:error, "`phoenix_sync[:electric][:url]` not set for phoenix_sync in HTTP mode"}
539+
defp configure_client(electric_config, :http) do
540+
with {:ok, url} <- fetch_with_error(electric_config, :url),
541+
credential_params = electric_config |> Keyword.get(:credentials, []) |> Map.new(),
542+
extra_params = electric_config |> Keyword.get(:params, []) |> Map.new(),
543+
params = Map.merge(extra_params, credential_params) do
544+
Electric.Client.new(
545+
base_url: url,
546+
params: params
547+
)
553548
end
554549
end
555550
end

test/phoenix/sync/client_test.exs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ defmodule Phoenix.Sync.ClientTest do
8383

8484
assert_http_client(client, "http://api.electric-sql.cloud/v1/shape")
8585
end
86+
87+
test "passes credentials into client" do
88+
config = [
89+
mode: :http,
90+
url: "http://api.electric-sql.cloud",
91+
credentials: [
92+
secret: "my-secret",
93+
source_id: "my-source-id"
94+
],
95+
params: %{
96+
something: "here"
97+
}
98+
]
99+
100+
assert {:ok, client} = Client.new(config)
101+
102+
assert client.params == %{secret: "my-secret", source_id: "my-source-id", something: "here"}
103+
end
86104
end
87105

88106
describe "stream" do

0 commit comments

Comments
 (0)