Skip to content

Commit

Permalink
avoid formatting responses by default
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedsoupe committed May 14, 2024
1 parent 1d514da commit a3d6928
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
16 changes: 16 additions & 0 deletions lib/supabase/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,22 @@ defmodule Supabase.Client do
end
end

def update_access_token(source, access_token) when is_atom(source) do
if pid = ClientRegistry.lookup(source) do
path = [Access.key(:conn), Access.key(:access_token)]
Agent.update(pid, &put_in(&1, path, access_token))
else
{:error, :client_not_started}
end
end

def update_access_token(source, access_token) when is_pid(source) do
path = [Access.key(:conn), Access.key(:access_token)]
Agent.update(source, &put_in(&1, path, access_token))
rescue
_ -> {:error, :client_not_started}
end

def retrieve_base_url(%__MODULE__{conn: conn}) do
conn.base_url
end
Expand Down
2 changes: 1 addition & 1 deletion lib/supabase/client/conn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ defmodule Supabase.Client.Conn do

cond do
not changeset.valid? -> changeset
token -> changeset
not is_nil(token) -> changeset
true -> put_change(changeset, :access_token, api_key)
end
end
Expand Down
20 changes: 10 additions & 10 deletions lib/supabase/fetcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ defmodule Supabase.Fetcher do
{:ok, %{"key" => "value"}}
"""
@impl true
def post(url, body \\ nil, headers \\ []) do
def post(url, body \\ nil, headers \\ [], opts \\ []) do
headers = merge_headers(headers, [{"content-type", "application/json"}])

:post
|> new_connection(url, Jason.encode_to_iodata!(body), headers)
|> Finch.request(Supabase.Finch)
|> format_response()
|> then(&if opts[:resolve_json], do: format_response(&1), else: &1)
end

@doc """
Expand All @@ -163,41 +163,41 @@ defmodule Supabase.Fetcher do
{:ok, %{"key" => "value"}}
"""
@impl true
def put(url, body, headers \\ []) do
def put(url, body, headers \\ [], opts \\ []) do
headers = merge_headers(headers, [{"content-type", "application/json"}])

:put
|> new_connection(url, Jason.encode_to_iodata!(body), headers)
|> Finch.request(Supabase.Finch)
|> format_response()
|> then(&if opts[:resolve_json], do: format_response(&1), else: &1)
end

@doc """
Simple HEAD request that format the response to a map or
retrieve the error as `String.t()`
"""
@impl true
def head(url, body, headers \\ []) do
def head(url, body, headers \\ [], opts \\ []) do
headers = merge_headers(headers, [{"content-type", "application/json"}])

:head
|> new_connection(url, Jason.encode_to_iodata!(body), headers)
|> Finch.request(Supabase.Finch)
|> format_response()
|> then(&if opts[:resolve_json], do: format_response(&1), else: &1)
end

@doc """
Simple PATCH request that format the response to a map or
retrieve the error as `String.t()`
"""
@impl true
def patch(url, body, headers \\ []) do
def patch(url, body, headers \\ [], opts \\ []) do
headers = merge_headers(headers, [{"content-type", "application/json"}])

:put
|> new_connection(url, Jason.encode_to_iodata!(body), headers)
|> Finch.request(Supabase.Finch)
|> format_response()
|> then(&if opts[:resolve_json], do: format_response(&1), else: &1)
end

@doc """
Expand All @@ -213,13 +213,13 @@ defmodule Supabase.Fetcher do
{:error, :not_found}
"""
@impl true
def delete(url, body \\ nil, headers \\ []) do
def delete(url, body \\ nil, headers \\ [], opts \\ []) do
headers = merge_headers(headers, [{"content-type", "application/json"}])

:delete
|> new_connection(url, Jason.encode_to_iodata!(body), headers)
|> Finch.request(Supabase.Finch)
|> format_response()
|> then(&if opts[:resolve_json], do: format_response(&1), else: &1)
end

@doc """
Expand Down
12 changes: 6 additions & 6 deletions lib/supabase/fetcher_behaviour.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ defmodule Supabase.FetcherBehaviour do
@type url :: String.t() | URI.t()
@type body :: nullable(map) | map
@type headers :: [{String.t(), String.t()}]
@type opts :: [resolve_json: boolean] | []
@type response :: map | String.t()
@type reason :: String.t() | atom
@type method :: :get | :post
@type result :: {:ok, response} | {:error, reason}

@callback get(url, body, headers, opts) :: result
when opts: [resolve_json: boolean]
@callback post(url, body, headers) :: result
@callback put(url, body, headers) :: result
@callback head(url, body, headers) :: result
@callback patch(url, body, headers) :: result
@callback delete(url, body, headers) :: result
@callback post(url, body, headers, opts) :: result
@callback put(url, body, headers, opts) :: result
@callback head(url, body, headers, opts) :: result
@callback patch(url, body, headers, opts) :: result
@callback delete(url, body, headers, opts) :: result
@callback upload(method, url, Path.t(), headers) :: result
@callback stream(url, headers, keyword) :: {:ok, stream} | {:error, reason}
when stream: Enumerable.t()
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Supabase.Potion.MixProject do
use Mix.Project

@version "0.3.6"
@version "0.3.7"
@source_url "https://github.com/zoedsoupe/supabase"

def project do
Expand Down

0 comments on commit a3d6928

Please sign in to comment.