Current section
Files
Jump to
Current section
Files
src/biscotto.erl
-module(biscotto).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([init/0, peek/1, get/2, put/2, remove/2, from_response/2, with_cookies/2]).
-export_type([cookie/0, cookie_jar/0]).
-type cookie() :: {cookie,
binary(),
binary(),
binary(),
binary(),
binary(),
boolean()}.
-type cookie_jar() :: {cookie_jar, gleam@dict:dict(binary(), cookie())}.
-spec init() -> cookie_jar().
init() ->
{cookie_jar, gleam@dict:new()}.
-spec peek(cookie_jar()) -> list(cookie()).
peek(Jar) ->
_pipe = erlang:element(2, Jar),
gleam@dict:values(_pipe).
-spec get(cookie_jar(), binary()) -> {ok, cookie()} | {error, nil}.
get(Jar, Key) ->
_pipe = erlang:element(2, Jar),
gleam@dict:get(_pipe, Key).
-spec cookie_from_list(list({binary(), binary()})) -> {ok, cookie()} |
{error, nil}.
cookie_from_list(Cookie) ->
Kv = gleam@list:first(Cookie),
case Kv of
{ok, Kv@1} ->
{Key, Value} = Kv@1,
Expires@1 = case gleam@list:find(
Cookie,
fun(El) ->
{Name, _} = El,
Name =:= <<"expires"/utf8>>
end
) of
{ok, Expires} ->
erlang:element(2, Expires);
{error, _} ->
<<""/utf8>>
end,
Domain@1 = case gleam@list:find(
Cookie,
fun(El@1) ->
{Name@1, _} = El@1,
Name@1 =:= <<"domain"/utf8>>
end
) of
{ok, Domain} ->
erlang:element(2, Domain);
{error, _} ->
<<""/utf8>>
end,
Path@1 = case gleam@list:find(
Cookie,
fun(El@2) ->
{Name@2, _} = El@2,
Name@2 =:= <<"path"/utf8>>
end
) of
{ok, Path} ->
erlang:element(2, Path);
{error, _} ->
<<""/utf8>>
end,
Secure@1 = case gleam@list:find(
Cookie,
fun(El@3) ->
{Name@3, _} = El@3,
Name@3 =:= <<"secure"/utf8>>
end
) of
{ok, Secure} ->
erlang:element(2, Secure) =:= <<"true"/utf8>>;
{error, _} ->
false
end,
{ok, {cookie, Key, Value, Domain@1, Path@1, Expires@1, Secure@1}};
{error, _} ->
{error, nil}
end.
-spec put(cookie_jar(), list({binary(), binary()})) -> cookie_jar().
put(Jar, Cookie) ->
case cookie_from_list(Cookie) of
{ok, Cookie@1} ->
{cookie_jar,
begin
_pipe = erlang:element(2, Jar),
gleam@dict:insert(
_pipe,
erlang:element(2, Cookie@1),
Cookie@1
)
end};
{error, _} ->
Jar
end.
-spec remove(cookie_jar(), binary()) -> cookie_jar().
remove(Jar, Key) ->
{cookie_jar,
begin
_pipe = erlang:element(2, Jar),
gleam@dict:delete(_pipe, Key)
end}.
-spec from_response(cookie_jar(), gleam@http@response:response(any())) -> cookie_jar().
from_response(Jar, Resp) ->
{response, _, Headers, _} = Resp,
_pipe = Headers,
_pipe@1 = gleam@list:filter_map(
_pipe,
fun(Header) ->
{Name, Value} = Header,
case Name of
<<"set-cookie"/utf8>> ->
{ok, gleam@http@cookie:parse(Value)};
_ ->
{error, nil}
end
end
),
gleam@list:fold(
_pipe@1,
Jar,
fun(Jar@1, Cookie) -> case cookie_from_list(Cookie) of
{ok, Cookie@1} ->
{cookie_jar,
begin
_pipe@2 = erlang:element(2, Jar@1),
gleam@dict:insert(
_pipe@2,
erlang:element(2, Cookie@1),
Cookie@1
)
end};
{error, _} ->
Jar@1
end end
).
-spec with_cookies(gleam@http@request:request(GRU), cookie_jar()) -> gleam@http@request:request(GRU).
with_cookies(Req, Jar) ->
_pipe = erlang:element(2, Jar),
gleam@dict:fold(_pipe, Req, fun(Req@1, Key, Cookie) -> _pipe@1 = Req@1,
gleam@http@request:set_cookie(
_pipe@1,
Key,
erlang:element(3, Cookie)
) end).