Current section

Files

Jump to
dream_http_client src dream_http_client@fetch.erl
Raw

src/dream_http_client@fetch.erl

-module(dream_http_client@fetch).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/dream_http_client/fetch.gleam").
-export([request/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(
" Non-streaming HTTP request functionality\n"
"\n"
" This module provides non-streaming HTTP request functionality that collects\n"
" all response chunks and returns the complete response body as a string.\n"
"\n"
" Use this module when you need the complete response body at once, such as\n"
" for JSON API responses or small files. For large responses or streaming\n"
" data, use `dream_http_client/stream` instead.\n"
"\n"
" ## Quick Start\n"
"\n"
" ```gleam\n"
" import dream_http_client/client\n"
" import dream_http_client/fetch\n"
" import gleam/http\n"
"\n"
" let result = client.new\n"
" |> client.host(\"api.example.com\")\n"
" |> client.path(\"/users/123\")\n"
" |> fetch.request()\n"
"\n"
" case result {\n"
" Ok(body) -> decode_json(body)\n"
" Error(msg) -> handle_error(msg)\n"
" }\n"
" ```\n"
).
-file("src/dream_http_client/fetch.gleam", 80).
?DOC(
" Make a non-streaming HTTP request\n"
"\n"
" Sends an HTTP request and collects all response chunks, returning the\n"
" complete response body as a string. This is ideal for:\n"
"\n"
" - JSON API responses\n"
" - Small files or documents\n"
" - Any case where you need the full response before processing\n"
"\n"
" For large responses (files, streaming data, or AI responses), use\n"
" `stream.stream_request()` instead to process chunks as they arrive.\n"
"\n"
" ## Parameters\n"
"\n"
" - `client_request`: The configured HTTP request\n"
"\n"
" ## Returns\n"
"\n"
" - `Ok(String)`: The complete response body as a string\n"
" - `Error(String)`: An error message if the request failed or body conversion failed\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import dream_http_client/client\n"
" import dream_http_client/fetch\n"
" import gleam/http\n"
" import gleam/json\n"
"\n"
" let result = client.new\n"
" |> client.host(\"api.example.com\")\n"
" |> client.path(\"/users/123\")\n"
" |> client.add_header(\"Authorization\", \"Bearer \" <> token)\n"
" |> fetch.request()\n"
"\n"
" case result {\n"
" Ok(body) -> {\n"
" case json.decode(body, user_decoder) {\n"
" Ok(user) -> Ok(user)\n"
" Error(_) -> Error(\"Invalid JSON response\")\n"
" }\n"
" }\n"
" Error(msg) -> Error(\"Request failed: \" <> msg)\n"
" }\n"
" ```\n"
).
-spec request(dream_http_client@client:client_request()) -> {ok, binary()} |
{error, binary()}.
request(Client_request) ->
Chunks = begin
_pipe = dream_http_client@stream:stream_request(Client_request),
gleam@yielder:to_list(_pipe)
end,
case Chunks of
[] ->
{ok, <<""/utf8>>};
[Single] ->
_pipe@1 = erlang:list_to_bitstring(Single),
_pipe@2 = gleam@bit_array:to_string(_pipe@1),
gleam@result:map_error(
_pipe@2,
fun(_) -> <<"Failed to convert response to string"/utf8>> end
);
Multiple ->
_pipe@3 = gleam_stdlib:identity(Multiple),
_pipe@4 = erlang:list_to_bitstring(_pipe@3),
_pipe@5 = gleam@bit_array:to_string(_pipe@4),
gleam@result:map_error(
_pipe@5,
fun(_) -> <<"Failed to convert response to string"/utf8>> end
)
end.