Current section
Files
Jump to
Current section
Files
src/tallgrass@client@request.erl
-module(tallgrass@client@request).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([get/4, get_url/3, next/3, previous/3]).
-export_type([error/0]).
-type error() :: request_error | decode_error | no_previous_page | no_next_page.
-file("/Users/stevetoro/Code/tallgrass/src/tallgrass/client/request.gleam", 93).
-spec send(gleam@http@request:request(binary())) -> {ok,
gleam@http@response:response(binary())} |
{error, error()}.
send(Request) ->
_pipe = Request,
_pipe@1 = gleam@hackney:send(_pipe),
gleam@result:replace_error(_pipe@1, request_error).
-file("/Users/stevetoro/Code/tallgrass/src/tallgrass/client/request.gleam", 75).
-spec send_and_cache(
gleam@http@request:request(binary()),
tallgrass@client@cache:cache()
) -> {ok, gleam@http@response:response(binary())} | {error, error()}.
send_and_cache(Req, Cache) ->
Req_string = begin
_pipe = Req,
_pipe@1 = gleam@http@request:to_uri(_pipe),
gleam@uri:to_string(_pipe@1)
end,
case begin
_pipe@2 = Cache,
tallgrass@client@cache:lookup(_pipe@2, Req_string)
end of
{error, _} ->
gleam@result:'try'(
begin
_pipe@3 = Req,
send(_pipe@3)
end,
fun(Res) ->
_pipe@4 = Cache,
tallgrass@client@cache:insert(
_pipe@4,
Req_string,
erlang:element(4, Res)
),
{ok, Res}
end
);
{ok, Res@1} ->
{ok,
begin
_pipe@5 = gleam@http@response:new(200),
gleam@http@response:set_body(_pipe@5, Res@1)
end}
end.
-file("/Users/stevetoro/Code/tallgrass/src/tallgrass/client/request.gleam", 99).
-spec decode(gleam@http@response:response(binary()), decode:decoder(KAK)) -> {ok,
KAK} |
{error, error()}.
decode(Response, Decoder) ->
_pipe = erlang:element(4, Response),
_pipe@1 = gleam@json:decode(
_pipe,
fun(_capture) -> decode:from(Decoder, _capture) end
),
gleam@result:replace_error(_pipe@1, decode_error).
-file("/Users/stevetoro/Code/tallgrass/src/tallgrass/client/request.gleam", 67).
-spec new(binary(), list({binary(), binary()})) -> gleam@http@request:request(binary()).
new(Path, Query) ->
_pipe = gleam@http@request:new(),
_pipe@1 = gleam@http@request:set_host(_pipe, <<"pokeapi.co/api/v2"/utf8>>),
_pipe@2 = gleam@http@request:set_path(_pipe@1, Path),
_pipe@3 = gleam@http@request:set_query(_pipe@2, Query),
gleam@http@request:set_header(
_pipe@3,
<<"Content-Type"/utf8>>,
<<"application/json"/utf8>>
).
-file("/Users/stevetoro/Code/tallgrass/src/tallgrass/client/request.gleam", 46).
-spec get(
binary(),
list({binary(), binary()}),
decode:decoder(KAA),
gleam@option:option(tallgrass@client@cache:cache())
) -> {ok, KAA} | {error, error()}.
get(Path, Query, Decoder, Cache) ->
Req = new(Path, Query),
case Cache of
{some, Cache@1} ->
gleam@io:debug(<<"Trying cache"/utf8>>),
gleam@result:'try'(
begin
_pipe = Req,
send_and_cache(_pipe, Cache@1)
end,
fun(Res) -> decode(Res, Decoder) end
);
none ->
gleam@io:debug(<<"Not trying cache"/utf8>>),
gleam@result:'try'(
begin
_pipe@1 = Req,
send(_pipe@1)
end,
fun(Res@1) -> decode(Res@1, Decoder) end
)
end.
-file("/Users/stevetoro/Code/tallgrass/src/tallgrass/client/request.gleam", 41).
-spec get_url(
binary(),
decode:decoder(JZW),
gleam@option:option(tallgrass@client@cache:cache())
) -> {ok, JZW} | {error, error()}.
get_url(Url, Decoder, Cache) ->
_assert_subject = begin
_pipe = Url,
gleam@string:split(
_pipe,
<<"https://"/utf8, "pokeapi.co/api/v2"/utf8, "/"/utf8>>
)
end,
[_, Path] = case _assert_subject of
[_, _] -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"tallgrass/client/request"/utf8>>,
function => <<"get_url"/utf8>>,
line => 42})
end,
get(Path, [], Decoder, Cache).
-file("/Users/stevetoro/Code/tallgrass/src/tallgrass/client/request.gleam", 27).
-spec next(
gleam@option:option(binary()),
decode:decoder(JZN),
gleam@option:option(tallgrass@client@cache:cache())
) -> {ok, JZN} | {error, error()}.
next(Url, Decoder, Cache) ->
case Url of
{some, Url@1} ->
get_url(Url@1, Decoder, Cache);
none ->
{error, no_next_page}
end.
-file("/Users/stevetoro/Code/tallgrass/src/tallgrass/client/request.gleam", 34).
-spec previous(
gleam@option:option(binary()),
decode:decoder(JZS),
gleam@option:option(tallgrass@client@cache:cache())
) -> {ok, JZS} | {error, error()}.
previous(Url, Decoder, Cache) ->
case Url of
{some, Url@1} ->
get_url(Url@1, Decoder, Cache);
none ->
{error, no_previous_page}
end.