Skip to content

Commit 28146d7

Browse files
committed
refactor: rename Atex.XRPC.Adapter to Atex.HTTP.Adapter
Also provide `Atex.HTTP` module for easy delegation to the currently configured adapter.
1 parent 5e5e37b commit 28146d7

File tree

5 files changed

+25
-23
lines changed

5 files changed

+25
-23
lines changed

lib/http.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
defmodule Atex.HTTP do
2+
@adapter Application.compile_env(:atex, :adapter, Atex.HTTP.Adapter.Req)
3+
4+
defdelegate get(url, opts), to: @adapter
5+
defdelegate post(url, opts), to: @adapter
6+
end

lib/xrpc/adapter.ex renamed to lib/http/adapter.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
defmodule Atex.XRPC.Adapter do
1+
defmodule Atex.HTTP.Adapter do
22
@moduledoc """
3-
Behaviour for defining a HTTP client adapter to be used for XRPC.
3+
Behaviour for defining a HTTP client adapter to be used within atex.
44
"""
55

66
@type success() :: {:ok, map()}

lib/xrpc/adapter/req.ex renamed to lib/http/adapter/req.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
defmodule Atex.XRPC.Adapter.Req do
1+
defmodule Atex.HTTP.Adapter.Req do
22
@moduledoc """
3-
`Req` adapter for XRPC.
3+
`Req` adapter for atex.
44
"""
55

6-
@behaviour Atex.XRPC.Adapter
6+
@behaviour Atex.HTTP.Adapter
77

88
def get(url, opts) do
99
Req.get(url, opts) |> adapt()

lib/xrpc.ex

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
defmodule Atex.XRPC do
2-
alias Atex.XRPC
3-
4-
defp adapter do
5-
Application.get_env(:atex, :adapter, XRPC.Adapter.Req)
6-
end
2+
alias Atex.{HTTP, XRPC}
73

84
# TODO: automatic user-agent, and env for changing it
95

@@ -16,38 +12,38 @@ defmodule Atex.XRPC do
1612
@doc """
1713
Perform a HTTP GET on a XRPC resource. Called a "query" in lexicons.
1814
"""
19-
@spec get(XRPC.Client.t(), String.t(), keyword()) :: XRPC.Adapter.result()
15+
@spec get(XRPC.Client.t(), String.t(), keyword()) :: HTTP.Adapter.result()
2016
def get(%XRPC.Client{} = client, name, opts \\ []) do
2117
opts = put_auth(opts, client.access_token)
22-
adapter().get(url(client, name), opts)
18+
HTTP.get(url(client, name), opts)
2319
end
2420

2521
@doc """
2622
Perform a HTTP POST on a XRPC resource. Called a "prodecure" in lexicons.
2723
"""
28-
@spec post(XRPC.Client.t(), String.t(), keyword()) :: XRPC.Adapter.result()
24+
@spec post(XRPC.Client.t(), String.t(), keyword()) :: HTTP.Adapter.result()
2925
def post(%XRPC.Client{} = client, name, opts \\ []) do
3026
# TODO: look through available HTTP clients and see if they have a
3127
# consistent way of providing JSON bodies with auto content-type. If not,
3228
# create one for adapters.
3329
opts = put_auth(opts, client.access_token)
34-
adapter().post(url(client, name), opts)
30+
HTTP.post(url(client, name), opts)
3531
end
3632

3733
@doc """
3834
Like `get/3` but is unauthenticated by default.
3935
"""
40-
@spec unauthed_get(String.t(), String.t(), keyword()) :: XRPC.Adapter.result()
36+
@spec unauthed_get(String.t(), String.t(), keyword()) :: HTTP.Adapter.result()
4137
def unauthed_get(endpoint, name, opts \\ []) do
42-
adapter().get(url(endpoint, name), opts)
38+
HTTP.get(url(endpoint, name), opts)
4339
end
4440

4541
@doc """
4642
Like `post/3` but is unauthenticated by default.
4743
"""
48-
@spec unauthed_post(String.t(), String.t(), keyword()) :: XRPC.Adapter.result()
44+
@spec unauthed_post(String.t(), String.t(), keyword()) :: HTTP.Adapter.result()
4945
def unauthed_post(endpoint, name, opts \\ []) do
50-
adapter().post(url(endpoint, name), opts)
46+
HTTP.post(url(endpoint, name), opts)
5147
end
5248

5349
# TODO: use URI module for joining instead?

lib/xrpc/client.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
defmodule Atex.XRPC.Client do
2-
@doc """
2+
@moduledoc """
33
Struct to store client information for ATProto XRPC.
44
"""
55

6-
alias Atex.XRPC
6+
alias Atex.{XRPC, HTTP}
77
use TypedStruct
88

99
typedstruct do
@@ -39,9 +39,9 @@ defmodule Atex.XRPC.Client do
3939
iex> Atex.XRPC.Client.login("https://bsky.social", "example.com", "password123")
4040
{:ok, %Atex.XRPC.Client{...}}
4141
"""
42-
@spec login(String.t(), String.t(), String.t()) :: {:ok, t()} | XRPC.Adapter.error()
42+
@spec login(String.t(), String.t(), String.t()) :: {:ok, t()} | HTTP.Adapter.error()
4343
@spec login(String.t(), String.t(), String.t(), String.t() | nil) ::
44-
{:ok, t()} | XRPC.Adapter.error()
44+
{:ok, t()} | HTTP.Adapter.error()
4545
def login(endpoint, identifier, password, auth_factor_token \\ nil) do
4646
json =
4747
%{identifier: identifier, password: password}
@@ -67,7 +67,7 @@ defmodule Atex.XRPC.Client do
6767
@doc """
6868
Request a new `refresh_token` for the given client.
6969
"""
70-
@spec refresh(t()) :: {:ok, t()} | XRPC.Adapter.error()
70+
@spec refresh(t()) :: {:ok, t()} | HTTP.Adapter.error()
7171
def refresh(%__MODULE__{endpoint: endpoint, refresh_token: refresh_token} = client) do
7272
response =
7373
XRPC.unauthed_post(

0 commit comments

Comments
 (0)