Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/gleam/hackney.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,36 @@ fn ffi_send(
b: String,
c: List(http.Header),
d: BytesTree,
e: Int,
) -> Result(Response(BitArray), Error)

// TODO: test
pub fn send_bits(
request: Request(BytesTree),
timeout: Int,
) -> Result(Response(BitArray), Error) {
use response <- result.then(
request
|> request.to_uri
|> uri.to_string
|> ffi_send(request.method, _, request.headers, request.body),
|> ffi_send(request.method, _, request.headers, request.body, timeout),
)
let headers = list.map(response.headers, normalise_header)
Ok(Response(..response, headers: headers))
}

pub fn send(req: Request(String)) -> Result(Response(String), Error) {
send_with_timeout(req, 8000)
}

pub fn send_with_timeout(
req: Request(String),
timeout: Int,
) -> Result(Response(String), Error) {
use resp <- result.then(
req
|> request.map(bytes_tree.from_string)
|> send_bits,
|> send_bits(timeout),
)

case bit_array.to_string(resp.body) {
Expand Down
6 changes: 3 additions & 3 deletions src/gleam_hackney_ffi.erl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
-module(gleam_hackney_ffi).

-export([send/4]).
-export([send/5]).

send(Method, Url, Headers, Body) ->
Options = [{with_body, true}],
send(Method, Url, Headers, Body, Timeout) ->
Options = [{with_body, true}, {recv_timeout, Timeout}],
case hackney:request(Method, Url, Headers, Body, Options) of
{ok, Status, ResponseHeaders, ResponseBody} ->
{ok, {response, Status, ResponseHeaders, ResponseBody}};
Expand Down