Current section

Files

Jump to
scrapbook src scrapbook@internal@http.erl
Raw

src/scrapbook@internal@http.erl

-module(scrapbook@internal@http).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([fetch/1]).
-file("/home/norbert/projects/scrapbook/src/scrapbook/internal/http.gleam", 9).
-spec fetch(binary()) -> {ok, binary()} | {error, scrapbook@error:fetch_error()}.
fetch(Url) ->
gleam@result:'try'(
gleam@result:replace_error(
gleam@http@request:to(Url),
{request_construction_error, Url}
),
fun(Req) ->
Req@1 = begin
_pipe = Req,
_pipe@1 = gleam@http@request:set_header(
_pipe,
<<"accept"/utf8>>,
<<"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8"/utf8>>
),
_pipe@2 = gleam@http@request:set_header(
_pipe@1,
<<"accept-language"/utf8>>,
<<"en-US,en;q=0.6"/utf8>>
),
_pipe@3 = gleam@http@request:set_header(
_pipe@2,
<<"cache-control"/utf8>>,
<<"max-age=0"/utf8>>
),
_pipe@4 = gleam@http@request:set_header(
_pipe@3,
<<"sec-fetch-dest"/utf8>>,
<<"document"/utf8>>
),
_pipe@5 = gleam@http@request:set_header(
_pipe@4,
<<"sec-fetch-mode"/utf8>>,
<<"navigate"/utf8>>
),
_pipe@6 = gleam@http@request:set_header(
_pipe@5,
<<"sec-fetch-site"/utf8>>,
<<"same-origin"/utf8>>
),
_pipe@7 = gleam@http@request:set_header(
_pipe@6,
<<"sec-fetch-user"/utf8>>,
<<"?1"/utf8>>
),
_pipe@8 = gleam@http@request:set_header(
_pipe@7,
<<"sec-gpc"/utf8>>,
<<"1"/utf8>>
),
_pipe@9 = gleam@http@request:set_header(
_pipe@8,
<<"upgrade-insecure-requests"/utf8>>,
<<"1"/utf8>>
),
gleam@http@request:set_header(
_pipe@9,
<<"user-agent"/utf8>>,
<<"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"/utf8>>
)
end,
case begin
_pipe@10 = Req@1,
gleam@httpc:send(_pipe@10)
end of
{ok, Resp} ->
case (erlang:element(2, Resp) >= 300) andalso (erlang:element(
2,
Resp
)
< 400) of
true ->
Headers = maps:from_list(erlang:element(3, Resp)),
gleam@result:'try'(
gleam@result:replace_error(
begin
_pipe@11 = Headers,
gleam_stdlib:map_get(
_pipe@11,
<<"location"/utf8>>
)
end,
follow_redirection_error
),
fun(Location) -> fetch(Location) end
);
false ->
{ok, erlang:element(4, Resp)}
end;
{error, Err} ->
{error, {http_error, Err}}
end
end
).