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

refactor: ditch HTTP adapter

ovyerus.com 81de840e a5d95b4d

verified
Changed files
+3 -73
lib
atex
http
identity_resolver
-6
lib/atex/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
-13
lib/atex/http/adapter.ex
···
-
defmodule Atex.HTTP.Adapter do
-
@moduledoc """
-
Behaviour for defining a HTTP client adapter to be used within atex.
-
"""
-
alias Atex.HTTP.Response
-
-
@type success() :: {:ok, Response.t()}
-
@type error() :: {:error, Response.t() | term()}
-
@type result() :: success() | error()
-
-
@callback get(url :: String.t(), opts :: keyword()) :: result()
-
@callback post(url :: String.t(), opts :: keyword()) :: result()
-
end
-38
lib/atex/http/adapter/req.ex
···
-
defmodule Atex.HTTP.Adapter.Req do
-
@moduledoc """
-
`Req` adapter for atex.
-
"""
-
-
@behaviour Atex.HTTP.Adapter
-
-
@impl true
-
def get(url, opts) do
-
Req.get(url, opts) |> adapt()
-
end
-
-
@impl true
-
def post(url, opts) do
-
Req.post(url, opts) |> adapt()
-
end
-
-
@spec adapt({:ok, Req.Response.t()} | {:error, any()}) :: Atex.HTTP.Adapter.result()
-
defp adapt({:ok, %Req.Response{status: status} = res}) when status < 400 do
-
{:ok, to_response(res)}
-
end
-
-
defp adapt({:ok, %Req.Response{} = res}) do
-
{:error, to_response(res)}
-
end
-
-
defp adapt({:error, exception}) do
-
{:error, exception}
-
end
-
-
defp to_response(%Req.Response{} = res) do
-
%Atex.HTTP.Response{
-
body: res.body,
-
status: res.status,
-
__raw__: res
-
}
-
end
-
end
-13
lib/atex/http/response.ex
···
-
defmodule Atex.HTTP.Response do
-
@moduledoc """
-
A generic response struct to be returned by an `Atex.HTTP.Adapter`.
-
"""
-
-
use TypedStruct
-
-
typedstruct enforce: true do
-
field :status, integer()
-
field :body, any()
-
field :__raw__, any()
-
end
-
end
+2 -2
lib/atex/identity_resolver/did.ex
···
@spec resolve_plc(String.t()) :: resolution_result()
defp resolve_plc("did:plc:" <> _id = did) do
with {:ok, resp} when resp.status in 200..299 <-
-
Atex.HTTP.get("https://plc.directory/#{did}", []),
+
Req.get("https://plc.directory/#{did}"),
{:ok, body} <- decode_body(resp.body),
{:ok, document} <- DIDDocument.from_json(body),
:ok <- DIDDocument.validate_for_atproto(document, did) do
···
@spec resolve_web(String.t()) :: resolution_result()
defp resolve_web("did:web:" <> domain = did) do
with {:ok, resp} when resp.status in 200..299 <-
-
Atex.HTTP.get("https://#{domain}/.well-known/did.json", []),
+
Req.get("https://#{domain}/.well-known/did.json"),
{:ok, body} <- decode_body(resp.body),
{:ok, document} <- DIDDocument.from_json(body),
:ok <- DIDDocument.validate_for_atproto(document, did) do
+1 -1
lib/atex/identity_resolver/handle.ex
···
@spec resolve_via_http(String.t()) :: {:ok, String.t()} | :error
defp resolve_via_http(handle) do
-
case Atex.HTTP.get("https://#{handle}/.well-known/atproto-did", []) do
+
case Req.get("https://#{handle}/.well-known/atproto-did") do
{:ok, %{body: "did:" <> _ = did}} -> {:ok, did}
_ -> :error
end