A set of utilities for working with the AT Protocol in Elixir.

refactor: rename `Atex.XRPC.Adapter` to `Atex.HTTP.Adapter`

Also provide `Atex.HTTP` module for easy delegation to the currently configured adapter.

ovyerus.com 28146d78 5e5e37bc

verified
Changed files
+25 -23
lib
+6
lib/http.ex
···
+
defmodule Atex.HTTP do
+
@adapter Application.compile_env(:atex, :adapter, Atex.HTTP.Adapter.Req)
+
+
defdelegate get(url, opts), to: @adapter
+
defdelegate post(url, opts), to: @adapter
+
end
+9 -13
lib/xrpc.ex
···
defmodule Atex.XRPC do
-
alias Atex.XRPC
-
-
defp adapter do
-
Application.get_env(:atex, :adapter, XRPC.Adapter.Req)
-
end
+
alias Atex.{HTTP, XRPC}
# TODO: automatic user-agent, and env for changing it
···
@doc """
Perform a HTTP GET on a XRPC resource. Called a "query" in lexicons.
"""
-
@spec get(XRPC.Client.t(), String.t(), keyword()) :: XRPC.Adapter.result()
+
@spec get(XRPC.Client.t(), String.t(), keyword()) :: HTTP.Adapter.result()
def get(%XRPC.Client{} = client, name, opts \\ []) do
opts = put_auth(opts, client.access_token)
-
adapter().get(url(client, name), opts)
+
HTTP.get(url(client, name), opts)
end
@doc """
Perform a HTTP POST on a XRPC resource. Called a "prodecure" in lexicons.
"""
-
@spec post(XRPC.Client.t(), String.t(), keyword()) :: XRPC.Adapter.result()
+
@spec post(XRPC.Client.t(), String.t(), keyword()) :: HTTP.Adapter.result()
def post(%XRPC.Client{} = client, name, opts \\ []) do
# TODO: look through available HTTP clients and see if they have a
# consistent way of providing JSON bodies with auto content-type. If not,
# create one for adapters.
opts = put_auth(opts, client.access_token)
-
adapter().post(url(client, name), opts)
+
HTTP.post(url(client, name), opts)
end
@doc """
Like `get/3` but is unauthenticated by default.
"""
-
@spec unauthed_get(String.t(), String.t(), keyword()) :: XRPC.Adapter.result()
+
@spec unauthed_get(String.t(), String.t(), keyword()) :: HTTP.Adapter.result()
def unauthed_get(endpoint, name, opts \\ []) do
-
adapter().get(url(endpoint, name), opts)
+
HTTP.get(url(endpoint, name), opts)
end
@doc """
Like `post/3` but is unauthenticated by default.
"""
-
@spec unauthed_post(String.t(), String.t(), keyword()) :: XRPC.Adapter.result()
+
@spec unauthed_post(String.t(), String.t(), keyword()) :: HTTP.Adapter.result()
def unauthed_post(endpoint, name, opts \\ []) do
-
adapter().post(url(endpoint, name), opts)
+
HTTP.post(url(endpoint, name), opts)
end
# TODO: use URI module for joining instead?
+2 -2
lib/xrpc/adapter.ex lib/http/adapter.ex
···
-
defmodule Atex.XRPC.Adapter do
+
defmodule Atex.HTTP.Adapter do
@moduledoc """
-
Behaviour for defining a HTTP client adapter to be used for XRPC.
+
Behaviour for defining a HTTP client adapter to be used within atex.
"""
@type success() :: {:ok, map()}
+3 -3
lib/xrpc/adapter/req.ex lib/http/adapter/req.ex
···
-
defmodule Atex.XRPC.Adapter.Req do
+
defmodule Atex.HTTP.Adapter.Req do
@moduledoc """
-
`Req` adapter for XRPC.
+
`Req` adapter for atex.
"""
-
@behaviour Atex.XRPC.Adapter
+
@behaviour Atex.HTTP.Adapter
def get(url, opts) do
Req.get(url, opts) |> adapt()
+5 -5
lib/xrpc/client.ex
···
defmodule Atex.XRPC.Client do
-
@doc """
+
@moduledoc """
Struct to store client information for ATProto XRPC.
"""
-
alias Atex.XRPC
+
alias Atex.{XRPC, HTTP}
use TypedStruct
typedstruct do
···
iex> Atex.XRPC.Client.login("https://bsky.social", "example.com", "password123")
{:ok, %Atex.XRPC.Client{...}}
"""
-
@spec login(String.t(), String.t(), String.t()) :: {:ok, t()} | XRPC.Adapter.error()
+
@spec login(String.t(), String.t(), String.t()) :: {:ok, t()} | HTTP.Adapter.error()
@spec login(String.t(), String.t(), String.t(), String.t() | nil) ::
-
{:ok, t()} | XRPC.Adapter.error()
+
{:ok, t()} | HTTP.Adapter.error()
def login(endpoint, identifier, password, auth_factor_token \\ nil) do
json =
%{identifier: identifier, password: password}
···
@doc """
Request a new `refresh_token` for the given client.
"""
-
@spec refresh(t()) :: {:ok, t()} | XRPC.Adapter.error()
+
@spec refresh(t()) :: {:ok, t()} | HTTP.Adapter.error()
def refresh(%__MODULE__{endpoint: endpoint, refresh_token: refresh_token} = client) do
response =
XRPC.unauthed_post(