Packages

Render wisp HTTP responses into a human-readable representation

Current section

Files

Jump to
wispie src wispie.erl
Raw

src/wispie.erl

-module(wispie).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/wispie.gleam").
-export([response_to_string/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.
-file("src/wispie.gleam", 37).
-spec format_status(integer()) -> binary().
format_status(Status) ->
erlang:integer_to_binary(Status).
-file("src/wispie.gleam", 41).
-spec format_headers(list({binary(), binary()})) -> binary().
format_headers(Headers) ->
_pipe = Headers,
_pipe@1 = gleam@list:map(
_pipe,
fun(Header) ->
<<<<(erlang:element(1, Header))/binary, ": "/utf8>>/binary,
(erlang:element(2, Header))/binary>>
end
),
gleam@string:join(_pipe@1, <<"\n"/utf8>>).
-file("src/wispie.gleam", 67).
-spec get_content_type(gleam@http@response:response(wisp:body())) -> {ok,
contenty:content_type()} |
{error, nil}.
get_content_type(Response) ->
case gleam@http@response:get_header(Response, <<"content-type"/utf8>>) of
{ok, Content_type_header} ->
_pipe = contenty:parse(Content_type_header),
gleam@result:replace_error(_pipe, nil);
{error, E} ->
{error, E}
end.
-file("src/wispie.gleam", 47).
-spec format_body(gleam@http@response:response(wisp:body())) -> binary().
format_body(Response) ->
Content_type = begin
_pipe = get_content_type(Response),
gleam@option:from_result(_pipe)
end,
case erlang:element(4, Response) of
{text, Content} ->
wispie@internal@pretty_print:format_content(Content, Content_type);
{bytes, Bytes} ->
case begin
_pipe@1 = erlang:list_to_bitstring(Bytes),
gleam@bit_array:to_string(_pipe@1)
end of
{error, _} ->
<<"!!! Invalid UTF-8 Response Body !!!"/utf8>>;
{ok, Content@1} ->
Content@1
end;
{file, Path, Offset, Limit} ->
wispie@internal@pretty_print:format_file(Path, Offset, Limit)
end.
-file("src/wispie.gleam", 27).
?DOC(
" Converts an HTTP `response.Response(wisp.Body)` into a human-readable string.\n"
"\n"
" This function renders the full HTTP response snapshot, including:\n"
" - **Status code**\n"
" - **Headers**\n"
" - **Body**, formatted according to its `Content-Type`\n"
"\n"
" Body rendering rules:\n"
" - `wisp.Text` bodies are pretty-printed based on their content type (e.g. JSON, HTML).\n"
" - `wisp.Bytes` bodies are decoded as UTF-8 when possible; otherwise a placeholder is shown.\n"
" - `wisp.File` bodies are displayed as a formatted file reference with offset and limit.\n"
).
-spec response_to_string(gleam@http@response:response(wisp:body())) -> binary().
response_to_string(Response) ->
_pipe = gleam@string_tree:new(),
_pipe@1 = gleam@string_tree:append(
_pipe,
format_status(erlang:element(2, Response))
),
_pipe@2 = gleam@string_tree:append(_pipe@1, <<"\n"/utf8>>),
_pipe@3 = gleam@string_tree:append(
_pipe@2,
format_headers(erlang:element(3, Response))
),
_pipe@4 = gleam@string_tree:append(_pipe@3, <<"\n\n"/utf8>>),
_pipe@5 = gleam@string_tree:append(_pipe@4, format_body(Response)),
unicode:characters_to_binary(_pipe@5).