Skip to content

Commit

Permalink
Allow setting recv_timeout when sending request
Browse files Browse the repository at this point in the history
  • Loading branch information
felixyz committed Jan 19, 2024
1 parent d8e96bb commit 867b807
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/gleam/hackney.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,33 @@ fn ffi_send(
b: String,
c: List(http.Header),
d: BytesBuilder,
e: Int
) -> Result(Response(BitArray), Error)

// TODO: test
pub fn send_bits(
request: Request(BytesBuilder),
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_builder.from_string)
|> send_bits,
|> send_bits(timeout),
)

case bit_array.to_string(resp.body) {
Expand Down

0 comments on commit 867b807

Please sign in to comment.