Skip to content

Commit

Permalink
try to use new function with timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Dec 11, 2024
1 parent 16761d1 commit ed1a39c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/Network/HTTP/Error.idr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ data HttpError e
| MissingHeader String
| UnknownTransferEncoding String
| DecompressionError String
| TimedOutError String
| OtherReason e

%runElab derive "HttpError" [Eq, Show]
28 changes: 6 additions & 22 deletions src/Network/HTTP/Scheduler.idr
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,6 @@ to_list Nothing = []
to_list (Just (x ::: xs)) = x :: xs


accumulateResponse2 : (HasIO m, Scheme a) => Channel a -> List a -> m (Either (HttpError e) (HttpResponse, List a))
accumulateResponse2 chan acc = do
maybeChunk <- channelGetNonBlocking chan
case maybeChunk of
Nothing =>
if null acc
then pure $ Left $ SocketError "No data received"
else pure $ Right (MkHttpResponse (MkDPair 200 OK) "" [], acc)
Just chunk => accumulateResponse2 chan (acc ++ [chunk])


public export
start_request : {m, e : _} -> (HasIO m, Scheduler e m scheduler) =>
scheduler ->
Expand All @@ -86,14 +75,9 @@ start_request : {m, e : _} -> (HasIO m, Scheduler e m scheduler) =>
start_request scheduler protocol msg content = do
mvar <- makeChannel
schedule_request scheduler protocol $ MkScheduleRequest msg content mvar
result <- accumulateResponse2 mvar []
case result of
Left err => pure $ Left err
Right (response, accumulatedContent) =>
let (encoding ** wit) = decompressor $ to_list $ lookup_header response.headers ContentEncoding
in pure $ Right (response, channel_to_stream (init @{wit}) (streamFromList accumulatedContent))

where
streamFromList : List a -> Stream (Of a) m (Either (HttpError e) ())
streamFromList [] = pure $ Right ()
streamFromList (x :: xs) = x `cons` streamFromList xs
Just res <- channelGetWithTimeout mvar 1000
| Nothing => pure $ Left TimedOutError "Timed out while getting response"
Right response <- res
| Left err => pure $ Left err
let (encoding ** wit) = decompressor $ to_list $ lookup_header response.raw_http_response.headers ContentEncoding
pure $ Right (response.raw_http_response, channel_to_stream (init @{wit}) response.content)

0 comments on commit ed1a39c

Please sign in to comment.