Packages
The shared sans-io core of the gleam-atproto client packages: XRPC error vocabulary and response handling, plus the OAuth effect kernel.
Current section
Files
Jump to
Current section
Files
src/atproto_core@oauth@effect.erl
-module(atproto_core@oauth@effect).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/atproto_core/oauth/effect.gleam").
-export([done/1, then/2, map/2, 'try'/2, map_error/2, fetch/1, get_checked/1, fetch_bits/1, proof/4]).
-export_type([effect/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" A sans-IO effect kernel: an `Effect(a)` is a pure description of the\n"
" HTTP + DPoP steps a flow needs, interpreted by a per-target runner.\n"
" Keys never appear here; `DpopProof` asks the runner for a proof.\n"
" `Fetch` carries string bodies (OAuth endpoints), `FetchBits` binary\n"
" (the resource path).\n"
).
-type effect(ETN) :: {fetch,
gleam@http@request:request(binary()),
fun(({ok, gleam@http@response:response(binary())} |
{error, atproto_core@xrpc:transport_error()}) -> effect(ETN))} |
{fetch_bits,
gleam@http@request:request(bitstring()),
fun(({ok, gleam@http@response:response(bitstring())} |
{error, atproto_core@xrpc:transport_error()}) -> effect(ETN))} |
{dpop_proof,
binary(),
binary(),
gleam@option:option(binary()),
gleam@option:option(binary()),
fun(({ok, binary()} | {error, binary()}) -> effect(ETN))} |
{done, ETN}.
-file("src/atproto_core/oauth/effect.gleam", 32).
-spec done(ETO) -> effect(ETO).
done(Value) ->
{done, Value}.
-file("src/atproto_core/oauth/effect.gleam", 37).
?DOC(" Monadic bind, `use`-syntax friendly: `use x <- effect.then(some_effect)`.\n").
-spec then(effect(ETQ), fun((ETQ) -> effect(ETS))) -> effect(ETS).
then(Effect, F) ->
case Effect of
{done, Value} ->
F(Value);
{fetch, Request, Next} ->
{fetch, Request, fun(R) -> then(Next(R), F) end};
{fetch_bits, Request@1, Next@1} ->
{fetch_bits, Request@1, fun(R@1) -> then(Next@1(R@1), F) end};
{dpop_proof, Method, Url, Nonce, Ath, Next@2} ->
{dpop_proof,
Method,
Url,
Nonce,
Ath,
fun(R@2) -> then(Next@2(R@2), F) end}
end.
-file("src/atproto_core/oauth/effect.gleam", 47).
-spec map(effect(ETV), fun((ETV) -> ETX)) -> effect(ETX).
map(Effect, F) ->
then(Effect, fun(A) -> {done, F(A)} end).
-file("src/atproto_core/oauth/effect.gleam", 53).
?DOC(
" Short-circuiting bind over `Effect(Result(_, e))`:\n"
" `use ok <- effect.try(eff)` stops at the first `Error`.\n"
).
-spec 'try'(
effect({ok, ETZ} | {error, EUA}),
fun((ETZ) -> effect({ok, EUE} | {error, EUA}))
) -> effect({ok, EUE} | {error, EUA}).
'try'(Effect, F) ->
then(Effect, fun(Result) -> case Result of
{ok, A} ->
F(A);
{error, E} ->
{done, {error, E}}
end end).
-file("src/atproto_core/oauth/effect.gleam", 65).
-spec map_error(effect({ok, EUL} | {error, EUM}), fun((EUM) -> EUQ)) -> effect({ok,
EUL} |
{error, EUQ}).
map_error(Effect, F) ->
map(Effect, fun(_capture) -> gleam@result:map_error(_capture, F) end).
-file("src/atproto_core/oauth/effect.gleam", 85).
?DOC(" A string-bodied HTTP request, yielding the response (or a transport error).\n").
-spec fetch(gleam@http@request:request(binary())) -> effect({ok,
gleam@http@response:response(binary())} |
{error, atproto_core@xrpc:transport_error()}).
fetch(Request) ->
{fetch, Request, fun(Field@0) -> {done, Field@0} end}.
-file("src/atproto_core/oauth/effect.gleam", 74).
?DOC(
" GET `url` and status-check the response: the shared shape of the\n"
" metadata and resolver lookups.\n"
).
-spec get_checked(binary()) -> effect({ok,
gleam@http@response:response(binary())} |
{error, atproto_core@xrpc:xrpc_error()}).
get_checked(Url) ->
case gleam@http@request:to(Url) of
{error, _} ->
{done, {error, {request_failed, {invalid_url, Url}}}};
{ok, Req} ->
'try'(
begin
_pipe = fetch(Req),
map_error(
_pipe,
fun(Field@0) -> {request_failed, Field@0} end
)
end,
fun(Resp) -> {done, atproto_core@xrpc:check_ok(Resp)} end
)
end.
-file("src/atproto_core/oauth/effect.gleam", 92).
?DOC(" A binary HTTP request (the resource path, for blobs), yielding the response.\n").
-spec fetch_bits(gleam@http@request:request(bitstring())) -> effect({ok,
gleam@http@response:response(bitstring())} |
{error, atproto_core@xrpc:transport_error()}).
fetch_bits(Request) ->
{fetch_bits, Request, fun(Field@0) -> {done, Field@0} end}.
-file("src/atproto_core/oauth/effect.gleam", 100).
?DOC(
" Ask the runner for a DPoP proof bound to this method+URL (and optional nonce\n"
" and access-token hash). The runner signs it with its platform key.\n"
).
-spec proof(
binary(),
binary(),
gleam@option:option(binary()),
gleam@option:option(binary())
) -> effect({ok, binary()} | {error, binary()}).
proof(Method, Url, Nonce, Ath) ->
{dpop_proof, Method, Url, Nonce, Ath, fun(Field@0) -> {done, Field@0} end}.